T-T 对 JDK8 排序方法 看法

public class Table_3 {

    private String id;

    private String name;

    private String nameId;

    private String nametype;

。。。。。

}

 

public class testSort {


    public static void main(String[] args) {
        
        Table_3 table1 = new Table_3();
        table1.setId("1");
        table1.setName("a");
        table1.setNameId("因为");
        table1.setNametype("!");
        
        Table_3 table2 = new Table_3();
        table2.setId("3");
        table2.setName("c");
        table2.setNameId("世界");
        table2.setNametype("+a");
        
        Table_3 table3 = new Table_3();
        table3.setId("2");
        table3.setName("c");
        table3.setNameId("精彩c");
        table3.setNametype("-");
        
        Table_3 table4 = new Table_3();
        table4.setId("3");
        table4.setName("b");
        table4.setNameId("jingcaic");
        table4.setNametype("+b");
        
        Table_3 table5 = new Table_3();
        table5.setId("3");
        table5.setName("a");
        table5.setNameId("精彩a");
        table5.setNametype("&");

        
        List<Table_3> list = new ArrayList<>();
        list.add(table1);
        list.add(table2);
        list.add(table3);
        list.add(table4);
        list.add(table5);
    
        
        
        System.out.println("--------原始顺序---------");
        for(Table_3 h : list){
            System.out.println(h.getId()+"--"+h.getName());
        }
        
        list.sort((Table_3 a, Table_3 b) -> a.getId().compareTo(b.getId()));
        System.out.println("----Lambda----Id升序---------");
        for(Table_3 h : list){
            System.out.println(h.getId()+"--"+h.getName());
        }
        
        
        //sql的中文字段是 nvarchar,对应的java把字符串也转成unicode 来排序
        list.sort((Table_3 a, Table_3 b) -> Collator.getInstance(Locale.CHINA).compare(a.getNameId(), b.getNameId() ) );
        System.out.println("--------中文升序---------");
        for(Table_3 h : list){
            System.out.println(h.getId()+"--"+h.getName()+"--"+h.getNameId());
        }
        
        
       //个人认为按照String的一般排序比较就行,有意见可以提出来
        list.sort((Table_3 a, Table_3 b) -> a.getNametype().compareTo(b.getNametype()));
        System.out.println("--------特殊字符升序---------");
        for(Table_3 h : list){
            System.out.println(h.getId()+"--"+h.getNametype());
        }
        
        
        Collections.sort(list, Comparator.comparing(Table_3 :: getId).thenComparing(Table_3 :: getName).reversed());
        System.out.println("--------Id降序Name降序---------");
        list.forEach(h -> System.out.println(h.getId()+"--"+h.getName()));
        
        Collections.sort(list, Comparator.comparing(Table_3 :: getId).reversed().thenComparing(Table_3 :: getName));
        System.out.println("--------Id降序Name升序---------");
        list.forEach(h -> System.out.println(h.getId()+"--"+h.getName()));
        
        
        list.sort((Table_3 a, Table_3 b) -> b.getId().compareTo(a.getId()));
        System.out.println("--------Id降序---------");
        list.forEach(h -> System.out.println(h.getId()+"--"+h.getName()));
        
        Collections.sort(list, Comparator.comparing(Table_3 :: getName).reversed());
        System.out.println("--------Name降序---------");
        list.forEach(h -> System.out.println(h.getId()+"--"+h.getName()));

        Collections.sort(list, Comparator.comparing(Table_3 :: getId).thenComparing(Table_3 :: getName));
        System.out.println("--------Id升序Name升序---------");
        list.forEach(h -> System.out.println(h.getId()+"--"+h.getName()));

        /*这个排序倒是有疑义, 
        比如 reversed() 是n ,
        1. a b n =A B
        2. a n b n = A B
        3. a n b = A b
        开始以为 ( a n b ) n = a B,其实跟 2 没差别
        4. a n n b n 的话, 是对 a n n --> a , 然后  a b n = A B
        所以就得出
        a n n n b n --> a n n n = A ---> A b n = a B  
        */
        Collections.sort(list, Comparator.comparing(Table_3 :: getId).reversed().reversed().reversed().thenComparing(Table_3 :: getName).reversed());
        System.out.println("--------Id升序Name降序---------");
        list.forEach(h -> System.out.println(h.getId()+"--"+h.getName()+"--"+h.getNameId()));
        
        List<Table_3> list2 = list.stream().filter(n -> n.getName().equals("c")).sorted((Table_3 a, Table_3 b)-> b.getId().compareTo(a.getId())).collect(Collectors.toList());
        System.out.println("--------筛选名字=c的Id降序---------");
        list2.forEach(h -> System.out.println(h.getId()+"--"+h.getName()));
        
        System.out.println("--------name自定义排序 cab---------");
        list.sort((Table_3 a, Table_3 b) -> ownOrder(a.getName()).compareTo(ownOrder(b.getName()) ));
        list.forEach(h -> System.out.println(h.getId()+"--"+h.getName()));
    }
    
    private static String ownOrder(String order){
        if(order.equals("a")){
            return "1";
        }else if(order.equals("b")){
            return "2";
        }else if(order.equals("c")){
            return "0";
        }else{
            return order;
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值