java8特性:list转Map并排序(转)
//java8去重
List<User> userList = new ArrayList<User>();
buildUserList = buildUserList.stream().collect(
Collectors.collectingAndThen(Collectors.toCollection(
() -> new TreeSet<>(Comparator.comparing(BuildUser::getIdNum))), ArrayList::new));
防止为空或相同
.stream().collect(Collectors.toMap(BuildScanRegisterWithBLOBs::getIdNum, v -> v,(a, b) -> b, HashMap::new));
初始代码
public Map<String,List<RgwstBean>> getMap(List<RgwstBean> lists){
Map<String,List<RgwstBean>> map = new TreeMap<String,List<RgwstBean>>();
if(lists==null) {
return map;
}
for(RgwstBean rb :lists) {
String newdate = rb.getDatetime();
if(map.containsKey(newdate)) {
map.get(newdate).add(rb);
}else {
List<RgwstBean> newlist2 = new ArrayList<RgwstBean>();
newlist2.add(rb);
map.put(newdate, newlist2);
}
}
return map;
}
lambda语法
public Map<String,List<RgwstBean>> getMap(List<RgwstBean> lists){
//groupingBy无排序
Map<String,List<RgwstBean>> map = lists.stream().collect(Collectors.groupingBy(RgwstBean::getDatetime));
Map<String,List<RgwstBean>> sortmap = new TreeMap<>();
//Map<String,List<RgwstBean>> 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<String, List<File>> maps = Arrays.stream(allFiles).collect(Collectors.groupingBy(f -> f.getName().substring(0,f.getName().lastIndexOf("."))));
Map<String, List<File>> maps2 = Arrays.stream(allFiles).collect(Collectors.groupingBy(File::getName));
//list统计某个字段
Map<String, Long> map = ls.stream().collect(Collectors.groupingBy(WarningSynthesize::getDistributionArea, Collectors.counting()));
public Map<Long, String> getIdNameMap(List<Account> accounts) {
return accounts.stream().collect(Collectors.toMap(Account::getId, Account::getUsername));
}
list排序
List<File> 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<File> files = maps.get(time);
List转Map并去重复key
List<Map> mapLists = mongoTemplate.find(new Query(Criteria.where("datetime").gte(startTime).lte(endTime)),Map.class,"xxx");
Map<String,Object> map = mapLists.stream().collect(Collectors.toMap(a -> a.get("station_id_d").toString(), Function.identity(), (key1, key2) -> key2));
List求和
List<Map> map2Lists;
double preNum = map2Lists.stream().mapToDouble(m -> Double.parseDouble(m.get("pre").toString())).sum();
List转Map<String,Map<String,Long>>
List<String> objects = new ArrayList<>();
for (int i=0;i<2;i++) {
objects.add("admin-random"+i+"-2");
}
for (int i=0;i<2;i++) {
objects.add("jiang-apiTest"+i+"-2");
}
System.out.println("objects = " + new Gson().toJson(objects));
Map<String,Map<String,Long>> 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<File> 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<Contract> ls = null;
List<String> num_list = ls.stream().map(a -> a.getNum().split("_")[a.getNum().split("_").length-1]).collect(Collectors.toList());
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 没有源码,如何修改代码逻辑?
· DeepSeek R1 简明指南:架构、训练、本地部署及硬件要求
· NetPad:一个.NET开源、跨平台的C#编辑器
· PowerShell开发游戏 · 打蜜蜂