當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。