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小时内删除。
java stream的用法
« 上一篇
Python基础(5)输入三个数并排序
下一篇 »
推荐文章
热门文章
-
m函数数字提取
2025-01-07 -
jest断言方法大全
2025-01-07 -
中兴ZXSEC US 管理员手册
2025-01-07 -
keras系列(一):参数设置
2025-01-07 -
Qt从QString中提取出数字
2025-01-07 -
element input 金额千分位格式化
2025-01-07 -
freemaker 参数解析正则
2025-01-07 -
C#正则验证数字
2025-01-07 -
form表单验证正则
2025-01-07 -
scanf正则表达式用法
2025-01-07 -
grafana value的正则表达式
2025-01-07 -
Android平台浮点数运算应用
2025-01-07 -
js-(JS正则表达式验证数字)
2025-01-07 -
判断Python输入是否是整数,字符,或浮点数
2025-01-07 -
c语言 sscanf 正则规则
2025-01-07 -
从文本中提取数值技巧
2025-01-07 -
js将整数转换成两位浮点数的方法
2025-01-07 -
vue正则限制浮点数
2025-01-07 -
8到20的结尾的正则
2025-01-07 -
shell 正则表达式 最后一行
2025-01-07
最新文章
-
应用程序的安全检测方法、装置、电子设备和存储介质
2025-01-07 -
VBA之正则表达式(1)--基础篇
2025-01-07 -
代码编辑的辅助方法、装置及电子设备
2025-01-07 -
SHELL查字符串中包含字符的命令
2025-01-07 -
String方法中replace和replaceAll的区别详解(源码分析)
2025-01-07 -
双字节符号正则
2025-01-07
发表评论