给予 设置 ( 哈希集 或 有序树 )Java中的字符串,将其转换为列表( ArrayList 或 链表 )弦乐。
null
Input : Set hash_Set = new HashSet(); hash_Set.add("Geeks"); hash_Set.add("For"); Output : ArrayList or Linked List with following content {"Geeks", "for"}
用Java设置: 集合接口出现在 JAVAutil 打包并扩展 收集接口 是一个无序的对象集合,其中无法存储重复的值。
Java中的列表: 列表界面提供了一种存储有序集合的方法。它是集合的子接口。它是可以存储重复值的有序对象集合。
Java中将集合转换为列表的方法
在Java中,有很多方法可以将集合转换为列表。下面列出了其中一些。
- 使用集合遍历
- 使用Arraylist或LinkedList构造函数
- 使用addAll()方法
- 在Java中使用流
1.使用集合遍历
我们只是创建一个列表。我们遍历给定的集合,一个接一个地向列表中添加元素。
JAVA
// 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的构造函数将集合转换为列表。
JAVA
// 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中的Set-to-List转换也可以使用Java List的addAll()方法执行。
JAVA
// Java program to demonstrate conversion of // Set to array using addAll() method. 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" ); List<String> aList = new ArrayList<String>(); aList.addAll(s); System.out.println( "Created ArrayList is" ); for (String x : aList) System.out.println(x); List<String> lList = new LinkedList<String>(); lList.addAll(s); System.out.println( "Created LinkedList is" ); for (String x : lList) System.out.println(x); } } |
输出
Created ArrayList is Geeks for Created LinkedList is Geeks for
4.使用 Java中的流
我们使用Java中的流将给定的集合转换为steam,然后将流转换为list。这只适用于Java8或之后的版本。
JAVA
// Java program to demonstrate conversion of // Set to list using stream import java.util.*; import java.util.stream.*; 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" ); List<String> aList = s.stream().collect(Collectors.toList()); for (String x : aList) System.out.println(x); } } |
输出
Geeks for
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END