java8mapgroupby对多个字段进⾏去重,统计partitioningBy,排序package com.st.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:blog.csdn/robotfive/article/details/89200294
* 参考2:blog.csdn/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;
sortedlistpublic MilestonesValidTO(String id, String message, String orderNO, String courierBillNo) {
super();
this.id = id;
}
}
/**
     >如果 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().Collection(
() -> new TreeSet<>(Comparatorparing(o -> o.getId() + "#" + o.getMessage() + "#" + o.getOrderNO()))),
ArrayList<MilestonesValidTO>::new));
lst.forEach(m-> String()));
}
/**
* ⽤ java8 对 list 按照多个字段进⾏去重 id  ,  message ,orderNO 去重
* 对对象 MilestonesValidTO 按照 field
* ⽅式2:⽤于收集,统计,⽐较合适
* @throws Exception
*/
/*>>>## ⽅式2: upingBy(内嵌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()
//                .upingBy(
//                        StatisticalAnalysis::getCreateTime,
//                        upingBy(StatisticalAnalysis::getHandlingDepartmentName, Collectors.summingDouble(StatisticalAnalysis::getSimplePenaltyAmount))));        Map<String, Map<String, List<MilestonesValidTO>>> collect = list.parallelStream()
.upingBy(MilestonesValidTO::upingBy(MilestonesValidTO::getMessage)));
collect.forEach((k,v)->{println("K->"+k+"  V-->"+v);});
}
}
常⽤:
//提取Bean 中的个别字段
List<String> hbookingNo = houseLinkList.stream().map(MasterLinkDO::getBookingNo).List());
//把⼀个⼤的list切割
List<List<String>> ListbookingNoList = Lists.partition(bookingNoList, 100);
//过滤去重
List<MilestoneInfo> successAndDistincList = worktbMileAllList.stream().filter(m-
&Success()==true).Collection(
// () -> new TreeSet<>(Comparatorparing(o -> o.getCourierBillNo()))),
() -> new TreeSet<>(Comparatorparing(o -> o.getStatus() + "#" + o.getBookingNo() + "#" + o.getCourierBillNo()))),
ArrayList<MilestoneInfo>::new));
//分组
Map<String, Map<String, List<MilestoneInfo>>> status_bookingNoAndDetailMap_map = worktbMileSuccessAndDistincList.stream()
.upingBy(MilestoneInfo::upingBy(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列表。
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 = Set().stream()
.sorted(Map.EntryparingByKey())
.Map(Map.Entry::getKey, Map.Entry::getValue,                        (oleValue, newValue) -> oleValue, LinkedHashMap::new));
System.out.println(result1);
Map<String, Integer> result2 = new LinkedHashMap<>();
.sorted(Map.EntryparingByKey())
.forEachOrdered(x -> result2.Key(), 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 = Set().stream()
.sorted(Map.verseOrder()))                .Map(Map.Entry::getKey, Map.Entry::getValue,                        (oldValue, newValue) -> oldValue, LinkedHashMap::new));
System.out.println(result1);
Map<String, Integer> result2 = new LinkedHashMap<>();
.sorted(Map.Entry.<String, Integer>comparingByValue().reversed())                .forEachOrdered(x -> result2.Key(), x.getValue()));
System.out.println(result2);
}
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。

发表评论