iterable java
There are various ways to convert Iterable to Collection in Java programming language.
有多种方法可以用Java编程语言将Iterable转换为Collection 。
-
With the help of creating a utility function
在创建实用程序功能的帮助下
-
-
By using for loop
通过使用for循环
-
By using forEach() method of Iterable
通过使用Iterable的forEach()方法
-
By using Iterator
通过使用迭代器
-
With the help of stream with collect() method in Java 8
借助Java 8中collect()方法的流
-
With the help of utility function
借助实用程序功能
In this method, we will change or convert Iterable to Collection explicitly (i.e. we will take each element in an object manually).
在这种方法中,我们将明确地将Iterable更改或转换为Collection(即,我们将手动获取对象中的每个元素)。
i)通过使用循环 (i) By using for Loop)
// Java program to demonstrate the example of
// converting an Iterable into a Collection by using for loop
import java.util.*;
import java.io.*;
public class ConvertIterableToCollection {
// This is a user defined method which is used to
// convert Iterable to Collection
public static < T > Collection < T >
convertCollectionFromIterable(Iterable < T > iterable) {
// Create a blank Collection to hold the result
Collection < T > collect = new LinkedList < T > ();
// By using for loop to iterate through the
// iterable to add each element
for (T type: iterable)
collect.add(type);
return collect;
}
public static void main(String[] args) {
Iterable < Double > itr = Arrays.asList(10.0, 20.0, 30.0, 40.0);
System.out.println("The values of Iterable list are : " + itr);
Collection < Double > coll = convertCollectionFromIterable(itr);
System.out.println("The values of Collection list are : " + coll);
}
}
Output
输出量
E:\Programs>javac ConvertIterableToCollection.java
E:\Programs>java ConvertIterableToCollection
The values of Iterable list are : [10.0, 20.0, 30.0, 40.0]
The values of Collection list are : [10.0, 20.0, 30.0, 40.0]
ii)使用Iterable的forEach() (ii) By using forEach() of Iterable)
This method is available in Java 8 or higher versions so it supports java8 or higher versions.
此方法在Java 8或更高版本中可用,因此它支持java8或更高版本。
// Java program to demonstrate the example of converting
// an Iterable into a Collection by using forEach() of Iterable.
import java.util.*;
import java.io.*;
public class ConvertIterableToCollection {
// This is a user defined method which is used to
// convert Iterable to Collection
public static < T > Collection < T >
convertCollectionFromIterable(Iterable < T > iterable) {
// Create a blank Collection to hold the result
Collection < T > collect = new LinkedList < T > ();
// By using forEach() to iterate through
// the iterable to add each element
iterable.forEach(collect::add);
return collect;
}
public static void main(String[] args) {
Iterable < Double > itr = Arrays.asList(10.0, 20.0, 30.0, 40.0);
System.out.println("The values of Iterable list are : " + itr);
Collection < Double > coll = convertCollectionFromIterable(itr);
System.out.println("The values of Collection list are : " + coll);
}
}
Output
输出量
E:\Programs>javac ConvertIterableToCollection.java
E:\Programs>java ConvertIterableToCollection
The values of Iterable list are : [10.0, 20.0, 30.0, 40.0]
The values of Collection list are : [10.0, 20.0, 30.0, 40.0]
iii)使用迭代器 (iii) By using Iterator)
// Java program to demonstrate the example of
// converting an Iterable into a Collection by using Iterator.
import java.util.*;
import java.io.*;
public class ConvertIterableToCollection {
// This is a user defined method which is used to
// convert Iterable to Collection
public static < T > Collection < T >
convertCollectionFromIterable(Iterable < T > iterable) {
// Create a blank Collection to hold the result
Collection < T > collect = new LinkedList < T > ();
// By using Iterator to get the Iterator
Iterator < T > iterate = iterable.iterator();
// By using Iterator to iterate through the iterable
// to add each element into the Collection
while (iterate.hasNext())
collect.add(iterate.next());
return collect;
}
public static void main(String[] args) {
Iterable < Double > itr = Arrays.asList(10.0, 20.0, 30.0, 40.0);
System.out.println("The values of Iterable list are : " + itr);
Collection < Double > coll = convertCollectionFromIterable(itr);
System.out.println("The values of Collection list are : " + coll);
}
}
Output
输出量
E:\Programs>javac ConvertIterableToCollection.java
E:\Programs>java ConvertIterableToCollection
The values of Iterable list are : [10.0, 20.0, 30.0, 40.0]
The values of Collection list are : [10.0, 20.0, 30.0, 40.0]
2)借助Java 8中的collect()方法的流 (2) With the help of stream with collect() method in Java 8)
In this method Iterable first convert into spliterator then after with the help of StreamSupport.stream() the spliterator can be traversed and then collected with the help of collect() into Collection.
在此方法中,Iterable首先转换为拆分器,然后在StreamSupport.stream()的帮助下遍历拆分器,然后借助collect()将其收集到Collection中。
// Java program to demonstrate the example of stream()
// with collect() to convert an Iterable into Collection
import java.util.*;
import java.io.*;
import java.util.stream.*;
public class ConvertIterableToCollection {
// This is a user defined method which is used
// to convert Iterable to Collection
public static < T > Collection < T >
convertCollectionFromIterable(Iterable < T > iterable) {
// Create a blank Collection to hold the result
Collection < T > collect = new LinkedList < T > ();
return StreamSupport.stream(iterable.spliterator(), false).collect(Collectors.toList());
}
public static void main(String[] args) {
Iterable < Double > itr = Arrays.asList(10.0, 20.0, 30.0, 40.0);
System.out.println("The values of Iterable list are : " + itr);
Collection < Double > coll = convertCollectionFromIterable(itr);
System.out.println("The values of Collection list are : " + coll);
}
}
Output
输出量
E:\Programs>javac ConvertIterableToCollection.java
E:\Programs>java ConvertIterableToCollection
The values of Iterable list are : [10.0, 20.0, 30.0, 40.0]
The values of Collection list are : [10.0, 20.0, 30.0, 40.0]
翻译自: https://www.includehelp.com/java/convert-iterable-to-collection-in-java.aspx
iterable java