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());
 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM