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 tailSet()用法及代码示例
- Java TreeSet pollLast()用法及代码示例
- Java TreeSet isEmpty()用法及代码示例
- Java TreeSet clear()用法及代码示例
- Java TreeSet higher()用法及代码示例
- Java TreeSet containsAll()用法及代码示例
- Java TreeSet lower()用法及代码示例
- Java TreeSet toString()用法及代码示例
- Java TreeSet equals()用法及代码示例
- Java TreeSet last()用法及代码示例
- Java TreeSet first()用法及代码示例
- Java TreeSet addAll()用法及代码示例
- Java TreeSet toArray(T[])用法及代码示例
- Java TreeSet add()用法及代码示例
- Java TreeSet descendingIterator()用法及代码示例
- Java TreeSet floor()用法及代码示例
- Java TreeSet iterator()用法及代码示例
- Java TreeSet contains()用法及代码示例
- Java TreeSet toArray()用法及代码示例
- Java TreeSet pollFirst()用法及代码示例
注:本文由纯净天空筛选整理自 Java TreeSet Spliterator() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。