Java Code Examples for java.util.Comparator#thenComparing()
The following examples show how to use
java.util.Comparator#thenComparing() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CompositeOrderComparator.java From openAGV with Apache License 2.0 | 6 votes |
@Inject public CompositeOrderComparator(DefaultDispatcherConfiguration configuration, Map<String, Comparator<TransportOrder>> availableComparators) { // At the end, if all other comparators failed to see a difference, compare by age. // As the age of two distinct transport orders may still be the same, finally compare by name. // Add configured comparators before these two. Comparator<TransportOrder> composite = new TransportOrderComparatorByAge().thenComparing(new TransportOrderComparatorByName()); for (String priorityKey : Lists.reverse(configuration.orderPriorities())) { Comparator<TransportOrder> configuredComparator = availableComparators.get(priorityKey); checkArgument(configuredComparator != null, "Unknown order priority key: %s", priorityKey); composite = configuredComparator.thenComparing(composite); } this.compositeComparator = composite; }
Example 2
Source File: CompositeOrderCandidateComparator.java From openAGV with Apache License 2.0 | 6 votes |
@Inject public CompositeOrderCandidateComparator( DefaultDispatcherConfiguration configuration, Map<String, Comparator<AssignmentCandidate>> availableComparators) { // At the end, if all other comparators failed to see a difference, compare by age. // As the age of two distinct transport orders may still be the same, finally compare by name. // Add configured comparators before these two. Comparator<AssignmentCandidate> composite = new CandidateComparatorByOrderAge().thenComparing(new CandidateComparatorByOrderName()); for (String priorityKey : Lists.reverse(configuration.orderCandidatePriorities())) { Comparator<AssignmentCandidate> configuredComparator = availableComparators.get(priorityKey); checkArgument(configuredComparator != null, "Unknown order candidate priority key: %s", priorityKey); composite = configuredComparator.thenComparing(composite); } this.compositeComparator = composite; }
Example 3
Source File: CompositeVehicleComparator.java From openAGV with Apache License 2.0 | 6 votes |
@Inject public CompositeVehicleComparator(DefaultDispatcherConfiguration configuration, Map<String, Comparator<Vehicle>> availableComparators) { // At the end, if all other comparators failed to see a difference, compare by energy level. // As the energy level of two distinct vehicles may still be the same, finally compare by name. // Add configured comparators before these two. Comparator<Vehicle> composite = new VehicleComparatorByEnergyLevel().thenComparing(new VehicleComparatorByName()); for (String priorityKey : Lists.reverse(configuration.vehiclePriorities())) { Comparator<Vehicle> configuredComparator = availableComparators.get(priorityKey); checkArgument(configuredComparator != null, "Unknown vehicle priority key: %s", priorityKey); composite = configuredComparator.thenComparing(composite); } this.compositeComparator = composite; }
Example 4
Source File: ApiVersionComparator.java From intellij-kubernetes with Apache License 2.0 | 6 votes |
@Override public int compare(final String o1, final String o2) { final Matcher matcherOne = VERSION_FORMAT_REGEX.matcher(o1); final Matcher matcherTwo = VERSION_FORMAT_REGEX.matcher(o2); // Fall back to simple string comparision if either of the versions cannot be parsed if (!matcherOne.matches() || !matcherTwo.matches()) { return o1.compareTo(o2); } Comparator<Matcher> comparator = Comparator.comparing(m -> m.group("group"), Comparator.nullsFirst(Comparator.naturalOrder())); comparator = comparator.thenComparingInt(m -> Integer.parseInt(m.group("major"))); comparator = comparator.thenComparing(m -> m.group("minor"), Comparator.nullsLast(Comparator.naturalOrder())); return comparator.compare(matcherOne, matcherTwo); }
Example 5
Source File: BasicTest.java From dragonwell8_jdk with GNU General Public License v2.0 | 5 votes |
public void testComposeComparator() { // Longer string in front Comparator<String> first = (s1, s2) -> s2.length() - s1.length(); Comparator<String> second = Comparator.naturalOrder(); Comparator<String> composed = first.thenComparing(second); assertTrue(composed.compare("abcdefg", "abcdef") < 0); assertTrue(composed.compare("abcdef", "abcdefg") > 0); assertTrue(composed.compare("abcdef", "abcdef") == 0); assertTrue(composed.compare("abcdef", "ghijkl") < 0); assertTrue(composed.compare("ghijkl", "abcdefg") > 0); }
Example 6
Source File: EntryComparators.java From openjdk-8-source with GNU General Public License v2.0 | 5 votes |
public void testKVComparators() { // Comparator<People> cmp = Comparator.naturalOrder(); // Should fail to compiler as People is not comparable // We can use simple comparator, but those have been tested above. // Thus choose to do compose for some level of interation. Comparator<People> cmp1 = Comparator.comparing(People::getFirstName); Comparator<People> cmp2 = Comparator.comparing(People::getLastName); Comparator<People> cmp = cmp1.thenComparing(cmp2); assertPairComparison(people[0], people[0], people[1], people[1], Map.Entry.<People, People>comparingByKey(cmp), Map.Entry.<People, People>comparingByValue(cmp)); }
Example 7
Source File: BasicTest.java From jdk8u_jdk with GNU General Public License v2.0 | 5 votes |
public void testNullsFirst() { Comparator<String> strcmp = Comparator.nullsFirst(Comparator.naturalOrder()); Comparator<People> cmp = Comparator.comparing(People::getLastName, strcmp) .thenComparing(People::getFirstName, strcmp); // Mary.null vs Mary.Cook - solve by last name assertComparison(cmp, people[6], people[5]); // John.null vs Mary.null - solve by first name assertComparison(cmp, people[7], people[6]); // More than one thenComparing strcmp = Comparator.nullsFirst(Comparator.comparingInt(String::length) .thenComparing(String.CASE_INSENSITIVE_ORDER)); assertComparison(strcmp, null, "abc"); assertComparison(strcmp, "ab", "abc"); assertComparison(strcmp, "abc", "def"); assertEquals(0, strcmp.compare("abc", "ABC")); // Ensure reverse still handle null properly Comparator<String> strcmp2 = strcmp.reversed().thenComparing(Comparator.naturalOrder()); assertComparison(strcmp2, "abc", null); assertComparison(strcmp2, "abc", "ab"); assertComparison(strcmp2, "def", "abc"); assertComparison(strcmp2, "ABC", "abc"); // Considering non-null values to be equal Comparator<String> blind = Comparator.nullsFirst(null); assertComparison(blind, null, "abc"); assertEquals(0, blind.compare("abc", "def")); // reverse still consider non-null values to be equal strcmp = blind.reversed(); assertComparison(strcmp, "abc", null); assertEquals(0, strcmp.compare("abc", "def")); // chain with another comparator to compare non-nulls strcmp = blind.thenComparing(Comparator.naturalOrder()); assertComparison(strcmp, null, "abc"); assertComparison(strcmp, "abc", "def"); }
Example 8
Source File: BasicTest.java From jdk8u60 with GNU General Public License v2.0 | 5 votes |
public void testComposeComparator() { // Longer string in front Comparator<String> first = (s1, s2) -> s2.length() - s1.length(); Comparator<String> second = Comparator.naturalOrder(); Comparator<String> composed = first.thenComparing(second); assertTrue(composed.compare("abcdefg", "abcdef") < 0); assertTrue(composed.compare("abcdef", "abcdefg") > 0); assertTrue(composed.compare("abcdef", "abcdef") == 0); assertTrue(composed.compare("abcdef", "ghijkl") < 0); assertTrue(composed.compare("ghijkl", "abcdefg") > 0); }
Example 9
Source File: BasicTest.java From hottub with GNU General Public License v2.0 | 5 votes |
public void testComposeComparator() { // Longer string in front Comparator<String> first = (s1, s2) -> s2.length() - s1.length(); Comparator<String> second = Comparator.naturalOrder(); Comparator<String> composed = first.thenComparing(second); assertTrue(composed.compare("abcdefg", "abcdef") < 0); assertTrue(composed.compare("abcdef", "abcdefg") > 0); assertTrue(composed.compare("abcdef", "abcdef") == 0); assertTrue(composed.compare("abcdef", "ghijkl") < 0); assertTrue(composed.compare("ghijkl", "abcdefg") > 0); }
Example 10
Source File: BasicTest.java From openjdk-jdk8u-backup with GNU General Public License v2.0 | 5 votes |
public void testComposeComparator() { // Longer string in front Comparator<String> first = (s1, s2) -> s2.length() - s1.length(); Comparator<String> second = Comparator.naturalOrder(); Comparator<String> composed = first.thenComparing(second); assertTrue(composed.compare("abcdefg", "abcdef") < 0); assertTrue(composed.compare("abcdef", "abcdefg") > 0); assertTrue(composed.compare("abcdef", "abcdef") == 0); assertTrue(composed.compare("abcdef", "ghijkl") < 0); assertTrue(composed.compare("ghijkl", "abcdefg") > 0); }
Example 11
Source File: EntryComparators.java From openjdk-jdk9 with GNU General Public License v2.0 | 5 votes |
public void testKVComparators() { // Comparator<People> cmp = Comparator.naturalOrder(); // Should fail to compiler as People is not comparable // We can use simple comparator, but those have been tested above. // Thus choose to do compose for some level of interation. Comparator<People> cmp1 = Comparator.comparing(People::getFirstName); Comparator<People> cmp2 = Comparator.comparing(People::getLastName); Comparator<People> cmp = cmp1.thenComparing(cmp2); assertPairComparison(people[0], people[0], people[1], people[1], Map.Entry.<People, People>comparingByKey(cmp), Map.Entry.<People, People>comparingByValue(cmp)); }
Example 12
Source File: EntryComparators.java From openjdk-jdk8u with GNU General Public License v2.0 | 5 votes |
public void testKVComparators() { // Comparator<People> cmp = Comparator.naturalOrder(); // Should fail to compiler as People is not comparable // We can use simple comparator, but those have been tested above. // Thus choose to do compose for some level of interation. Comparator<People> cmp1 = Comparator.comparing(People::getFirstName); Comparator<People> cmp2 = Comparator.comparing(People::getLastName); Comparator<People> cmp = cmp1.thenComparing(cmp2); assertPairComparison(people[0], people[0], people[1], people[1], Map.Entry.<People, People>comparingByKey(cmp), Map.Entry.<People, People>comparingByValue(cmp)); }
Example 13
Source File: BasicTest.java From openjdk-jdk8u with GNU General Public License v2.0 | 5 votes |
public void testNullsFirst() { Comparator<String> strcmp = Comparator.nullsFirst(Comparator.naturalOrder()); Comparator<People> cmp = Comparator.comparing(People::getLastName, strcmp) .thenComparing(People::getFirstName, strcmp); // Mary.null vs Mary.Cook - solve by last name assertComparison(cmp, people[6], people[5]); // John.null vs Mary.null - solve by first name assertComparison(cmp, people[7], people[6]); // More than one thenComparing strcmp = Comparator.nullsFirst(Comparator.comparingInt(String::length) .thenComparing(String.CASE_INSENSITIVE_ORDER)); assertComparison(strcmp, null, "abc"); assertComparison(strcmp, "ab", "abc"); assertComparison(strcmp, "abc", "def"); assertEquals(0, strcmp.compare("abc", "ABC")); // Ensure reverse still handle null properly Comparator<String> strcmp2 = strcmp.reversed().thenComparing(Comparator.naturalOrder()); assertComparison(strcmp2, "abc", null); assertComparison(strcmp2, "abc", "ab"); assertComparison(strcmp2, "def", "abc"); assertComparison(strcmp2, "ABC", "abc"); // Considering non-null values to be equal Comparator<String> blind = Comparator.nullsFirst(null); assertComparison(blind, null, "abc"); assertEquals(0, blind.compare("abc", "def")); // reverse still consider non-null values to be equal strcmp = blind.reversed(); assertComparison(strcmp, "abc", null); assertEquals(0, strcmp.compare("abc", "def")); // chain with another comparator to compare non-nulls strcmp = blind.thenComparing(Comparator.naturalOrder()); assertComparison(strcmp, null, "abc"); assertComparison(strcmp, "abc", "def"); }
Example 14
Source File: BasicTest.java From jdk8u-jdk with GNU General Public License v2.0 | 5 votes |
public void testNullsLast() { Comparator<String> strcmp = Comparator.nullsLast(Comparator.naturalOrder()); Comparator<People> cmp = Comparator.comparing(People::getLastName, strcmp) .thenComparing(People::getFirstName, strcmp); // Mary.null vs Mary.Cook - solve by last name assertComparison(cmp, people[5], people[6]); // John.null vs Mary.null - solve by first name assertComparison(cmp, people[7], people[6]); // More than one thenComparing strcmp = Comparator.nullsLast(Comparator.comparingInt(String::length) .thenComparing(String.CASE_INSENSITIVE_ORDER)); assertComparison(strcmp, "abc", null); assertComparison(strcmp, "ab", "abc"); assertComparison(strcmp, "abc", "def"); // Ensure reverse still handle null properly Comparator<String> strcmp2 = strcmp.reversed().thenComparing(Comparator.naturalOrder()); assertComparison(strcmp2, null, "abc"); assertComparison(strcmp2, "abc", "ab"); assertComparison(strcmp2, "def", "abc"); assertComparison(strcmp2, "ABC", "abc"); // Considering non-null values to be equal Comparator<String> blind = Comparator.nullsLast(null); assertComparison(blind, "abc", null); assertEquals(0, blind.compare("abc", "def")); // reverse still consider non-null values to be equal strcmp = blind.reversed(); assertComparison(strcmp, null, "abc"); assertEquals(0, strcmp.compare("abc", "def")); // chain with another comparator to compare non-nulls strcmp = blind.thenComparing(Comparator.naturalOrder()); assertComparison(strcmp, "abc", null); assertComparison(strcmp, "abc", "def"); }
Example 15
Source File: BasicTest.java From jdk8u-dev-jdk with GNU General Public License v2.0 | 5 votes |
public void testNullsLast() { Comparator<String> strcmp = Comparator.nullsLast(Comparator.naturalOrder()); Comparator<People> cmp = Comparator.comparing(People::getLastName, strcmp) .thenComparing(People::getFirstName, strcmp); // Mary.null vs Mary.Cook - solve by last name assertComparison(cmp, people[5], people[6]); // John.null vs Mary.null - solve by first name assertComparison(cmp, people[7], people[6]); // More than one thenComparing strcmp = Comparator.nullsLast(Comparator.comparingInt(String::length) .thenComparing(String.CASE_INSENSITIVE_ORDER)); assertComparison(strcmp, "abc", null); assertComparison(strcmp, "ab", "abc"); assertComparison(strcmp, "abc", "def"); // Ensure reverse still handle null properly Comparator<String> strcmp2 = strcmp.reversed().thenComparing(Comparator.naturalOrder()); assertComparison(strcmp2, null, "abc"); assertComparison(strcmp2, "abc", "ab"); assertComparison(strcmp2, "def", "abc"); assertComparison(strcmp2, "ABC", "abc"); // Considering non-null values to be equal Comparator<String> blind = Comparator.nullsLast(null); assertComparison(blind, "abc", null); assertEquals(0, blind.compare("abc", "def")); // reverse still consider non-null values to be equal strcmp = blind.reversed(); assertComparison(strcmp, null, "abc"); assertEquals(0, strcmp.compare("abc", "def")); // chain with another comparator to compare non-nulls strcmp = blind.thenComparing(Comparator.naturalOrder()); assertComparison(strcmp, "abc", null); assertComparison(strcmp, "abc", "def"); }
Example 16
Source File: EntryComparators.java From openjdk-jdk8u-backup with GNU General Public License v2.0 | 5 votes |
public void testKVComparators() { // Comparator<People> cmp = Comparator.naturalOrder(); // Should fail to compiler as People is not comparable // We can use simple comparator, but those have been tested above. // Thus choose to do compose for some level of interation. Comparator<People> cmp1 = Comparator.comparing(People::getFirstName); Comparator<People> cmp2 = Comparator.comparing(People::getLastName); Comparator<People> cmp = cmp1.thenComparing(cmp2); assertPairComparison(people[0], people[0], people[1], people[1], Map.Entry.<People, People>comparingByKey(cmp), Map.Entry.<People, People>comparingByValue(cmp)); }
Example 17
Source File: BasicTest.java From openjdk-8-source with GNU General Public License v2.0 | 5 votes |
public void testComposeComparator() { // Longer string in front Comparator<String> first = (s1, s2) -> s2.length() - s1.length(); Comparator<String> second = Comparator.naturalOrder(); Comparator<String> composed = first.thenComparing(second); assertTrue(composed.compare("abcdefg", "abcdef") < 0); assertTrue(composed.compare("abcdef", "abcdefg") > 0); assertTrue(composed.compare("abcdef", "abcdef") == 0); assertTrue(composed.compare("abcdef", "ghijkl") < 0); assertTrue(composed.compare("ghijkl", "abcdefg") > 0); }
Example 18
Source File: EntryComparators.java From openjdk-jdk8u-backup with GNU General Public License v2.0 | 4 votes |
private <K, V> void assertPairComparison(K k1, V v1, K k2, V v2, Comparator<Map.Entry<K, V>> ck, Comparator<Map.Entry<K, V>> cv) { final Map.Entry<K, V> p11 = new AbstractMap.SimpleImmutableEntry<>(k1, v1); final Map.Entry<K, V> p12 = new AbstractMap.SimpleImmutableEntry<>(k1, v2); final Map.Entry<K, V> p21 = new AbstractMap.SimpleImmutableEntry<>(k2, v1); final Map.Entry<K, V> p22 = new AbstractMap.SimpleImmutableEntry<>(k2, v2); assertTrue(ck.compare(p11, p11) == 0); assertTrue(ck.compare(p12, p11) == 0); assertTrue(ck.compare(p11, p12) == 0); assertTrue(ck.compare(p12, p22) < 0); assertTrue(ck.compare(p12, p21) < 0); assertTrue(ck.compare(p21, p11) > 0); assertTrue(ck.compare(p21, p12) > 0); assertTrue(cv.compare(p11, p11) == 0); assertTrue(cv.compare(p12, p11) > 0); assertTrue(cv.compare(p11, p12) < 0); assertTrue(cv.compare(p12, p22) == 0); assertTrue(cv.compare(p12, p21) > 0); assertTrue(cv.compare(p21, p11) == 0); assertTrue(cv.compare(p21, p12) < 0); Comparator<Map.Entry<K, V>> cmp = ck.thenComparing(cv); assertTrue(cmp.compare(p11, p11) == 0); assertTrue(cmp.compare(p12, p11) > 0); assertTrue(cmp.compare(p11, p12) < 0); assertTrue(cmp.compare(p12, p22) < 0); assertTrue(cmp.compare(p12, p21) < 0); assertTrue(cmp.compare(p21, p11) > 0); assertTrue(cmp.compare(p21, p12) > 0); cmp = cv.thenComparing(ck); assertTrue(cmp.compare(p11, p11) == 0); assertTrue(cmp.compare(p12, p11) > 0); assertTrue(cmp.compare(p11, p12) < 0); assertTrue(cmp.compare(p12, p22) < 0); assertTrue(cmp.compare(p12, p21) > 0); assertTrue(cmp.compare(p21, p11) > 0); assertTrue(cmp.compare(p21, p12) < 0); }
Example 19
Source File: EntryComparators.java From hottub with GNU General Public License v2.0 | 4 votes |
private <K, V> void assertPairComparison(K k1, V v1, K k2, V v2, Comparator<Map.Entry<K, V>> ck, Comparator<Map.Entry<K, V>> cv) { final Map.Entry<K, V> p11 = new AbstractMap.SimpleImmutableEntry<>(k1, v1); final Map.Entry<K, V> p12 = new AbstractMap.SimpleImmutableEntry<>(k1, v2); final Map.Entry<K, V> p21 = new AbstractMap.SimpleImmutableEntry<>(k2, v1); final Map.Entry<K, V> p22 = new AbstractMap.SimpleImmutableEntry<>(k2, v2); assertTrue(ck.compare(p11, p11) == 0); assertTrue(ck.compare(p12, p11) == 0); assertTrue(ck.compare(p11, p12) == 0); assertTrue(ck.compare(p12, p22) < 0); assertTrue(ck.compare(p12, p21) < 0); assertTrue(ck.compare(p21, p11) > 0); assertTrue(ck.compare(p21, p12) > 0); assertTrue(cv.compare(p11, p11) == 0); assertTrue(cv.compare(p12, p11) > 0); assertTrue(cv.compare(p11, p12) < 0); assertTrue(cv.compare(p12, p22) == 0); assertTrue(cv.compare(p12, p21) > 0); assertTrue(cv.compare(p21, p11) == 0); assertTrue(cv.compare(p21, p12) < 0); Comparator<Map.Entry<K, V>> cmp = ck.thenComparing(cv); assertTrue(cmp.compare(p11, p11) == 0); assertTrue(cmp.compare(p12, p11) > 0); assertTrue(cmp.compare(p11, p12) < 0); assertTrue(cmp.compare(p12, p22) < 0); assertTrue(cmp.compare(p12, p21) < 0); assertTrue(cmp.compare(p21, p11) > 0); assertTrue(cmp.compare(p21, p12) > 0); cmp = cv.thenComparing(ck); assertTrue(cmp.compare(p11, p11) == 0); assertTrue(cmp.compare(p12, p11) > 0); assertTrue(cmp.compare(p11, p12) < 0); assertTrue(cmp.compare(p12, p22) < 0); assertTrue(cmp.compare(p12, p21) > 0); assertTrue(cmp.compare(p21, p11) > 0); assertTrue(cmp.compare(p21, p12) < 0); }
Example 20
Source File: EntryComparators.java From jdk8u60 with GNU General Public License v2.0 | 4 votes |
private <K, V> void assertPairComparison(K k1, V v1, K k2, V v2, Comparator<Map.Entry<K, V>> ck, Comparator<Map.Entry<K, V>> cv) { final Map.Entry<K, V> p11 = new AbstractMap.SimpleImmutableEntry<>(k1, v1); final Map.Entry<K, V> p12 = new AbstractMap.SimpleImmutableEntry<>(k1, v2); final Map.Entry<K, V> p21 = new AbstractMap.SimpleImmutableEntry<>(k2, v1); final Map.Entry<K, V> p22 = new AbstractMap.SimpleImmutableEntry<>(k2, v2); assertTrue(ck.compare(p11, p11) == 0); assertTrue(ck.compare(p12, p11) == 0); assertTrue(ck.compare(p11, p12) == 0); assertTrue(ck.compare(p12, p22) < 0); assertTrue(ck.compare(p12, p21) < 0); assertTrue(ck.compare(p21, p11) > 0); assertTrue(ck.compare(p21, p12) > 0); assertTrue(cv.compare(p11, p11) == 0); assertTrue(cv.compare(p12, p11) > 0); assertTrue(cv.compare(p11, p12) < 0); assertTrue(cv.compare(p12, p22) == 0); assertTrue(cv.compare(p12, p21) > 0); assertTrue(cv.compare(p21, p11) == 0); assertTrue(cv.compare(p21, p12) < 0); Comparator<Map.Entry<K, V>> cmp = ck.thenComparing(cv); assertTrue(cmp.compare(p11, p11) == 0); assertTrue(cmp.compare(p12, p11) > 0); assertTrue(cmp.compare(p11, p12) < 0); assertTrue(cmp.compare(p12, p22) < 0); assertTrue(cmp.compare(p12, p21) < 0); assertTrue(cmp.compare(p21, p11) > 0); assertTrue(cmp.compare(p21, p12) > 0); cmp = cv.thenComparing(ck); assertTrue(cmp.compare(p11, p11) == 0); assertTrue(cmp.compare(p12, p11) > 0); assertTrue(cmp.compare(p11, p12) < 0); assertTrue(cmp.compare(p12, p22) < 0); assertTrue(cmp.compare(p12, p21) > 0); assertTrue(cmp.compare(p21, p11) > 0); assertTrue(cmp.compare(p21, p12) < 0); }