java treeset with comparator code example
Example: TreeSet comparator() method in java
import java.util.TreeSet;
public class TreeSetComparatorMethodExample
{
public static void main(String[] args)
{
TreeSet<Integer> ts = new TreeSet<Integer>();
TreeSet<Integer> comp = new TreeSet<Integer>();
ts.add(5);
ts.add(6);
ts.add(7);
ts.add(8);
ts.add(9);
ts.add(10);
comp = (TreeSet)ts.comparator();
if(comp != null)
{
for(Integer element : comp)
{
System.out.println(element + " ");
}
}
else
{
System.out.println("TreeSet comparator value: " + comp);
System.out.println("Hence it is using natural ordering.");
}
}
}