java8特性:list转Map并排序 您所在的位置:网站首页 java集合排序带null java8特性:list转Map并排序

java8特性:list转Map并排序

2024-07-16 20:47| 来源: 网络整理| 查看: 265

初始代码

public Map getMap(List lists){ Map map = new TreeMap(); if(lists==null) { return map; } for(RgwstBean rb :lists) { String newdate = rb.getDatetime(); if(map.containsKey(newdate)) { map.get(newdate).add(rb); }else { List newlist2 = new ArrayList(); newlist2.add(rb); map.put(newdate, newlist2); } } return map; }

lambda语法

public Map getMap(List lists){ //groupingBy无排序 Map map = lists.stream().collect(Collectors.groupingBy(RgwstBean::getDatetime)); Map sortmap = new TreeMap(); //Map sortmap = new TreeMap((o1,o2)->o2.compareTo(o1));//倒序 map.entrySet() .stream() .forEach(x->sortmap.put(x.getKey(),x.getValue())); return sortmap; } File[] allFiles = new File("D:/xx/20191114").listFiles(); Map maps = Arrays.stream(allFiles).collect(Collectors.groupingBy(f -> f.getName().substring(0,f.getName().lastIndexOf(".")))); Map maps2 = Arrays.stream(allFiles).collect(Collectors.groupingBy(File::getName)); //list统计某个字段 Map map = ls.stream().collect(Collectors.groupingBy(WarningSynthesize::getDistributionArea, Collectors.counting())); public Map getIdNameMap(List accounts) { return accounts.stream().collect(Collectors.toMap(Account::getId, Account::getUsername)); }

list排序

List files = maps.get(time); files.sort(Comparator.comparing(File::getName));//正序 files.sort(Comparator.comparing(File::getName).reversed());//倒序

取map中key最大值的记录

String time = maps.keySet().stream().max(String::compareTo).get(); List files = maps.get(time);

List转Map并去重复key

List mapLists = mongoTemplate.find(new Query(Criteria.where("datetime").gte(startTime).lte(endTime)),Map.class,"xxx"); Map map = mapLists.stream().collect(Collectors.toMap(a -> a.get("station_id_d").toString(), Function.identity(), (key1, key2) -> key2));

List求和、平均值、最小值、最大值

List map2Lists; double sum = map2Lists.stream().mapToDouble(m -> Double.parseDouble(m.get("pre").toString())).sum(); double avg = map2Lists.stream().mapToDouble(m -> Double.parseDouble(m.get("pre").toString())).average().getAsDouble(); double min = map2Lists.stream().mapToDouble(m -> Double.parseDouble(m.get("pre").toString())).min().getAsDouble(); double max = map2Lists.stream().mapToDouble(m -> Double.parseDouble(m.get("pre").toString())).max().getAsDouble();

List转Map

List objects = new ArrayList(); for (int i=0;i objects.add("jiang-apiTest"+i+"-2"); } System.out.println("objects = " + new Gson().toJson(objects)); Map obj = objects.stream().collect(Collectors.groupingBy(f -> f.split("-")[0], Collectors.toMap(v -> v.split("-")[1], v -> Long.parseLong(v.split("-")[2])))); System.out.println("maps = " + new Gson().toJson(obj)); objects = ["admin-random0-2","admin-random1-2","jiang-apiTest0-2","jiang-apiTest1-2"] maps = {"admin":{"random0":2,"random1":2},"jiang":{"apiTest0":2,"apiTest1":2}}

根据File文件名时间取时间最大文件

File[] files = new File("xxx").listFiles(); Optional fileOptional = Arrays.stream(files).filter(f -> f.getName().length()!=16).max(Comparator.comparingLong(file -> Long.parseLong(file.getName().substring(0,file.getName().indexOf("."))))); File f = fileOptional.get();

filter

.filter(f -> f.getName().startsWith("SATE")) 过滤掉文件名开头不是SATE的文件,即显示所有文件开头为SATE的文件

map遍历

map.forEach((k, v) -> System.out.println("key:value = " + k + ":" + v));

map转list

map.entrySet().stream().map(e -> new Person(e.getKey(),e.getValue())).collect(Collectors.toList());

list转list

List ls = null; List num_list = ls.stream().map(a -> a.getNum().split("_")[a.getNum().split("_").length-1]).collect(Collectors.toList());


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有