在这片文章里,主要介绍怎么用Java 8 Stream的开源框架 来解答上一些经常被问到关于Java 8 Stream的问题:
用JDK Stream API:
Mapresult = choices.stream().collect(Collectors.toMap(Choice::getName, Function.identity()));
用StreamEx API:
Mapresult = 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 staticPredicate distinctByKey(Function keyExtractor) { Set
用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);