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


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


TreeSet 类的 spliterator() 方法用于对集合元素进行拆分并一一迭代。

用法:

public Spliterator<E> spliterator()

返回:

它返回集合的拆分器元素。

例子1

import java.util.*;
public class JavaTreeSetSpilteratorExample1
{
  public static void main(String as[]){
  TreeSet <Integer>obj = new TreeSet<Integer>();
 
  obj.add(4);	
  obj.add(6);
  obj.add(9);
  obj.add(12);
  obj.add(18);
  System.out.println("TreeSet:" +obj);
  Spliterator<Integer> obj1 = obj.spliterator();
   System.out.println("Elements of TreeSet after using spliterator() method:");
  obj1.forEachRemaining(System.out::println);	
  }
}

输出:

TreeSet:[4, 6, 9, 12, 18]
Elements of TreeSet after using spliterator() method:
4
6
9
12
18

例子2

import java.util.*;
class Employee3{  
    private String name;  
    private String surname;  
    private String designation;  
    public Employee3(String name, String surname, String designation) {  
        this.name = name;  
        this.surname = surname;  
        this.designation = designation;  
    }  
   
    public String toString(){  
       return name+"  " +surname+"  "+designation;  
    }  
}  
public class JavaTreeSetSpilteratorExample2{  
    public static void main(String[] args) {  
        Employee3 employee1 = new Employee3("shristee", "vishwakarma", "Analyst");  
        Employee3 employee2 = new Employee3("shweta  ", "singh      ", "Accountant");  
        Employee3 employee3 = new Employee3("suman   ", "chandra    ", "Computer Engineer ");  
          TreeSet <String> obj = new TreeSet<String>();
          
          String s1=employee1.toString();
          String s2=employee2.toString();
          String s3=employee3.toString();
         obj.add(s1);  
        obj.add(s2);  
        obj.add(s3);  
        System.out.println("Elements in TreeSet:");
        for (String xyz:obj) {  
            System.out.println(xyz);  
        }
       Spliterator<String> obj1 = obj.spliterator();
   System.out.println("Elements of TreeSet after using spliterator() method:");
  obj1.forEachRemaining(System.out::println);	
        
    }  
}

输出:

Elements in TreeSet:
shristee  vishwakarma  Analyst
shweta    singh        Accountant
suman     chandra      Computer Engineer 
Elements of TreeSet after using spliterator() method:
shristee  vishwakarma  Analyst
shweta    singh        Accountant
suman     chandra      Computer Engineer




相关用法


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