Java中的Set和List转换
给定Java中的一个字符串集合(HashSet或TreeSet),将其转换为一个字符串列表(ArrayList或LinkedList)。在Java中, Set接口 位于java.util包中,扩展了Collection接口,是一个无序的对象集合,其中不能存储重复的值。 List接口 提供了一种存储有序集合的方法。它是Collection的子接口。它是一个有序的对象集合,其中可以存储重复的值。
输入: Set hash_Set = new HashSet();
hash_Set.add(“Geeks”);
hash_Set.add(“For”);
输出: 具有以下内容的ArrayList或LinkedList
{“Geeks”, “for”}
Java中将Set转换为List的方法
有许多方法可以在Java中将Set转换为List。以下是其中的一些方法。
- 使用Set Traversing
- 使用ArrayList或LinkedList构造函数
- 使用addAll()方法
- 使用Java中的Stream
- 使用Java中的List.copyOf()
1. 使用Set遍历
我们只需创建一个列表。我们遍历给定的集合,然后逐个将元素添加到列表中。
// Java program to demonstrate conversion of
// Set to array using simple traversal
import java.util.*;
class Test {
public static void main(String[] args)
{
// Creating a hash set of strings
Set<String> s = new HashSet<String>();
s.add("Geeks");
s.add("for");
int n = s.size();
List<String> aList = new ArrayList<String>(n);
for (String x : s)
aList.add(x);
System.out.println("Created ArrayList is");
for (String x : aList)
System.out.println(x);
// We can create LinkedList same way
}
}
输出
Created ArrayList is
Geeks
for
2. 使用ArrayList或LinkedList构造函数
我们可以使用ArrayList或LinkedList的构造函数将Set简单地转换为List。
// Java program to demonstrate conversion of
// Set to list using constructor
import java.util.*;
class Test {
public static void main(String[] args)
{
// Creating a hash set of strings
Set<String> s = new HashSet<String>();
s.add("Geeks");
s.add("for");
// Creating an array list using constructor
List<String> aList = new ArrayList<String>(s);
System.out.println("Created ArrayList is");
for (String x : aList)
System.out.println(x);
System.out.println("Created LinkedList is");
List<String> lList = new LinkedList<String>(s);
for (String x : lList)
System.out.println(x);
}
}
输出
Created ArrayList is
Geeks
for
Created LinkedList is
Geeks
for
3. 使用addAll()方法
在Java中,还可以使用Java List的addAll()方法执行从Set到List的转换。
// Java程序演示:如何将Set转换为数组,使用addAll()方法
import java.util.*;
class Test {
public static void main(String[] args)
{
// 创建一个字符串哈希集
Set s = new HashSet();
s.add("Geeks");
s.add("for");
List aList = new ArrayList();
aList.addAll(s);
System.out.println("创建的ArrayList是");
for (String x : aList)
System.out.println(x);
List lList = new LinkedList();
lList.addAll(s);
System.out.println("创建的LinkedList是");
for (String x : lList)
System.out.println(x);
}
}
输出结果
创建的ArrayList是
Geeks
for
创建的LinkedList是
Geeks
for
4. 在Java中使用流(Stream)进行转换
我们可以使用Java中的流(Stream)将给定的Set转换为流,然后流转换为列表。这仅适用于Java 8或之后的版本。
// Java程序演示:如何使用流(Stream)将Set转换为列表
import java.util.*;
import java.util.stream.*;
class Test {
public static void main(String[] args)
{
// 创建一个字符串哈希集
Set s = new HashSet();
s.add("Geeks");
s.add("for");
List aList
= s.stream().collect(Collectors.toList());
for (String x : aList)
System.out.println(x);
}
}
输出结果
Geeks
for
5. 使用List.copyOf()方法
我们可以使用Java中的List.copyOf(object)将Set转换为列表。为此,我们必须导入包 **java.util.List.*** 。这是一个创建列表对象的静态工厂方法,可以从另一个集合或对象中创建列表对象。
// Java程序演示:如何使用List.copyOf()方法将Set转换为列表
import java.io.*;
import java.util.*;
class GFG {
public static void main(String[] args)
{
Set s = new HashSet();
s.add("geeks");
s.add("forgeeks");
s.add("learning");
s.add("platform");
List aList = new ArrayList<>();
aList = List.copyOf(s);
System.out.println("创建的ArrayList是 :"
+ aList);
}
}
输出结果
创建的ArrayList是 :[geeks, forgeeks, learning, platform]
极客教程