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


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


Java中Comparator接口的thenComparingInt(java.util.function.ToIntFunction)方法将返回字典顺序比较器,并带有提取int排序键的函数。

用法:

default Comparator <T> 
    thenComparingInt(ToIntFunction <T> keyExtractor)

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


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

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

以下示例程序旨在说明thenComparingInt(java.util.function.ToIntFunction)方法:
示例1:

// Java program to demonstrate Comparator 
// thenComparingInt(ToIntFunction) 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<HardwareItems> list = getItems(); 
        System.out.println("before sort:"); 
        list.forEach(System.out::println); 
  
        // Apply sorting and 
        // also apply thenComparingInt() 
        Collections 
            .sort( 
                list, 
                Comparator 
                    .comparing(HardwareItems::getName) 
                    .thenComparingInt(HardwareItems::getPrice)); 
  
        System.out.println("after sort:"); 
        list.forEach(System.out::println); 
    } 
  
    private static List<HardwareItems> getItems() 
    { 
        return Arrays.asList( 
            new HardwareItems("Laptop", 40000), 
            new HardwareItems("Desktop", 20000), 
            new HardwareItems("Laptop", 45500), 
            new HardwareItems("Monitor", 10000), 
            new HardwareItems("Desktop", 22000)); 
    } 
  
    private static class HardwareItems { 
        private String name; 
        private int price; 
  
        public HardwareItems(String name, int price) 
        { 
            this.name = name; 
            this.price = price; 
        } 
  
        public String getName() 
        { 
            return name; 
        } 
  
        public int getPrice() 
        { 
            return price; 
        } 
  
        @Override
        public String toString() 
        { 
            return "HardwareItems [name="
                + name 
                + ", price="
                + price + "]"; 
        } 
    } 
}

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

您可以在示例中看到,首先按名称进行排序,如果名称相同,则按int类型的价格进行排序。

示例2:

// Java program to demonstrate Comparator 
// thenComparingInt(ToIntFunction)  method 
  
import java.util.Arrays; 
import java.util.Comparator; 
import java.util.List; 
  
public class GFG { 
  
    public static void main(String... args) 
    { 
  
        List<Double> list 
            = Arrays.asList(1.12, 2.4, 3.43, 
                            4.343, 5.434); 
  
        try { 
  
            // apply thenComparingInt 
            Comparator.comparing(list::get) 
                .thenComparingInt(null); 
        } 
        catch (Exception e) { 
  
            System.out.printf("Exception:" + e); 
        } 
    } 
}

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

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



相关用法


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