概述
There are various ways to convert an Iterator into Iterable,
有多种方法可以将Iterator转换为Iterable,
Overriding an abstract method iterator()
覆盖抽象方法iterator()
Lambda expression in Java 8
Java 8中的Lambda表达式
By using Spliterators
通过使用分离器
1)覆盖抽象方法iterator() (1) Overriding an abstract method iterator() )
We will see in various steps to override an abstract method iterator(),
我们将在各个步骤中看到重写一个抽象方法iterator()的过程,
We will get the Iterator object by using iterator() method of Collection.
我们将使用Collection的iterator()方法获取Iterator对象。
We will get the Iterable object by overriding an iterator() method.
我们将通过重写iterator()方法来获取Iterable对象。
We will define another method and in this method, we will pass the Iterator object and override iterator() method and then return Iterator object.
我们将定义另一个方法,在此方法中,我们将传递Iterator对象并覆盖iterator()方法,然后返回Iterator对象。
Example:
例:
// Java program to demonstrate the example of overriding
// an abstract method iterator() to convert Iterator into Iterable
import java.util.*;
public class ConvertIteratorToIterable {
// This is a user defined method to convert Iterator into Iterable
public static Iterable convertIterableFromIterator(Iterator iterator) {
return new Iterable() {
// Overriding an abstract method iterator()
public Iterator iterator() {
return iterator;
}
};
}
public static void main(String[] args) {
// Instantiating Iterator Object
Iterator iterate = Arrays.asList(10, 20, 30, 40).iterator();
// Now, we will get the Iterable from Iterator
Iterable iterable = convertIterableFromIterator(iterate);
// Display converted Iterable elements
iterable.forEach(System.out::println);
}
}
Output
输出量
10
20
30
40
2)Java 8中的Lambda表达式 (2) Lambda expression in Java 8)
We will see in various steps and the steps are given below:
我们将在各个步骤中看到以下步骤:
In the first step, Get the Iterator.
第一步,获取迭代器。
In the second step, we will convert Iterator into Iterable without overriding an abstract method iterator().
在第二步中,我们将把Iterator转换为Iterable而不覆盖抽象方法iterator() 。
In the third step, we will return Iterable from the method convertIteratorToIterable() and passed the Iterator object and get Iterable.
第三步,我们将从convertIteratorToIterable()方法返回Iterable,并传递Iterator对象并获得Iterable。
Example:
例:
// Java program to demonstrate the example of
// Lambda expression in Java 8 to convert Iterator into Iterable
import java.util.*;
public class ConvertIteratorToIterable {
// This is a user defined method to convert Iterator into Iterable
public static Iterable convertIterableFromIterator(Iterator iterator) {
return () -> iterator;
}
public static void main(String[] args) {
// Instantiating Iterator Object
Iterator iterate = Arrays.asList(10, 20, 30, 40).iterator();
// Now, we will get the Iterable from Iterator
Iterable iterable = convertIterableFromIterator(iterate);
// Display converted Iterable elements
iterable.forEach(System.out::println);
}
}
Output
输出量
10
20
30
40
3)通过使用分离器 (3) By using Spliterators)
We will see in few steps and the steps are given below:
我们将在几个步骤中看到以下步骤:
To get the Iterator and convert the Iterator into Spliterator by using Spliterators.spliteratorUnknownSize() method and by using this method we will take iterator object, size, a Boolean value.
要获取Iterator并通过使用Spliterators.spliteratorUnknownSize()方法将Iterator转换为Spliterator,并使用此方法,我们将使用iterator对象,大小和布尔值。
Now, our data is ready in Spliterator and again we will convert Spliterator into Stream by using stream() method.
现在,我们的数据已在Spliterator中准备好了,再次我们将使用stream()方法将Spliterator转换为Stream。
Now, our data is ready in Stream and again we will convert Stream to Iterable by using the collect() method.
现在,我们的数据已经在Stream中准备好了,再次,我们将使用collect()方法将Stream转换为Iterable。
At last, we will return the Iterable.
最后,我们将返回Iterable。
Example:
例:
// Java program to demonstrate the example of Spliterators to
// convert an Iterator into Iterable
import java.util.*;
import java.util.stream.*;
public class ConvertIteratorToIterable {
// This is a user defined method to convert Iterator into Iterable
public static < T > Iterable < T > convertIterableFromIterator(Iterator < T > iterator) {
return StreamSupport.stream(Spliterators.spliteratorUnknownSize(iterator, 0), false).collect(Collectors.toList());
}
public static void main(String[] args) {
// Instantiating Iterator Object
Iterator < Double > iterate = Arrays.asList(10.0, 20.0, 30.0, 40.0).iterator();
// Now, we will get the Iterable from Iterator
Iterable < Double > iterable = convertIterableFromIterator(iterate);
// Display converted Iterable elements
iterable.forEach(System.out::println);
}
}
Output
输出量
10.0
20.0
30.0
40.0
翻译自: https://www.includehelp.com/java/how-to-convert-an-iterator-into-iterable-in-java.aspx
最后
以上就是沉默红牛为你收集整理的如何在Java中将Iterator转换为Iterable?的全部内容,希望文章能够帮你解决如何在Java中将Iterator转换为Iterable?所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复