nullsFirst(java.util.Comparator)方法返回比較器,該比較器是對null友好的比較器,並認為null值小於非null。空值首先通過以下邏輯進行操作:
- null元素被認為小於non-null。
- 當兩個元素都為空時,則認為它們相等。
- 當兩個元素都不為空時,指定的Comparator確定順序。
- 如果指定的比較器為null,則返回的比較器將所有非null元素視為相等。
- 如果指定的比較器可序列化,則返回的比較器可序列化。
用法:
static <T> Comparator<T> nullsFirst(Comparator<? super T> comparator)
參數:此方法接受單個參數比較器,該比較器是用於比較非空值的比較器
返回值:此方法返回一個比較器,該比較器認為null小於非null,並將非null對象與提供的Comparator進行比較。
以下示例程序旨在說明nullsFirst(java.util.Comparator)方法:
示例1:
// Java program to demonstrate
// Comparator.nullsFirst(java.util.Comparator) method
import java.util.Arrays;
import java.util.Comparator;
public class GFG {
public static void main(String[] args)
{
// Create a collection of an array
// of names also contain nulls
String[] strings = { "aman", "suvam",
null, "sahil",
null };
// print the array
System.out.println("Before sorting: "
+ Arrays.toString(strings));
// apply nullsFirst method
// and sort the array
Arrays.sort(strings,
Comparator.nullsFirst(
Comparator.naturalOrder()));
// print the array
System.out.println("After sorting: "
+ Arrays.toString(strings));
}
}
IDE的控製台上打印的輸出如下所示。
輸出:
示例2:
// Java program to demonstrate
// Comparator.nullsFirst(java.util.Comparator) method
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class GFG {
public static void main(String[] args)
{
// Create some user objects
User u1 = new User("Aaman", 25);
User u2 = new User("Joyita", 22);
User u3 = new User("Suvam", 28);
User u4 = new User("mahafuj", 25);
System.out.println("One null Objects");
List<User> list
= Arrays.asList(u1, u2, u3, null, u4);
Collections.sort(list,
Comparator.nullsFirst(
Comparator.comparing(
User::getName)));
list.forEach(user -> System.out.println(user));
System.out.println("\nMore than One null Objects");
list = Arrays.asList(u1, u4, null, u2, u3, null, null);
Collections.sort(list,
Comparator.nullsFirst(
Comparator.comparing(
User::getName)));
list.forEach(user -> System.out.println(user));
}
}
class User implements Comparable<User> {
public String name;
public int age;
public User(String name, int age)
{
this.name = name;
this.age = age;
}
public int compareTo(User u1)
{
return name.compareTo(u1.name);
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public int getAge()
{
return age;
}
public void setAge(int age)
{
this.age = age;
}
@Override
public String toString()
{
return "User [name=" + name + ", age=" + age + "]";
}
}
控製台上打印的輸出如下所示。
輸出:
相關用法
- Java Collectors minBy(Comparator comparator)用法及代碼示例
- Java Collectors maxBy(Comparator comparator)用法及代碼示例
- Java Stream sorted(Comparator comparator)用法及代碼示例
- Java Comparator reverseOrder()用法及代碼示例
- Java Comparator reversed()用法及代碼示例
- Java Comparator thenComparingInt()用法及代碼示例
- Java Comparator comparingLong()用法及代碼示例
- Java TreeMap comparator()用法及代碼示例
- Java Comparator thenComparingLong()用法及代碼示例
- Java Comparator comparingDouble()用法及代碼示例
- Java Comparator thenComparingDouble()用法及代碼示例
- Java SortedMap comparator()用法及代碼示例
- Java Comparator naturalOrder()用法及代碼示例
- Java Comparator nullsLast()用法及代碼示例
- Java ConcurrentSkipListSet comparator()用法及代碼示例
注:本文由純淨天空篩選整理自AmanSingh2210大神的英文原創作品 Comparator nullsFirst() method in Java with examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。