Java中迭代器和分离器的区别
// Java program to illustrate Iterator interface
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
public class JavaIteratorExample1 {
public static void main(String[] args)
{
// create a linkedlist
List<String> list = new LinkedList<>();
// Add elements
list.add("Welcome");
list.add("to");
list.add("our");
list.add("website");
// print the list to the console
System.out.println("The list is given as : " + list);
// call iterator on the list
Iterator<String> itr = list.iterator();
// itr.hasNext() returns true if there is still an element next to the current element pointed by iterator
while (itr.hasNext()) {
// Returns the next element.
System.out.println(itr.next());
}
// Removes the last element.
itr.remove();
// print the list after removing an element
System.out.println("After the remove() method is called : "+ list);
}
}
运行结果:
The list is given as : [Welcome, to, our, geekdocs]
Welcome
to
our
geekdocs
After the remove() method is called : [Welcome, to, our]
像Iterator和ListIterator一样,Spliterator也是一个Java Iterator,它被用来从List实现的对象中逐一迭代元素。
Spliterator的主要功能是:
- 分割源数据
- 处理源数据
JDK 8中包含了接口Spliterator,以便在顺序遍历之外利用并行的优势。它被设计成一个迭代器的并行类似物。
// Java program to illustrate a Spliterator
import java.util.*;
import java.util.stream.Stream;
public class InterfaceSpliteratorExample {
public static void main(String args[])
{
// Create an object of array list
ArrayList<Integer> list = new ArrayList<>();
// Add elements to the array list
list.add(101);
list.add(201);
list.add(301);
list.add(401);
list.add(501);
// create a stream on the list
Stream<Integer> str = list.stream();
// Get Spliterator object on stream
Spliterator<Integer> splitr = str.spliterator();
// Get size of the list encountered by the forEachRemaining method
System.out.println("Estimate size: " + splitr.estimateSize());
// Print getExactSizeIfKnown returns exact size if finite or return -1
System.out.println("Exact size: "+ splitr.getExactSizeIfKnown());
// Check if the Spliterator has all
// the characteristics
System.out.println("Boolean Result: "+ splitr.hasCharacteristics(splitr.characteristics()));
System.out.println("Elements of ArrayList :");
// print elements using forEachRemaining
splitr.forEachRemaining((n) -> System.out.println(n));
// Obtaining another Stream to the array list.
Stream<Integer> str1 = list.stream();
splitr = str1.spliterator();
// Obtain spliterator using trySplit() method
Spliterator<Integer> splitr2 = splitr.trySplit();
// If splitr can be partitioned use splitr2 first.
if (splitr2 != null) {
System.out.println("Output from splitr2: ");
splitr2.forEachRemaining(
(n) -> System.out.println(n));
}
// Now, use the splitr
System.out.println("Output from splitr1: ");
splitr.forEachRemaining(
(n) -> System.out.println(n));
}
}
运行结果:
Estimate size: 5
Exact size: 5
Boolean Result: true
Elements of ArrayList :
101
201
301
401
501
Output from splitr2:
101
201
Output from splitr1:
301
401
501
java中迭代器和迭代器的区别
迭代器 | 迭代器 |
---|---|
引入于Java 1.2 | 引入于Java 1.8 |
迭代器只对元素进行单独迭代 | 迭代器对元素进行单独和批量的追踪 |
迭代器是整个集合API的一个迭代器 | 迭代器是集合和流API的一个迭代器,除了Map实现类之外 |
迭代器使用外部迭代 | 迭代器使用内部迭代。 |
迭代器是一个通用迭代器 | 迭代器不是一个通用迭代器 |
迭代器不支持并行编程 | 迭代器通过分割给定的元素集来支持并行编程,这样每个元素集可以被单独处理。 |