当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java Comparator nullsFirst()用法及代码示例


nullsFirst(java.util.Comparator)方法返回比较器,该比较器是对null友好的比较器,并认为null值小于非null。空值首先通过以下逻辑进行操作:

  1. null元素被认为小于non-null。
  2. 当两个元素都为空时,则认为它们相等。
  3. 当两个元素都不为空时,指定的Comparator确定顺序。
  4. 如果指定的比较器为null,则返回的比较器将所有非null元素视为相等。
  5. 如果指定的比较器可序列化,则返回的比较器可序列化。

用法:

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 + "]"; 
    } 
}

控制台上打印的输出如下所示。
输出:

参考文献: https://docs.oracle.com/javase/10/docs/api/java/util/Comparator.html#nullsFirst(java.util.Comparator)



相关用法


注:本文由纯净天空筛选整理自AmanSingh2210大神的英文原创作品 Comparator nullsFirst() method in Java with examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。