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


Java Comparator comparingLong()用法及代碼示例


Java中Comparator接口的compareLong(java.util.function.ToLongFunction)方法接受一個函數作為參數,該參數從類型T中提取一個長排序鍵,然後返回一個與該排序鍵進行比較的Comparator。指定的函數也可以序列化。

用法:

static <T> Comparator<T> 
    comparingLong(ToLongFunction <T> keyExtractor)

參數:此方法接受單個參數keyExtractor,該參數用於提取長排序鍵。


返回值:此方法返回一個比較器,該比較器通過提取的鍵進行比較

異常:如果參數為null,則此方法將引發NullPolongerException。

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

// Java program to demonstrate Comparator 
// comparingLong(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) 
    { 
  
        // create some user objects 
        User u1 = new User("Aaman", 25724474); 
        User u2 = new User("Joyita", 2262848); 
        User u3 = new User("Suvam", 2824444); 
        User u4 = new User("mahafuj", 2544244); 
  
        // before sort 
        List<User> list = Arrays.asList(u2, u1, u4, u3); 
        System.out.println("Before Sort:"); 
        list.forEach(user -> System.out.println(user)); 
  
        Collections.sort(list, 
                         Comparator.comparingLong( 
                             User::getregistrationId)); 
        System.out.println("\nAfterSort:"); 
        list.forEach(user -> System.out.println(user)); 
    } 
} 
  
class User implements Comparable<User> { 
    public String name; 
    public long registrationId; 
  
    public User(String name, long registrationId) 
    { 
        this.name = name; 
        this.registrationId = registrationId; 
    } 
  
    public int compareTo(User u1) 
    { 
        return name.compareTo(u1.name); 
    } 
  
    public String getName() 
    { 
        return name; 
    } 
  
    public void setName(String name) 
    { 
        this.name = name; 
    } 
  
    public long getregistrationId() 
    { 
        return registrationId; 
    } 
  
    public void setregistrationId(long registrationId) 
    { 
        this.registrationId = registrationId; 
    } 
  
    @Override
    public String toString() 
    { 
        return "User [name=" + name 
            + ", registrationId="
            + registrationId + "]"; 
    } 
}

IDE的控製台上打印的輸出如下所示。
輸出:

示例2:

// Java program to demonstrate Comparator 
// comparingLong(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) 
    { 
  
        // before sort 
        List<Order> list = Arrays.asList( 
            new Order("A382y482y48", 3204355), 
            new Order("Vvekhfbkje2", 2425355), 
            new Order("efkhfbekjfbe", 1345435), 
            new Order("bhdhdfaddvad", 2305535), 
            new Order("abkasbcjabjc", 1005454)); 
        System.out.println("Before Sort:"); 
        list.forEach(order -> System.out.println(order)); 
  
        Collections.sort(list, 
                         Comparator.comparingLong( 
                             Order::getValue)); 
        System.out.println("\nAfter Sort:"); 
        list.forEach(order -> System.out.println(order)); 
    } 
} 
  
class Order implements Comparable<Order> { 
    public String orderNo; 
    public long value; 
  
    public int compareTo(Order o1) 
    { 
        return orderNo.compareTo(o1.orderNo); 
    } 
  
    public Order(String orderNo, long 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 long getValue() 
    { 
        return value; 
    } 
  
    public void setValue(long value) 
    { 
        this.value = value; 
    } 
}

控製台上打印的輸出如下所示。
輸出:

參考文獻: https://docs.oracle.com/javase/10/docs/api/java/util/Comparator.html#comparingLong(java.util.function.ToLongFunction)



相關用法


注:本文由純淨天空篩選整理自AmanSingh2210大神的英文原創作品 Comparator comparingLong() method in Java with examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。