java 8中列表對象多條件排序

來源:互聯網
上載者:User

標籤:on()   method   code   csdn   inter   編程   tostring   tar   nbsp   

java 8 新加了 lambda 運算式,當介面是一個 @FunctionalInterface 時可以使用 lambda 運算式代替

 Function典型的應用情境為:A. 1個只有1個方法的介面,避免寫匿名類; B. 接受Fuction介面為參數的方法

 

1 匿名函式式編程特性

函數式介面:Functional Interface. 
定義的一個介面,介面裡面必須 有且只有一個抽象方法 ,這樣的介面就成為函數式介面。 
在可以使用lambda運算式的地方,方法聲明時必須包含一個函數式的介面。 

 函數式編程的格式如下幾種方式:
    a. 標準方式
    (Type1 param1, Type2 param2, ..., TypeN paramN) -> {
    statment1;
    statment2;
    //.............
    return statmentM;
    }

    b.省略類型
    (param1,param2, ..., paramN) -> {
    statment1;
    statment2;
    //.............
    return statmentM;
    }
    
    c.參數為1個時,可用省略參數小括弧
    param1 -> {
    statment1;
    statment2;
    //.............
    return statmentM;
    }
    
    d. 語句只有一條時,可省略語句大括弧
    param1 -> statment

 

2. 方法引用

如果我們想要調用的方法擁有一個名字,我們就可以通過它的名字直接調用它。 
Comparator byName = Comparator.comparing(Person::getName); 
方法引用的標準形式是:類名::方法名。(注意:只需要寫方法名,不需要寫括弧)
類型     樣本
引用靜態方法     ContainingClass::staticMethodName
引用某個對象的執行個體方法     containingObject::instanceMethodName
引用某個類型的任意對象的執行個體方法     ContainingType::methodName
引用構造方法     ClassName::new
    
靜態方法引用例子:

  String::valueOf   等價於lambda運算式 (s) -> String.valueOf(s)

  Math::pow       等價於lambda運算式  (x, y) -> Math.pow(x, y);    

 

3 Java SE 8中增加了一個新的包:java.util.function,它裡麵包含了常用的函數式介面,例如:

Predicate<T>——接收T對象並返回booleanConsumer<T>——接收T對象,不傳回值Function<T, R>——接收T對象,返回R對象Supplier<T>——提供T對象(例如工廠),不接收值UnaryOperator<T>——接收T對象,返回T對象BinaryOperator<T>——接收兩個T對象,返回T對象

那麼在參數為這些介面的地方,我們就可以直接使用lambda運算式了!

 

4 簡單例子

Person 類有 name、age

import org.apache.commons.lang3.builder.ToStringBuilder;import com.huitong.actdemo1.util.PinyinUtil;public class Person {        private String name;    private Integer age;        public Person() {    }        public Person(String name, Integer age) {        this.name = name;        this.age = age;    }    @Override    public String toString() {        return ToStringBuilder.reflectionToString(this);    }        public String getNamePinyin() {        return PinyinUtil.toPinyin(name);    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public Integer getAge() {        return age;    }    public void setAge(Integer age) {        this.age = age;    }}

 

現在要一個需求,一個List 先按照名字的拼音排序,然後按照年齡排序

public static void main(String[] args) {                List<Person> persons = getPersons();                long start = System.currentTimeMillis();//        List<Person> result = persons.stream().sorted(Comparator.comparing(Person::getNamePinyin).thenComparing(Person::getAge)).collect(Collectors.toList());        List<Person> result = persons.stream()                .sorted(Comparator.comparing((Person p) -> p.getNamePinyin())                        .thenComparing(Person::getAge)).collect(Collectors.toList());        long end = System.currentTimeMillis();                System.out.println("duration time:" + (end-start) + "ms");                for(Person p: result) {            System.out.println(p);        }    }        public static List<Person> getPersons() {        List<Person> persons = new ArrayList<>();        persons.add(new Person("中國", 24));        persons.add(new Person("中國", 23));        persons.add(new Person("中國", 78));        persons.add(new Person("美國", 23));        persons.add(new Person("泰國", 23));        persons.add(new Person("韓國", 23));        persons.add(new Person("日本", 23));                return persons;    }

 

兩種方法都可以。

Comparator.comparing()方法接受一個 Function 參數,可以使用lambda 運算式、也可以使用方法引用方式。

 

 

 

參考文獻:

78359045

50748202

 

java 8中列表對象多條件排序

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.