Java TreeSet is a Class int package java.util
We can use TreeSet to auto sorting data in the Tree for example in below
We can use TreeSet to auto sorting data in the Tree for example in below
Example to using java.util.TreeSet
//Create instance for TreeSet of String
TreeSet treeSet=new TreeSet();
//Store data to TreeSet with mix order
treeSet.add("aa1");
treeSet.add("aa2");
treeSet.add("aa3");
treeSet.add("bb1");
treeSet.add("bb2");
//Test containing data .. the same function for Other Collection
System.out.println("TreeSet contails "+treeSet.contains("aa1"));
System.out.println("TreeSet contails "+treeSet.contains("bb1"));
//Loop data from TreeSet
Iterator it=treeSet.iterator();
while(it.hasNext()){
System.out.println("TreeSet Next Data "+it.next());
}
Result when run program
TreeSet contails true
TreeSet contails true
TreeSet Next Data aa1
TreeSet Next Data aa2
TreeSet Next Data aa3
TreeSet Next Data bb1
TreeSet Next Data bb2
TreeSet contails true
TreeSet Next Data aa1
TreeSet Next Data aa2
TreeSet Next Data aa3
TreeSet Next Data bb1
TreeSet Next Data bb2
Amazed !! data will sorting automatic