Click here to watch in Youtube :
https://www.youtube.com/watch?v=T7fjzdgavIg&list=UUhwKlOVR041tngjerWxVccw
AscendingNameComparator.java
TreeSetExample.java
https://www.youtube.com/watch?v=T7fjzdgavIg&list=UUhwKlOVR041tngjerWxVccw
AscendingNameComparator.java
import java.util.Comparator; public class AscendingNameComparator implements Comparator<String> { /* * This method used to arrange the names in ascending order. */ @Override public int compare( String name1, String name2 ) { return name1.compareTo(name2); } }
import java.util.Comparator; import java.util.TreeSet; /* * Example of comparator() method. */ public class TreeSetExample { public static void main( String[] args ) { AscendingNameComparator ascendingNameComparator = new AscendingNameComparator(); TreeSet<String> treeSet = new TreeSet<String>(ascendingNameComparator); treeSet.add("Balu"); treeSet.add("Ajay"); treeSet.add("David"); treeSet.add("Charles"); /* * Returns the comparator used to order the elements in this set, or * null if this set uses the natural ordering of its elements. */ Comparator<?> comparator = treeSet.comparator(); System.out.println("comparator used : " + comparator.getClass().getName()); } }
comparator used : AscendingNameComparator
https://sites.google.com/site/javaee4321/java-collections/TreeSetDemoComparatorMethod.zip?attredirects=0&d=1
See also:
No comments:
Post a Comment