Skip to main content

Posts

Showing posts with the label TreeSet

Collection(TreeSet)

TreeSet:- TreeSet is an implemented class of Set interface where TreeSet is a group of individual objects as a single entity where duplicates are not allowed but insertion should be according to some sorting order and elements must be comparable. If we are not writing some additional code for sorting than automatically elements insert according to the default natural sorting (ascending order for numbers), if we want to customize sorting than we have to implements Comparator interface present in java.util package and implements compare method. Default natural sorting is done by the comparable interface which is present in java.lang package, having only one method compareTo. Example1:-TestTreeSet.java import java.util.*; class TestTreeSet { public static void main(String[]args) { TreeSet ts=new TreeSet(); ts.add(50); ts.add(40); ts.add(60); ts.add(30); ts.add(70); ts.add(20); ts.add(80); ts.add(10); System.out.println(ts); Iterator itr=ts.iterator(); ...