首页 > 其他分享 >map stream

map stream

时间:2022-12-06 13:12:07浏览次数:30  
标签:map 12 stream future 2022 put now LocalDate

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,12,stream,future,2022,put,now,LocalDate
From: https://www.cnblogs.com/tonggc1668/p/16954933.html

相关文章