list 集合排序,java8新特性,學習一下

最近做一個項目,遇到了list<Entity>需要按時間倒敘排列發現Java8新特性封裝的比較好:

例子:

List<Entity> list = Lists.newArrayList();

list.stream().sorted(Comparator.comparing(Entity::getCreateTime).reversed()).collect(Collectors.toList());
看到一篇博客總結的比較好(拷貝一下,留作紀念):
原文鏈接:http://www.concretepage.com/java/jdk-8/java-8-stream-sorted-example 
國外對Java8一系列總結的不錯, 翻譯過來給大家共享 
這篇文章將會講解Java 8 Stream sorted()示例, 我們能夠以自然序或着用Comparator接口定義的排序規則來排序一個流。
Comparator 能用用lambada表達式來初始化, 我們還能夠逆序一個已經排序的流。 
接下來我們將會使用java 8 的流式   sorted    排序     ListMap、 Set
 1、sorted() 默認使用自然序排序, 其中的元素必須實現Comparable  接口 
 2、sorted(Comparator<? super T> comparator)
我們可以使用lambada 來創建一個Comparator實例。可以按照升序或着降序來排序元素。 
下面代碼以自然序排序一個list
list.stream().sorted() 

自然序逆序元素,使用Comparator提供的reverseOrder()方法

list.stream().sorted(Comparator.reverseOrder()) 

使用Comparator來排序一個list

list.stream().sorted(Comparator.comparing(Student::getAge)) 

把上面的元素逆序

list.stream().sorted(Comparator.comparing(Student::getAge).reversed()) 

Stream sorted() with List

我們排序一組裝着Student類對象的List集合。 首先我們使用自然序, 接着我們使用Comparator分別進行升序和降序: 
SortList.java

package com.concretepage;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
public class SortList {
    public static void main(String[] args) {
        List<Student> list = new ArrayList<Student>();
        list.add(new Student(1, "Mahesh", 12));
        list.add(new Student(2, "Suresh", 15));
        list.add(new Student(3, "Nilesh", 10));

        System.out.println("---Natural Sorting by Name---");
        List<Student> slist = list.stream().sorted().collect(Collectors.toList());
        slist.forEach(e -> System.out.println("Id:"+ e.getId()+", Name: "+e.getName()+", Age:"+e.getAge()));

        System.out.println("---Natural Sorting by Name in reverse order---");
        slist = list.stream().sorted(Comparator.reverseOrder()).collect(Collectors.toList());
        slist.forEach(e -> System.out.println("Id:"+ e.getId()+", Name: "+e.getName()+", Age:"+e.getAge()));        

        System.out.println("---Sorting using Comparator by Age---");
        slist = list.stream().sorted(Comparator.comparing(Student::getAge)).collect(Collectors.toList());
        slist.forEach(e -> System.out.println("Id:"+ e.getId()+", Name: "+e.getName()+", Age:"+e.getAge()));

        System.out.println("---Sorting using Comparator by Age with reverse order---");
        slist = list.stream().sorted(Comparator.comparing(Student::getAge).reversed()).collect(Collectors.toList());
        slist.forEach(e -> System.out.println("Id:"+ e.getId()+", Name: "+e.getName()+", Age:"+e.getAge()));
    }
} 

* Student.java *

package com.concretepage;
public class Student implements Comparable<Student> {
    private int id;
    private String name;
    private int age;
    public Student(int id, String name, int age) {
        this.id = id;
        this.name = name;
        this.age = age;
    }
    public int getId() {
        return id;
    }
    public String getName() {
        return name;
    }
    public int getAge() {
        return age;
    }
    @Override
    public int compareTo(Student ob) {
        return name.compareTo(ob.getName());
    }
        @Override
        public boolean equals(final Object obj) {
          if (obj == null) {
             return false;
          }
          final Student std = (Student) obj;
          if (this == std) {
             return true;
          } else {
             return (this.name.equals(std.name) && (this.age == std.age));
          }
        }
        @Override
        public int hashCode() {
          int hashno = 7;
          hashno = 13 * hashno + (name == null ? 0 : name.hashCode());
          return hashno;
        }   
} 

* Output *

---Natural Sorting by Name---
Id:1, Name: Mahesh, Age:12
Id:3, Name: Nilesh, Age:10
Id:2, Name: Suresh, Age:15
---Natural Sorting by Name in reverse order---
Id:2, Name: Suresh, Age:15
Id:3, Name: Nilesh, Age:10
Id:1, Name: Mahesh, Age:12
---Sorting using Comparator by Age---
Id:3, Name: Nilesh, Age:10
Id:1, Name: Mahesh, Age:12
Id:2, Name: Suresh, Age:15
---Sorting using Comparator by Age with reverse order---
Id:2, Name: Suresh, Age:15
Id:1, Name: Mahesh, Age:12
Id:3, Name: Nilesh, Age:10
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章