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


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


Java中Comparator接口的thenComparingLong(java.util.function.ToLongFunction)方法将返回字典顺序比较器,该函数具有提取Long排序键的函数。

用法:

default Comparator <T> thenComparingLong(
    ToLongFunction <T> keyExtractor)

参数:此方法接受keyExtractor,该函数用于提取Long排序键。


返回值:此方法返回一个由该字典顺序排序的比较器,然后返回Long排序键。

异常:如果参数为null,则此方法将引发NullPointerException。

下面的程序演示了thenComparingLong(java.util.function.ToLongFunction)方法:
示例1:

// Java program to demonstrate Comparator 
// thenComparingLong(ToLongFunction) 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) 
    { 
        List<Users> list = createUsers(); 
        System.out.printf("before sort: %s%n", list); 
        Collections.sort(list, 
                         Comparator 
                             .comparing(Users::getDepartment) 
                             .thenComparingLong(Users::getId)); 
        System.out.printf("after sort: %s%n", list); 
    } 
  
    private static List<Users> createUsers() 
    { 
        return Arrays.asList( 
            new Users(12311, "ME"), 
            new Users(10211, "CSE"), 
            new Users(33111, "CSE"), 
            new Users(21000, "IT"), 
            new Users(12133, "IT"), 
            new Users(21445, "CSE")); 
    } 
  
    private static class Users { 
        private long id; 
        private String department; 
  
        public Users(long id, String department) 
        { 
            this.id = id; 
            this.department = department; 
        } 
  
        public long getId() 
        { 
            return id; 
        } 
  
        public void setId(long id) 
        { 
            this.id = id; 
        } 
  
        public String getDepartment() 
        { 
            return department; 
        } 
  
        public void setDepartment(String department) 
        { 
            this.department = department; 
        } 
  
        @Override
        public String toString() 
        { 
            return department + " - " + id; 
        } 
    } 
}

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

您可以在示例中看到,首先按部门进行排序,如果部门相同,则按ID进行排序。

示例2:

// Java program to demonstrate Comparator 
// thenComparingLong(ToLongFunction) method 
  
import java.util.Arrays; 
import java.util.Comparator; 
import java.util.List; 
  
public class GFG { 
    public static void main(String... args) 
    { 
  
        List<String> list 
            = Arrays.asList("KKR", "CSK", 
                            "MI", "KXIP", "RCB", 
                            "SRH", "DC", "RR"); 
  
        try { 
  
            // apply thenComparingLong 
            Comparator.comparing(list::get) 
                .thenComparingLong(null); 
        } 
        catch (Exception e) { 
  
            System.out.printf("Exception:" + e); 
        } 
    } 
}

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

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



相关用法


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