java8中的Stream
Collection.stream() / parallelStream()
1. Stream
1)Filter
2)Sorted
stringCollection .stream().sorted()
3)Map
stringCollection .stream().map(String::toUpperCase) .sorted((a, b) -> b.compareTo(a))
4)Matches
boolean allStartsWithA = stringCollection.stream().allMatch((s) -> s.startsWith("a"));
boolean noneStartsWithZ = stringCollection.stream().noneMatch((s) -> s.startsWith("z"));
5)Count
long count = values.parallelStream().sorted().count(); System.out.println(count);
long t1 = System.nanoTime();
long millis = TimeUnit.NANOSECONDS.toMillis(t1 - t0); System.out.println(String.format("parallel sort took: %d ms", millis)); // parallel sort took: 472 ms
2) MAP
for (int i = 0; i < 10; i++) { map.putIfAbsent(i, "val" + i);}
map.forEach((id, val) -> System.out.println(val));
map.computeIfPresent(9, (num, val) -> null); map.containsKey(9); // false
map.computeIfAbsent(23, num -> "val" + num); map.containsKey(23); // true
map.computeIfAbsent(3, num -> "bam"); map.get(3); // val33
map.getOrDefault(42, "not found"); // not found
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步