带拼接多字段分组List< Object> 转 Map
Map<String, List<ProfitAndLossMapping>> collect = plMappingList.stream()
.collect(Collectors.groupingBy(m -> m.getLosType() + ":" + m.getRuleType()));
带拼接多字段分组List< Object> 转 Map
List<LosNameListByFy> losNameListByFIES = losNameListByFyMapper.selectList(null);
Map<String, String> losMap = losNameListByFIES.stream()
.collect(Collectors.toMap(o -> o.getFy() + ":" + o.getLos(), LosNameListByFy::getNewLos));
List< Object> 转 Map
Map<String, Long> reportOrgIdMap = reportOrgConfs.stream()
.collect(Collectors.groupingBy(m -> m.getReport() + ":" + m.getOrgId(), Collectors.counting()));
List< Object> 转 Map
Map<String, BigDecimal> avgMap = summaryInitList.stream()
.collect(HashMap::new, (map, item) -> map.put(item.getCombinedValue(), item.getValue()), HashMap::putAll);
Map<String,String> columnAndDimensionMap=dimensions.stream()
.filter(m->StringUtils.isNotBlank(m.getColumnName()))
.collect(Collectors.toMap(Dimension::getColumnName, Dimension::getName, (key1, key2) -> key1));
List< String> 转 Map
Map<String,String> reportMap=report.stream()
.filter(StringUtils::isNotBlank)
.collect(Collectors.toMap(Function.identity(),Function.identity()));
List< Object> 转 Map
Map<String, List<DictItemDetailVO>> map = list.stream()
.collect(Collectors.groupingBy(DictItemDetailVO::getDescription));
List< String>去重 拼接
List<String> list = Arrays.asList("AA", "BB", "CC", "BB", "CC", "AA", "AA");
long l = list.stream().distinct().count();
System.out.println("No. of distinct elements:"+l);
String output = list.stream().distinct().collect(Collectors.joining(","));
System.out.println(output);
List<Map<String, String>> list = new ArrayList<>();
{
Map<String, String> map = new HashMap<>();
map.put("id", "1");
map.put("name", "B");
map.put("age", "C");
list.add(map);
}
{
Map<String, String> map = new HashMap<>();
map.put("id", "1");
map.put("name", "E");
map.put("age", "F");
list.add(map);
}
//1.返回结果{"1","B"},{"2","E"}
Map<String, String> a = list.stream().collect(Collectors.toMap(l -> l.get("id"),
l -> l.get("name")));
//2.两种方法返回结果{"1":{"name":"B","id":"1","age":"C"},"2":{"name":"E","id":"2","age":"F"}}
Map<String, Map> b = list.stream().collect(Collectors.toMap(l -> l.get("id"), map -> map));
Map<String, Map> c = list.stream().collect(Collectors.toMap(l -> l.get("id"),
Function.identity()));
//3.重复key情况下处理方式返回结果{"1":{"name":"E","id":"1","age":"F"}}
Map<String, Map> d = list.stream().collect(Collectors.toMap(l -> l.get("id"),
Function.identity(), (key1, key2) -> key2));
//4.重复key情况下指定返回数据类型处理方式返回结果{"1":{"name":"E","id":"1","age":"F"}}
Map<String, Map> e = list.stream().collect(Collectors.toMap(l -> l.get("id"),
Function.identity(), (key1, key2) -> key2, LinkedHashMap::new));
//5.list根据key合并并转map;返回结果{"1":[{"name":"B","id":"1","age":"C"},{"name":"E","id":"1","age":"F"}]}
Map<String, List<Map>> lableGbType = list.stream()
.collect(Collectors.groupingBy(l -> (String) l.get("id")));
//6.根据key提取list中指定的值转List数组;返回结果["1","1"]
List<String> ids = list.stream().map(m -> (String) m.get("id"))
.collect(Collectors.toList());
//7.数组去重并转list
String[] str = "1,1,2,3,4,5,".split(",");
List<String> listNames = Arrays.stream(str).filter(name -> !isEmpty(name)).distinct().collect(Collectors.toList());
}
查数据
List<Map<String, Object>> prnInfo = xxxMapper.selectInfo(pars);
List<String> ids= prnInfo.stream().map(m -> m.get("id").toString()).collect(Collectors.toList());
**1.根据map的某个key分组**
Map<String, List<Map<String, Object>>> res= dataList.stream().collect(
groupingBy(map -> map.get("d").toString()));
获取type="ZC"的数据
----------------------------------------------------------------
List<Map<String, Object>> data = res.stream().
filter(
map -> (map.get("type")+"").equals("ZC")
).collect(Collectors.toList());
-----------------------------------------------------------------
List<Map<String, Object>> res = prnInfo .stream().filter(e ->Integer.parseInt(e.get("caseFlag").toString()) != 0)
.collect(Collectors.toList());
**2.根据某个key求对应value和**
int totalNums= prnInfo .stream().collect(Collectors.summingInt( e -> Integer.parseInt(e.get("num").toString())));
**3.根据map中的某个key的value值进行判断过滤**
List
**4.对集合中的map做变更**
List<Map<String, Object>> res= prnInfo .stream().map(x -> {
x.put("encd", Double.parseDouble(x.get("rz")+"")-Double.parseDouble(x.get("tdz")+""));
return x;
}).collect(Collectors.toList());
**5.排序**
List<Map<String, Object>> res= prnInfo.stream().sorted((e1,e2) -> {
return -Double.compare(Double.valueOf(e1.get("num").toString()),Double.valueOf(e2.get("num").toString()));
}).collect(Collectors.toList());
res.sort(Comparator.comparing((Map<String, Object> h) -> (h.get("tm").toString())));
//排序可能对应字段数据为null导致空指针,需要先判断过滤一下
res.stream().filter(Objects::nonNull).filter((Map<String, Object> h)
-> (Objects.nonNull(h.get("fz")))).collect(Collectors.toList());
6.去重
List<String> res = prnInfo.stream().distinct().collect(Collectors.toList());
7.做统计
IntSummaryStatistics collect = list.stream().collect(Collectors.summarizingInt(Test::getId));
System.out.println("和:" + collect.getSum());
System.out.println("数量:" + collect.getCount());
System.out.println("最大值:" + collect.getMax());
System.out.println("最小值:" + collect.getMin());
System.out.println("平均值:" + collect.getAverage());
最大
double max = prnInfo.stream().mapToDouble(l -> ((BigDecimal) l.get("num")).doubleValue()).max().getAsDouble();
和
double sum = prnInfo.stream().mapToDouble(l -> ((BigDecimal) l.get("num")).doubleValue()).sum();
8.list-map转换
Map<String, Object> map = list.stream()
.collect(Collectors.toMap(i -> i.getName() + i.getUnitName(), j -> j, (k1, k2) -> k1));
------------------------------------------------------------------------
List<User> collect = map.entrySet().stream().map(item -> {
User user= new User();
user.setId(item.getKey());
user.setName(item.getValue());
return user;
}).collect(Collectors.toList());
遍历。。
users.stream().forEach(x->{
System.out.println(x);
});
下面这个场景用的也很多,List里面的a和b相等就把c属性相加,报表里面某些属性相等则求和等场景,可以先根据需要去重的多个字段进行分组,再计算返回
for (Map.Entry<String, List<DTO>> entry : beanList.parallelStream().collect(groupingBy(o -> (o.getId() + o.geCode()), Collectors.toList())).entrySet()) {
if(bitMap.contains(entry.getKey()) && entry.getValue().size()==1){
objects.add(entry.getValue().get(0));
}else{
List<DTO> transfer = entry.getValue();
transfer.stream().reduce((a, b) -> DTO.builder()
.irrCd(a.getIrrCd())
.id(a.getId())
.tm(a.getCode())
.build())
.ifPresent(objects::add);
}
}