jdk8的排序

一 实体类
package exmind.base ;


/**
 * 实体类
 *
 *  @author  pengXiaoLin
 *  @date  2017-09-07 11:10
 **/


/**
 * Created by jstarseven on 2016/3/21.
 */
public class Human {

     private String  name ;
    private int  age ;

     @Override
     public String  toString() {
         return  "Human{" +
             "name='" name ' \' +
             ", age=" age +
             '}' ;
    }

     public  Human() {
         super() ;
    }

     public  Human( final String name , final int age) {
         super() ;
        this. name = name ;
        this. age = age ;
    }

     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 ;
    }

     public static int  compareByNameThenAge(Human lhs Human rhs) {
         if (lhs.getName().equals(rhs.getName())) {
             return lhs.getAge() - rhs.getAge() ;
        else {
             return lhs.getName().compareTo(rhs.getName()) ;
        }
    }


}

测试类
package exmind.base;


import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * @author pengXiaoLin
 * @create 2017-08-11 16:28
 **/

public class Test {

    public static void main(String[] args) {
        baseSort();
    }

    /**
     * 不使用Lambda表达式的基本排序
     */
    public static void baseSort() {

        List<Human> humans = new ArrayList();
        humans.add(new Human("A", 10));
        humans.add(new Human("C", 14));
        humans.add(new Human("C", 12));
        humans.add(new Human("B", 12));
        Collections.sort(humans, new Comparator<Human>() {
            public int compare(Human o1, Human o2) {
                return o1.getName().compareTo(o2.getName());
            }
        });
        for (Human bean : humans) {
            System.out.println(bean.toString());
        }
    }


    //    使用Lambda表达式的基本排序
    public void whenSortingEntitiesByName_thenCorrectlySorted() {
        List<Human> humans = new ArrayList();
        humans.add(new Human("A", 10));
        humans.add(new Human("C", 14));
        humans.add(new Human("C", 12));
        humans.add(new Human("B", 12));
        humans.sort((Human h1, Human h2) -> h1.getName().compareTo(h2.getName()));
        for (Human bean : humans) {
            System.out.println(bean.toString());
        }
    }


    //    没有类型定义( Type Definitions)的基本排序
    public void givenLambdaShortForm_whenSortingEntitiesByName_thenCorrectlySorted() {
        List<Human> humans = new ArrayList();
        humans.add(new Human("A", 10));
        humans.add(new Human("C", 14));
        humans.add(new Human("C", 12));
        humans.add(new Human("B", 12));
        humans.sort((h1, h2) -> h1.getName().compareTo(h2.getName()));
        for (Human bean : humans) {
            System.out.println(bean.toString());
        }
    }

    //    使用静态方法的引用来排序
    public void givenMethodDefinition_whenSortingEntitiesByNameThenAge_thenCorrectlySorted() {
        List<Human> humans = new ArrayList();
        humans.add(new Human("A", 10));
        humans.add(new Human("C", 14));
        humans.add(new Human("C", 12));
        humans.add(new Human("B", 12));
        humans.sort(Human::compareByNameThenAge);
        for (Human bean : humans) {
            System.out.println(bean.toString());
        }
    }

    //    提取Comparator进行排序
    public void givenInstanceMethod_whenSortingEntitiesByNameThenAge_thenCorrectlySorted() {
        List<Human> humans = new ArrayList();
        humans.add(new Human("A", 10));
        humans.add(new Human("C", 14));
        humans.add(new Human("C", 12));
        humans.add(new Human("B", 12));
        Collections.sort(humans, Comparator.comparing(Human::getName));
        for (Human bean : humans) {
            System.out.println(bean.toString());
        }
    }

    //    反转排序
    public void whenSortingEntitiesByNameReversed_thenCorrectlySorted() {
        List<Human> humans = new ArrayList();
        humans.add(new Human("A", 10));
        humans.add(new Human("C", 14));
        humans.add(new Human("C", 12));
        humans.add(new Human("B", 12));
        Comparator<Human> comparator = (h1, h2) -> h1.getName().compareTo(h2.getName());

        humans.sort(comparator.reversed());
        for (Human bean : humans) {
            System.out.println(bean.toString());
        }
    }

    //    多条件排序
    public void whenSortingEntitiesByNameThenAge_thenCorrectlySorted() {
        List<Human> humans = new ArrayList();
        humans.add(new Human("A", 10));
        humans.add(new Human("C", 14));
        humans.add(new Human("C", 12));
        humans.add(new Human("B", 12));
        humans.sort((lhs, rhs) -> {
            if (lhs.getName().equals(rhs.getName())) {
                return lhs.getAge() - rhs.getAge();
            } else {
                return lhs.getName().compareTo(rhs.getName());
            }
        });
        for (Human bean : humans) {
            System.out.println(bean.toString());
        }
    }

    //    多条件组合排序
    public void givenComposition_whenSortingEntitiesByNameThenAge_thenCorrectlySorted() {
        List<Human> humans = new ArrayList();
        humans.add(new Human("A", 10));
        humans.add(new Human("C", 14));
        humans.add(new Human("C", 12));
        humans.add(new Human("B", 12));
        humans.sort(Comparator.comparing(Human::getName).thenComparing(Human::getAge));
        for (Human bean : humans) {
            System.out.println(bean.toString());
        }
    }
}




 
 
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author pengXiaoLin
 * @date 2018-05-21 17:59
 **/
public class Test3 {
    public static void main(String[] args) {
        // 数组求个数排序
        Integer[] x = new Integer[]{1, 1, 2, 2,2, 8,9,9,10};
        List<Integer> y = Arrays.asList(x);
        Map<Integer, Long> map = y.stream().collect(Collectors.groupingBy(p -> p, Collectors.counting()));
        Map<Integer, Long> finalMap = new LinkedHashMap<>();
        map.entrySet().stream().sorted(Map.Entry.<Integer, Long>comparingByValue().reversed()).forEachOrdered(e -> finalMap.put(e.getKey(), e.getValue()));
        List<Map.Entry<Integer, Long>> collect = finalMap.entrySet().stream().limit(1).collect(Collectors.toList());
        System.out.println(collect);
    }
}

4.7 KB
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值