Java Code Examples for java.util.Comparator#nullsLast()
The following examples show how to use
java.util.Comparator#nullsLast() .
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: BasicTest.java From TencentKona-8 with GNU General Public License v2.0 | 6 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 2
Source File: AbstractEndTurnDelegate.java From triplea with GNU General Public License v3.0 | 6 votes |
@VisibleForTesting static Comparator<Territory> getSingleBlockadeThenHighestToLowestBlockadeDamage( final Map<Territory, Tuple<Integer, List<Territory>>> damagePerBlockadeZone) { return Comparator.nullsLast( (t1, t2) -> { if (Objects.equals(t1, t2)) { return 0; } final Tuple<Integer, List<Territory>> tuple1 = damagePerBlockadeZone.get(t1); final Tuple<Integer, List<Territory>> tuple2 = damagePerBlockadeZone.get(t2); final int num1 = tuple1.getSecond().size(); final int num2 = tuple2.getSecond().size(); if (num1 == 1 && num2 != 1) { return -1; } if (num2 == 1 && num1 != 1) { return 1; } final int d1 = tuple1.getFirst(); final int d2 = tuple2.getFirst(); return Integer.compare(d2, d1); }); }
Example 3
Source File: ExpressionFormatter.java From crate with Apache License 2.0 | 6 votes |
@Override public String visitObjectLiteral(ObjectLiteral node, @Nullable List<Expression> parameters) { StringBuilder builder = new StringBuilder("{"); boolean first = true; TreeMap<String, Expression> sorted = new TreeMap<>(Comparator.nullsLast(Comparator.naturalOrder())); sorted.putAll(node.values()); for (Map.Entry<String, Expression> entry : sorted.entrySet()) { if (!first) { builder.append(", "); } else { first = false; } builder.append(formatIdentifier(entry.getKey())) .append("= ") .append(entry.getValue().accept(this, parameters)); } return builder.append("}").toString(); }
Example 4
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 5
Source File: BasicTest.java From dragonwell8_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 6
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 7
Source File: Java8ComparatorUnitTest.java From tutorials with MIT License | 5 votes |
@Test public void whenNullsLast_thenSortedByNameWithNullsLast() { Comparator<Employee> employeeNameComparator = Comparator.comparing(Employee::getName); Comparator<Employee> employeeNameComparator_nullLast = Comparator.nullsLast(employeeNameComparator); Arrays.sort(employeesArrayWithNulls, employeeNameComparator_nullLast); // System.out.println(Arrays.toString(employeesArrayWithNulls)); assertTrue(Arrays.equals(employeesArrayWithNulls, sortedEmployeesArray_WithNullsLast)); }
Example 8
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 9
Source File: BasicTest.java From openjdk-8-source 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 10
Source File: BasicTest.java From hottub 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 11
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 12
Source File: BasicTest.java From openjdk-jdk9 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 13
Source File: BasicTest.java From openjdk-jdk8u-backup 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 14
Source File: JoiningIteratorTest.java From stream-utils with Apache License 2.0 | 5 votes |
@Test public void testJoinWithErrorInJoinFunction() throws Exception { Stream<SideItem> stream1 = Stream.of( new SideItem(1, "1l"), new SideItem(2, "2l"), new SideItem(3, "3l"), new SideItem(4, "4l"), new SideItem(5, "5l"), new SideItem(6, "6l") ); Stream<SideItem> stream2 = Stream.of( new SideItem(1, "1r"), new SideItem(2, "2r"), new SideItem(3, "3r"), new SideItem(4, "4r"), new SideItem(5, "5r"), new SideItem(6, "6r") ); final Iterator<String> joined = new JoiningIterator<>( stream1, stream2, Comparator.nullsLast(Comparator.naturalOrder()), KEYING_FUNCTION, KEYING_FUNCTION, (leftSide, rightSide) -> { if (leftSide != null && leftSide.getNumber() == 3) { throw new IllegalStateException("Holy exception batman!"); } return JOIN_FUNCTION.apply(leftSide, rightSide); }, JoinType.LEFT); assertEquals(joined.next(), "1 - 1l - 1 - 1r"); assertEquals(joined.next(), "2 - 2l - 2 - 2r"); // this next call should throw thrown.expect(RuntimeException.class); joined.next(); }
Example 15
Source File: JoiningIteratorTest.java From stream-utils with Apache License 2.0 | 5 votes |
@Test public void testJoinWithError() { Stream<SideItem> stream1 = Stream.of( new SideItem(1, "1l"), new SideItem(2, "2l"), new SideItem(3, "3l"), new SideItem(4, "4l"), new SideItem(5, "5l"), new SideItem(6, "6l") ); Iterator<SideItem> iterator = mock(Iterator.class); when(iterator.hasNext()).thenReturn(true); when(iterator.next()) .thenReturn(new SideItem(1, "1r")) .thenThrow(new IllegalStateException("Holy exception batman!")) .thenReturn(new SideItem(5, "5r")); final Iterator<String> joined = new JoiningIterator<>( stream1, StreamSupport.stream(Spliterators.spliteratorUnknownSize(iterator, 0), false), Comparator.nullsLast(Comparator.naturalOrder()), KEYING_FUNCTION, KEYING_FUNCTION, JOIN_FUNCTION, JoinType.LEFT); assertEquals(joined.next(), "1 - 1l - 1 - 1r"); // this next call should throw thrown.expect(RuntimeException.class); joined.next(); }
Example 16
Source File: JoiningIteratorTest.java From stream-utils with Apache License 2.0 | 5 votes |
@Test public void testJoinNextWithEmptyIteratorThrowsException() { final Iterator<String> iterator = new JoiningIterator<>( Stream.empty(), Stream.empty(), Comparator.nullsLast(Comparator.naturalOrder()), KEYING_FUNCTION, KEYING_FUNCTION, JOIN_FUNCTION, JoinType.LEFT); thrown.expect(NoSuchElementException.class); iterator.next(); }
Example 17
Source File: BasicTest.java From openjdk-jdk8u 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 18
Source File: BasicTest.java From jdk8u60 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 19
Source File: ComparatorTest.java From j2objc with Apache License 2.0 | 4 votes |
public void testNullsLast() { Comparator<Item> comparator = Comparator.nullsLast(Comparator.naturalOrder()); checkComparison(comparator, nullsLastItems); }
Example 20
Source File: MCRProcessableFactory.java From mycore with GNU General Public License v3.0 | 2 votes |
/** * Creates new PriorityBlockingQueue for runnables. Uses the {@link MCRRunnableComperator} * for comparision. * * @return a new priority blocking queue */ public static PriorityBlockingQueue<Runnable> newPriorityBlockingQueue() { int initialCapacity = 11; //taken from java.util.concurrent.PriorityBlockingQueue.DEFAULT_INITIAL_CAPACITY return new PriorityBlockingQueue<>(initialCapacity, Comparator.nullsLast(new MCRRunnableComperator())); }