package com.icil.elsa.test.utils;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.TreeSet;
import java.util.stream.Collectors;
import org.junit.Test;
import lombok.ToString;
import lombok.Data;

/**
 * 用 java8 对 list 按照多个字段进行去重 id  ,  message ,orderNO 去重
 * 对对象 MilestonesValidTO 按照 field 
 * 参考1:https://blog.csdn.net/robotfive/article/details/89200294
 * 参考2:https://blog.csdn.net/weixin_39643007/article/details/90511098 
 * @throws Exception
 */
public class GroupBYTest {
    @Data
    @ToString
    public class MilestonesValidTO {
        private String id;  
        private String message; 
        private String orderNO;  
        private String courierBillNo; 
        private String status;   
        public MilestonesValidTO(String id, String message, String orderNO, String courierBillNo) {
            super();
            this.id = id;
            this.message = message;
            this.orderNO = orderNO;
            this.courierBillNo = courierBillNo;
        }
        }


    /**
     #####如果 id + message + orderNo 都一样, 就认为是一条数据#######

* 用 java8 对 list 按照多个字段进行去重 id , message ,orderNO 去重 * 对对象 MilestonesValidTO 按照 field * 如果仅仅是去重,推荐使用方式1 * @throws Exception
*/ /*################# 方式1: 直接重写 comparater 方法 ####################*/ @Test public void testGroupByMutilFiled1() throws Exception { ArrayList<MilestonesValidTO> list = new ArrayList<MilestonesValidTO>(); list.add(new MilestonesValidTO("1","A","sea","2341")); list.add(new MilestonesValidTO("1","A","sea","2341")); list.add(new MilestonesValidTO("3","B","sea","2341")); list.add(new MilestonesValidTO("4","A","sea","2341")); list.add(new MilestonesValidTO("5","C","sea","2341")); List<MilestonesValidTO> lst = list.parallelStream().collect(Collectors.collectingAndThen(Collectors.toCollection( () -> new TreeSet<>(Comparator.comparing(o -> o.getId() + "#" + o.getMessage() + "#" + o.getOrderNO()))), ArrayList<MilestonesValidTO>::new)); lst.forEach(m-> System.err.println(m.toString())); } /** * 用 java8 对 list 按照多个字段进行去重 id , message ,orderNO 去重 * 对对象 MilestonesValidTO 按照 field * 方式2:用于收集,统计,比较合适 * @throws Exception */ /*################# 方式2: Collectors.groupingBy(内嵌groupBy) ####################*/ @Test public void testGroupByMutilFiled2() throws Exception { ArrayList<MilestonesValidTO> list = new ArrayList<MilestonesValidTO>(); list.add(new MilestonesValidTO("1","A","sea","2341")); list.add(new MilestonesValidTO("1","A","sea","2341")); list.add(new MilestonesValidTO("3","B","sea","2341")); list.add(new MilestonesValidTO("4","A","sea","2341")); list.add(new MilestonesValidTO("5","C","sea","2341")); // Map<String, Map<String, Double>> sumCase = statisticalAnalyses.stream() // .collect(Collectors.groupingBy( // StatisticalAnalysis::getCreateTime, // Collectors.groupingBy(StatisticalAnalysis::getHandlingDepartmentName, Collectors.summingDouble(StatisticalAnalysis::getSimplePenaltyAmount)))); Map<String, Map<String, List<MilestonesValidTO>>> collect = list.parallelStream() .collect(Collectors.groupingBy(MilestonesValidTO::getId,Collectors.groupingBy(MilestonesValidTO::getMessage))); collect.forEach((k,v)->{System.err.println("K->"+k+" V-->"+v);}); } }

 

常用:

//提取Bean 中的个别字段
List<String> hbookingNo = houseLinkList.stream().map(MasterLinkDO::getBookingNo).collect(Collectors.toList()); //把一个大的list切割 List<List<String>> ListbookingNoList = Lists.partition(bookingNoList, 100); //过滤去重

List<MilestoneInfo> successAndDistincList = worktbMileAllList.stream().filter(m->m.getSuccess()==true).collect(Collectors.collectingAndThen(Collectors.toCollection(
// () -> new TreeSet<>(Comparator.comparing(o -> o.getCourierBillNo()))),
() -> new TreeSet<>(Comparator.comparing(o -> o.getStatus() + "#" + o.getBookingNo() + "#" + o.getCourierBillNo()))),
ArrayList<MilestoneInfo>::new));


            
 //分组
Map<String, Map<String, List<MilestoneInfo>>> status_bookingNoAndDetailMap_map = worktbMileSuccessAndDistincList.stream()
        .collect(Collectors.groupingBy(MilestoneInfo::getStatus,Collectors.groupingBy(MilestoneInfo::getBookingNo)));
                
    

 

 partitioningBy:

有这样一个集合:

List<Integer> integers = [ 1,2,3,4,5,6,7,8,9]
现在需要把集合按奇偶数分割为两个列表。这种情况下,我们可以使用Collectors.partitioningBy来分割:

Map<Boolean, List<Integer>> map = 
    integers.stream().collect(Collectors.partitioningBy(x -> x%2 == 0));
partitioningBy会根据值是否为true,把集合分割为两个列表,一个true列表,一个false列表。

 

 

Java8-对map排序 

 1、Java8对map按key排序
/**
 * 内容 : Java8对map按key排序
 */
public class SortByKeyExample {

    public static void main(String[] args) {
        Map<String, Integer> unsortMap = new HashMap<>();
        unsortMap.put("z", 10);
        unsortMap.put("b", 5);
        unsortMap.put("a", 6);
        unsortMap.put("c", 20);
        unsortMap.put("d", 1);
        unsortMap.put("e", 7);
        unsortMap.put("y", 8);
        unsortMap.put("n", 99);
        unsortMap.put("g", 50);
        unsortMap.put("m", 2);
        unsortMap.put("f", 9);
        System.out.println(unsortMap);

        Map<String, Integer> result1 = unsortMap.entrySet().stream()
                .sorted(Map.Entry.comparingByKey())
                .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue,
                        (oleValue, newValue) -> oleValue, LinkedHashMap::new));
        System.out.println(result1);

        Map<String, Integer> result2 = new LinkedHashMap<>();
        unsortMap.entrySet().stream()
                .sorted(Map.Entry.comparingByKey())
                .forEachOrdered(x -> result2.put(x.getKey(), x.getValue()));
        System.out.println(result2);
    }
}

 

2、Java8对map按Vaule排序

/**
 * 内容 : Java8对map按Vaule排序
 */
public class SoryByKeyExample {

    public static void main(String[] args) {
        Map<String, Integer> unsortMap = new HashMap<>();
        unsortMap.put("z", 10);
        unsortMap.put("b", 5);
        unsortMap.put("a", 6);
        unsortMap.put("c", 20);
        unsortMap.put("d", 1);
        unsortMap.put("e", 7);
        unsortMap.put("y", 8);
        unsortMap.put("n", 99);
        unsortMap.put("g", 50);
        unsortMap.put("m", 2);
        unsortMap.put("f", 9);
        System.out.println(unsortMap);

        Map<String, Integer> result1 = unsortMap.entrySet().stream()
                .sorted(Map.Entry.comparingByValue(Comparator.reverseOrder()))
                .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue,
                        (oldValue, newValue) -> oldValue, LinkedHashMap::new));

        System.out.println(result1);

        Map<String, Integer> result2 = new LinkedHashMap<>();
        unsortMap.entrySet().stream()
                .sorted(Map.Entry.<String, Integer>comparingByValue().reversed())
                .forEachOrdered(x -> result2.put(x.getKey(), x.getValue()));
        System.out.println(result2);
    }
}

 

posted on 2019-06-27 12:12  lshan  阅读(10085)  评论(0编辑  收藏  举报