map stream
package com.example.demo; import java.time.LocalDate; import java.util.HashMap; import java.util.Map; public class Test { public static void main(String[] args) { Map<LocalDate,String> future = new HashMap<>(); future.put(LocalDate.now().minusDays(1), "1");//{2022-12-05=1} future.put(LocalDate.now(), "2");//{2022-12-06=2} future.put(LocalDate.now().plusDays(1), "3");//{2022-12-07=3} future.put(LocalDate.now().plusDays(2), "4");//{2022-12-08=4} future.put(LocalDate.now().plusDays(3), "5");//{2022-12-09=5} // future.entrySet().stream() // .filter(e -> e.getKey().isAfter(LocalDate.now())) // .sorted(Map.Entry.comparingByKey()) // .forEach(e -> System.out.println(e)); // 2022-12-07=3 // 2022-12-08=4 // 2022-12-09=5 Map<LocalDate,String> futureDates = future.entrySet().stream() .filter(e -> e.getKey().isAfter(LocalDate.now())) //.collect(Collectors.toMap(e -> e.getKey(), e -> e.getValue(), (key1, key2) -> key2)); .collect(HashMap::new,(m, e)->m.put(e.getKey(),e.getValue()),HashMap::putAll); System.out.println(futureDates);//{2022-12-09=5, 2022-12-08=4, 2022-12-07=3} } }
Map<String, Long> map = gatewayLogs.stream().collect(
Collectors.groupingBy(e -> getDate(e.getCreateTime(), DATE_SIMPLE_FORMAT), Collectors.counting()));
List<EmpServiceAnalyze> list = map.entrySet().stream() .sorted(Map.Entry.<String, Long>comparingByValue().reversed()).limit(rankNum) .map(entry -> new EmpServiceAnalyze(entry.getKey(), entry.getValue())).collect(Collectors.toList());
Map<String, EquipmentExamDetailPO> equipmentExamDetailPOMap = resultList.stream().collect(Collectors.toMap(EquipmentExamDetailPO::getDeviceId, Function.identity(), (k1, k2) -> k1));