java8特性:list转Map并排序

初始代码

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 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<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());
  • 0
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值