/* * Copyright (c) 2013, 2017, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions.
*/
privatefinal People people[] = { new People("John", "Doe", 34), new People("Mary", "Doe", 30), new People("Maria", "Doe", 14), new People("Jonah", "Doe", 10), new People("John", "Cook", 54), new People("Mary", "Cook", 50), new People("Mary", null, 25), new People("John", null, 27)
};
publicvoid 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"));
// 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");
}
publicvoid 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");
publicvoid testNaturalAndReverseIdentity() { var naturalOrder = Comparator.<String>naturalOrder(); var reverseOrder = Comparator.<String>reverseOrder();
assertEquals(
naturalOrder,
Collections.reverseOrder(reverseOrder), "Comparator.naturalOrder() and Collections.reverseOrder(Comparator.reverseOrder()) not equal");
assertEquals(
reverseOrder,
Collections.reverseOrder(naturalOrder), "Comparator.reverseOrder() and Collections.reverseOrder(Comparator.naturalOrder()) not equal");
assertEquals(
naturalOrder.reversed(),
reverseOrder, "Comparator.naturalOrder().reversed() amd Comparator.reverseOrder() not equal");
assertEquals(
reverseOrder.reversed(),
naturalOrder, "Comparator.reverseOrder().reversed() and Comparator.naturalOrder() not equal");
}
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.