当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java ArrayBlockingQueue Spliterator()用法及代码示例


Java ArrayBlockingQueue 类的 Spliterator() 方法在此 ArrayBlockingQueue 中的元素上返回一个 Spliterator。

用法:

public Spliterator<E> spliterator()

参数:

NA

指定者:

ArrayBlockingQueue 类的 Spliterator() 方法指定为:

  • 接口 Collection<E> 中的 Spliterator。
  • 接口 Iterable<E> 中的 Spliterator

返回值:

Spliterator() 方法在此 ArrayBlockingQueue 中的元素上返回一个 Spliterator。

例子1

import java.util.Spliterator;
import java.util.concurrent.ArrayBlockingQueue;

public class ArrayBlockingQueueSpliteratorExample1 {
    static int i=1;
    public static void main(String[] args) {
        int capacity =100;
        ArrayBlockingQueue<String> queue = new ArrayBlockingQueue <String>(capacity);
         //add add elements to the ArrayBlockingQueue
        queue.add("Reema");
        queue.add("Varun");
        queue.add("Sukla");
        //spliterator  split and iterate the split parts in parallel
        Spliterator<String> str = queue.spliterator();
        // if element exists tryAdvance() will perform action
        while(str.tryAdvance((n)->System.out.println(i++ +" Name - "+n)));
    }
}

输出:

1 Name - Reema
2 Name - Varun
3 Name - Sukla

例子2

import java.util.Spliterator;
import java.util.concurrent.ArrayBlockingQueue;

class StudentProfile {
    Integer marks;
    String rollNo, name;
    static int i=1;

    public StudentProfile(String rollNo, String name, int marks) {
        this.rollNo = rollNo;
        this.name = name;
        this.marks = marks;
    }
    public String toString(){
        return i++ +" Roll No = "+rollNo +"\n Name = "+name +"\n Marks = "+marks+"\n";
    }
}
public class ArrayBlockingQueueSpliteratorExample2 {
    public static void main(String[] args) {
        int capacity=100;
        ArrayBlockingQueue<StudentProfile> Pqueue = new ArrayBlockingQueue<StudentProfile>(capacity);
        StudentProfile val1 = new StudentProfile("15cs1029","Reema panda",21);
        StudentProfile val2 = new StudentProfile("15cs1012","Geetanjali Sharma",26);
        StudentProfile val3 = new StudentProfile("15cs1011","Himanshu Bhardwaj",27);
        Pqueue.add(val1);
        Pqueue.add(val2);
        Pqueue.add(val3);
        Spliterator<StudentProfile> str = Pqueue.spliterator();
        while(str.tryAdvance((n)->System.out.println(n)));
    }
}

输出:

1 Roll No = 15cs1029
 Name = Reema panda
 Marks = 21

2 Roll No = 15cs1012
 Name = Geetanjali Sharma
 Marks = 26

3 Roll No = 15cs1011
 Name = Himanshu Bhardwaj
 Marks = 27

例子3

import java.util.Spliterator;
import java.util.concurrent.ArrayBlockingQueue;

public class ArrayBlockingQueueSpliteratorExample3 {
    static int i =0;
    public static void main(String[] args) {
        int capacity=100;
        ArrayBlockingQueue<String> queue = new ArrayBlockingQueue <String>(capacity);
        // add elements to the Priority queue
        queue.add("reema");
        queue.add("bhavika");
        queue.add("chehak");
        queue.add("chandan");
        //spliterator  split and iterate the split parts in parallel
        Spliterator<String> str = queue.spliterator();
        // perform action for each remaining element
        str.forEachRemaining(
                (n)-> {
                    String lc = n.toUpperCase();
                    System.out.println(++i +" Lower case = "+n);
                    System.out.println(i +" Upper case = "+lc);
                    System.out.println();
                }
        );
    }
}

输出:

1 Lower case = reema
1 Upper case = REEMA

2 Lower case = bhavika
2 Upper case = BHAVIKA

3 Lower case = chehak
3 Upper case = CHEHAK

4 Lower case = chandan
4 Upper case = CHANDAN




相关用法


注:本文由纯净天空筛选整理自 Java ArrayBlockingQueue Spliterator() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。