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


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


compareInt(java.util.function.ToIntFunction)方法接受一个函数作为参数,从类型T中提取一个int排序键,并返回一个与该排序键进行比较的Comparator。返回的比较器可以序列化(如果指定的函数也是可序列化。

用法:

static <T> Comparator<T> comparingInt(ToIntFunction <T> keyExtractor)

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


返回值:此方法返回一个比较器,该比较器通过提取的键进行比较

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

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

// Java program to demonstrate 
// Comparator.comparingInt(java.util.function.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) 
    { 
  
        // 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); 
  
        // before sort 
        List<User> list 
            = Arrays.asList(u2, u1, u4, u3); 
        System.out.println("Before Sort:"); 
        list.forEach(User 
                     -> System.out.println("User age "
                                           + User.getAge())); 
  
        Collections.sort(list, 
                         Comparator.comparingInt( 
                             User::getAge)); 
        System.out.println("\nAfterSort:"); 
        list.forEach(User 
                     -> System.out.println("User age "
                                           + User.getAge())); 
    } 
} 
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 + "]"; 
    } 
}

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

示例2:

// Java program to demonstrate 
// Comparator.comparingInt(java.util.function.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) 
    { 
  
        // before sort 
        List<Order> list = Arrays.asList( 
            new Order("A382y482y48", 320), 
            new Order("Vvekhfbkje2", 242), 
            new Order("efkhfbekjfbe", 1345), 
            new Order("bhdhdfaddvad", 230), 
            new Order("abkasbcjabjc", 100)); 
        System.out.println("Before Sort:"); 
        list.forEach(order -> System.out.println(order)); 
  
        Collections.sort(list, 
                         Comparator.comparingInt( 
                             Order::getValue)); 
        System.out.println("\nAfter Sort:"); 
        list.forEach(order -> System.out.println(order)); 
    } 
} 
class Order implements Comparable<Order> { 
    public String orderNo; 
    public int value; 
  
    public int compareTo(Order o1) 
    { 
        return orderNo.compareTo(o1.orderNo); 
    } 
  
    public Order(String orderNo, int value) 
    { 
        super(); 
        this.orderNo = orderNo; 
        this.value = value; 
    } 
  
    @Override
    public String toString() 
    { 
        return "Order [orderNo=" + orderNo 
            + ", value=" + value + "]"; 
    } 
  
    public String getOrderNo() 
    { 
        return orderNo; 
    } 
  
    public void setOrderNo(String orderNo) 
    { 
        this.orderNo = orderNo; 
    } 
  
    public int getValue() 
    { 
        return value; 
    } 
  
    public void setValue(int value) 
    { 
        this.value = value; 
    } 
}

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

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



相关用法


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