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());
 
posted @ 2022-02-15 09:58  全琪俊  阅读(1309)  评论(0编辑  收藏  举报