JAVA8使用Stream排序

一、 基础类型排序

package com.xiaobu.sort;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author xiaobu
 * @version JDK1.8.0_171
 * @date on  2019/6/24 22:17
 * @description V1.0   sorted(Comparator.reverseOrder()) 逆序 默认是正序
 */
public class SortDemo1 {
    public static void main(String[] args) {
        sortAsc();
        sortDesc();


    }


    /**
     * @author xiaobu
     * @date 2019/6/24 22:31
     * @return void
     * @descprition  正序排列
     * @version 1.0
     */
    public static void sortAsc(){
        List list = new ArrayList<>();
        list.add(50);
        list.add(45);
        list.add(25);
        list.add(98);
        list.add(32);
        List collect = list.stream().sorted().collect(Collectors.toList());
        System.out.println("list元素正序:" + collect);
    }

    /**
     * @author xiaobu
     * @date 2019/6/24 22:31
     * @return void
     * @descprition  逆序排列
     * @version 1.0
     */
    public static void sortDesc(){
        List list = new ArrayList<>();
        list.add(50);
        list.add(45);
        list.add(25);
        list.add(98);
        list.add(32);
        List collect = list.stream().sorted(Comparator.reverseOrder()).collect(Collectors.toList());
        System.out.println("list元素逆序:" + collect);
    }


}

二、根据某个属性排序

 

package com.xiaobu.sort;

import lombok.Data;

import java.math.BigDecimal;

/**
 * @author xiaobu
 * @version JDK1.8.0_171
 * @date on  2019/6/24 22:03
 * @description V1.0
 */
@Data
public class Person {
    private String name;

    private BigDecimal salary;

    public Person(){}

    public Person(String name,BigDecimal salary){
        this. name = name;
        this.salary =salary;
    }
}

 

package com.xiaobu.sort;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author xiaobu
 * @version JDK1.8.0_171
 * @date on  2019/6/24 22:02
 * @description V1.0  Comparator.comparing()根据属性排序
 */
public class SortDemo {
    public static void main(String[] args) {
        Person p1 = new Person("张三", new BigDecimal("50.0"));
        Person p2 = new Person("王五", new BigDecimal("25.0"));
        Person p3 = new Person("李四", new BigDecimal("68.0"));
        Person p4 = new Person("李四", new BigDecimal("17.0"));
        Person p5 = new Person("张三", new BigDecimal("45.0"));
        List list = new ArrayList<>();
        list.add(p1);
        list.add(p2);
        list.add(p3);
        list.add(p4);
        list.add(p5);
        sortAsc(list);
        sortDesc(list);
    }

    /**
     * @author xiaobu
     * @date 2019/6/24 22:46
     * @param list List
     * @return void
     * @descprition
     * @version 1.0
     */
    public static void  sortAsc(List list){
        List collect = list.stream().sorted(Comparator.comparing(Person::getSalary).reversed()).collect(Collectors.toList());
        System.out.println("元素的属性值倒序:"+collect);
    }


    /**
     * @author xiaobu
     * @date 2019/6/24 22:46
     * @param list List
     * @return void
     * @descprition
     * @version 1.0
     */
    public static void sortDesc(List list){
     List collect=list.stream().sorted(Comparator.comparing(Person::getSalary)).collect(Collectors.toList());
        System.out.println("元素的属性值倒序:"+collect);
    }
}

 

你可能感兴趣的:(JAVA)