脚本宝典收集整理的这篇文章主要介绍了Java 8 Stream之实战篇,脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
在这片文章里,主要介绍怎么用Java 8 Stream的开源框架 StreamEx来解答StackOverflow上一些经常被问到关于Java 8 Stream的问题:
- Convert Java 8
List<V>
into Map<K, V>
用JDK Stream API:
Map<String, Choice> result = choices.stream().collect(Collectors.toMap(Choice::getName, Function.identity()));
用StreamEx API:
Map<String, Choice> result = StreamEx.of(choices).toMap(Choice::getName);
用JDK Stream API:
ForkJoinPool forkJoinPool = new ForkJoinPool(2); forkJoinPool.submit(() -> //parallel task here, for example IntStream.range(1, 1_000_000).parallel().filter(PrimesPrint::isPrime).collect(toList()) ).get();
用StreamEx API:
IntStreamEx.range(1, 1_000_000).parallel(new ForkJoinPool(2)) .filter(PrimesPrint::isPrime).toList();
用JDK Stream API:
public static <T> Predicate<T> distinctByKey(Function<? super T, ?> keyExtractor) { Set<Object> seen = ConcurrentHashMap.newKeySet(); return t -> seen.add(keyExtractor.apply(t)); } persons.stream().filter(distinctByKey(Person::getName));
用StreamEx API:
StreamEx.of(persons).distinctBy(Person::getName);
用JDK Stream API:
Stream.of(objects) .filter(Client.class::isInstance) .map(Client.class::cast) .map(Client::getID) .foreach(System.out::println);
用StreamEx API:
StreamEx.of(objects) .select(Client.class) .map(Client::getID) .forEach(System.out::println);
以上是脚本宝典为你收集整理的Java 8 Stream之实战篇全部内容,希望文章能够帮你解决Java 8 Stream之实战篇所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。