📜  Java中将 HashSet 转换为 ArrayList

📅  最后修改于: 2022-05-13 01:55:46.978000             🧑  作者: Mango

Java中将 HashSet 转换为 ArrayList

数组列表 class 是一个可调整大小的数组,存在于Java.util 包中。 Java中的数组和 ArrayList 之间的区别在于,数组的大小不能修改(即,如果您想向/从数组中添加/添加或删除元素,则必须创建一个新数组。但是,可以在 ArrayList 中添加/追加或删除元素,而无需创建新数组。

哈希集 Java.util包中的类用于创建使用哈希表存储数据项的集合。 HashSet 使用散列机制存储元素。HashSet 只包含唯一元素并且允许空值。 HashSet 不维护插入顺序,元素是根据它们的哈希码插入的。 HashSet 是搜索操作的最佳方法。

为了将 HashSet 转换为 Arraylist,我们需要使用 ArrayList 构造函数并将 HashSet 实例作为构造函数参数传递。它将所有元素从 HashSet 复制到新创建的 ArrayList。

Java
// Java program to convert HashSet to ArrayList
import java.io.*;
import java.util.ArrayList;
import java.util.HashSet;
class GFG {
    public static void main(String[] args)
    {
 
        HashSet flower_set = new HashSet<>();
 
        flower_set.add("tulip");
        flower_set.add("rose");
        flower_set.add("orchid");
        flower_set.add("marie-gold");
 
        // Pass hashset to arraylist constructor
        ArrayList flower_array
            = new ArrayList<>(flower_set);
 
        // all elements from hashset are copied to arraylist
        System.out.println(
            "Elements of flower Arraylist are :");
        System.out.println(flower_array);
 
        // using the get method of Arraylist to get the
        // element at index=0
        System.out.print("Element at index 0 is : "
                         + flower_array.get(0) + " ");
    }
}


Java
// Java Program to convert the HashSet to ArrayList
// Using Stream Api Java 8
 
import java.io.*;
import java.util.*;
import java.util.stream.*;
class GFG {
    public static void main(String[] args)
    {
        HashSet flower_set = new HashSet<>();
 
        flower_set.add("tulip");
        flower_set.add("rose");
        flower_set.add("orchid");
        flower_set.add("marie-gold");
 
        // Using stream api
        // and typecasting the List object to ArraylList
        ArrayList flower_array
            = (ArrayList)flower_set.stream()
                  .collect(Collectors.toList());
 
        // all elements from hashset are copied to arraylist
        System.out.println(
            "Elements of flower Arraylist are :");
        System.out.println(flower_array);
 
        // using the get method of Arraylist to get the
        // element at index=0
        System.out.print("Element at index 0 is : "
                         + flower_array.get(0) + " ");
    }
}


输出
Elements of flower Arraylist are :
[marie-gold, rose, tulip, orchid]
Element at index 0 is : marie-gold


还有一种将HashSet对象转换为ArrayList的方法是使用Java 8中引入的stream api。我们将使用Collectors类的toList()方法将输入数据转换为List,但我们希望它是一个ArrayList,所以我们将在前面使用此 (ArrayList) 将其类型转换为 ArrayList。

Java

// Java Program to convert the HashSet to ArrayList
// Using Stream Api Java 8
 
import java.io.*;
import java.util.*;
import java.util.stream.*;
class GFG {
    public static void main(String[] args)
    {
        HashSet flower_set = new HashSet<>();
 
        flower_set.add("tulip");
        flower_set.add("rose");
        flower_set.add("orchid");
        flower_set.add("marie-gold");
 
        // Using stream api
        // and typecasting the List object to ArraylList
        ArrayList flower_array
            = (ArrayList)flower_set.stream()
                  .collect(Collectors.toList());
 
        // all elements from hashset are copied to arraylist
        System.out.println(
            "Elements of flower Arraylist are :");
        System.out.println(flower_array);
 
        // using the get method of Arraylist to get the
        // element at index=0
        System.out.print("Element at index 0 is : "
                         + flower_array.get(0) + " ");
    }
}
输出
Elements of flower Arraylist are :
[marie-gold, rose, tulip, orchid]
Element at index 0 is : marie-gold