Java TreeSet Example
TreeSet is the class which is available in java.util package.
The main advantage of TreeSet is, it automatically sorts the elements.
TreeSet implements the Set interface, it doesn't allow the Null values and allows unique elements.
TreeSet has lot of methods which are very much useful.
TreeSet isEmpty() method which checks whether the TreeSet is empty or not.
TreeSet methods are remove,removeall, add,addall,contails,contailsall...etc
Java TreeSet Example Code
import java.util.Iterator;
import java.util.TreeSet;
//TreeSet Example Java
public class TreeSetExampleJava {
public static void main(String[] args) {
//TreeSet, it sorts the elements.
TreeSet<String> treeSet = new TreeSet<String>();
treeSet.add("corejava");
treeSet.add("jsp");
treeSet.add("jsp");
//treeSet.add(null);
treeSet.add("servlets");
treeSet.add("jdbc");
Iterator<String> iterator = treeSet.iterator();
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
treeSet.remove("jsp");
System.out.println("After removing jsp");
// after removing jsp
iterator = treeSet.iterator();
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
if (treeSet.contains("jdbc")) {
System.out.println("jdbc available");
}
}
}
Output :
corejava
jdbc
jsp
servlets
After removing jsp
corejava
jdbc
servlets
jdbc available
Hi there! I simply wish to give you a huge thumbs up for your excellent information you've got here on this post. I am returning to your web site for more soon.
ReplyDeleteCLick here for information.
The given information was excellent and useful. This is one of the excellent blog, I have come across. Do share more.
ReplyDeleteAndroid Training Institute in Chennai | Android Training Institute in anna nagar | Android Training Institute in omr | Android Training Institute in porur | Android Training Institute in tambaram | Android Training Institute in velachery