Object a = 5; Object b = 6; Comparator rawComp = new MyComparator(); int comp = rawComp.compare(a, b);
public static final Comparator<String> NULL_SAFE_COMPARATOR = new NullComparator(String.CASE_INSENSITIVE_ORDER); @Override public int compareTo(Metadata other) { int result = NULL_SAFE_COMPARATOR.compare(this.name, other.name); if (result != 0) { return result; } return NULL_SAFE_COMPARATOR.compare(this.value, other.value); }
Collections.sort(list, Collections.reverseOrder(new Comparator()));
public static <K, V extends Comparable<? super V>> List<K> getKeysSortedByValue2(Map<K, V> map) { final int size = map.size(); final List reusedList = new ArrayList(size); final List<Map.Entry<K, V>> meView = reusedList; meView.addAll(map.entrySet()); Collections.sort(meView, SINGLE); final List<K> keyView = reusedList; for (int i = 0; i < size; i++) { keyView.set(i, meView.get(i).getKey()); } return keyView; } private static final Comparator SINGLE = new ValueComparator();
Comparator<T> cmp = new MyComparator<T>(); TreeSet<T> t = new TreeSet<T>(cmp); l.add(someT);
Comparator comp = new MyComparator(); int result = comp.compare(object1, object2);
class Strategies { private static final Comparator<Person> PERSON_NAME_LENGTH_COMPARATOR = new StartegyOne(); private static final Comparator<Person> PERSON_NAME_LEXICAL_COMPARATOR = new StartegyTwo(); public static Comparator<Person> personNameLengthComparator(){ return PERSON_NAME_LENGTH_COMPARATOR; } public static Comparator<Person> personNameLexicalComparator(){ return PERSON_NAME_LEXICAL_COMPARATOR; } }
Comparator<Zodiac> byMonthComparator = new Comparator<>() { int compare(Zodiac o1, Zodiac o2) { return Integer.compare(o1.month, o2.month); } }
Comparator<Dog> dogComp = new DogComparator(); // Invalid Java, and nonsensical -- we couldn't use our DogComparator to compare cats! // Comparator<Animal> animalComp = new DogComparator(); // Invalid Java, because Comparator is invariant in T // Comparator<Doberman> dobermanComp = new DogComparator(); // So we introduce a contra-variance annotation on the type of dobermanComp. Comparator<? super Doberman> dobermanComp = new DogComparator();
Comparator<Map.Entry<String, Integer>> comparator = new Comparator<>() { public @Override int compare(Map.Entry<String, Integer> a, Map.Entry<String, Integer> b) { return a.getValue().intValue() - b.getValue().intValue(); } }
private static void SelectionSort (Temperature[] array, int size) { Comparator<Temperature> comparator = new TemperatureComparator(); ... if ( comparator.compare(array[j], array[indexLowest]) < 0 ) ... }
Comparator<String[]> comp = new Comparator<>() { @Override public int compare(String[] student1, String[] student2) { // comparison logic goes here // return: // a negative number if student1 goes before student2 // 0 if student1 is tied with student2 // a positive number if student1 goes after student2 } };
Comparator<String> myComparator = new Comparator<>(){ @Override public int compare(String a, String b){ return a.length - b.length;// returns an int < 0, if a < b, // or 0, if a = b, // int > 0, if a > b } }
public void sortByLastName(List people) { persons.add(one); Comparator comp = new PersonComparator(); Collections.sort(persons , comp); }
public class ItemComparator2 implements Comparator<itemDetails> { Comparator myComp = new ItemComparator(); @Override public int compare(itemDetails a, itemDetails b){ return myComp.compare(a.itemId, b.itemId); } } // ... Collections.sort(AssociatedItemCasuaList.get(LHS).getA(), new ItemComparator2());
new Comparator<File>() { private final Comparator<String> NATURAL_SORT = new WindowsExplorerComparator(); @Override public int compare(File o1, File o2) {; return NATURAL_SORT.compare(o1.getName(), o2.getName()); } }
Comparator myComparator = new myComparator(); List list = ...; List reverse = new ArrayList(list); //acceding Collections.sort(list, myComparator); //descending Collections.sort(list, new ReverseOrder(myComparator));
String[] points = {"100", "500", "200", "400", "300"}; String[] countries = {"nepal", "japan", "finland", "brazil", "spain"}; Comparator<String> comparator = new CustomArrayComparator<String, String>(points, countries); Arrays.sort(countries, comparator); Arrays.sort(points, Collections.reverseOrder()); System.out.println(Arrays.toString(points)); System.out.println(Arrays.toString(countries));
Comparator ageComparator = new PersonAgeComparator(); List<Person> personList = // populate list somehow Person fourYearOld = new Person(); fourYearOld.setAge(4); for (Person p : personList) { if (ageComparator.compare(fourYearOld, p) == 0) { System.out.println(p.getName() + " is 4 years old"); } }
// create different comparison mechanism Comparator<Event> comparator = new EventByRandomDefinitionComparator(); for (Event e : events) { if (!eventsByName.containsKey(e.getName())) { // create new set by name // changed Set implementation to use new comparator eventsByName.put(e.getName(), new TreeSet<Event>(comparator))); } // add event to existing Set. // duplicates will be dropped since it's a `Set` eventsByName.get(e.getName()).add(e); }