當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java Iterator和Spliterator的區別用法及代碼示例


Java Iterator 接口表示一個能夠迭代 Java 對象集合的對象,一次一個對象。 Iterator 接口是 Java 中用於迭代對象集合的最古老的機製之一(盡管不是最古老的——Enumerator 早於 Iterator)。

此外,迭代器與枚舉有兩點不同:

1. 迭代器允許調用者在元素迭代期間從指定集合中刪除給定元素。

2. 方法名稱得到增強。

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, website]
Welcome
to
our
website
After the remove() method is called : [Welcome, to, our]

就像迭代器ListIterator,分離器 是一個 Java 迭代器,用於從 List 實現的對象中迭代元素 one-by-one。

Spliterator的主要函數有:

  • 拆分源數據
  • 處理源數據

JDK 8 中包含 Interface Spliterator,用於利用順序遍曆之外的並行性優勢。它被設計為迭代器的並行模擬。

Java


// 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中Iterator和Spliterator的區別:

迭代器

Spliterator

Java 1.2 中引入 Java 1.8 中引入
迭代器僅單獨迭代元素 Spliterator 可以單獨或批量遍曆元素
它是整個集合 API 的迭代器 它是 Collection 和 Stream API 的迭代器,Map 實現類除外
它使用外部迭代 它使用內部迭代。
它是一個通用迭代器 它不是通用迭代器
不支持並行編程 它通過分割給定的元素集來支持並行編程,以便每個集合都可以單獨處理。


相關用法


注:本文由純淨天空篩選整理自gargmikku941大神的英文原創作品 Difference Between Iterator and Spliterator in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。