Summary This article explores the implementation principle of Stream and introduces how to use the open source framework StreamEx of Java 8 Stream to answer some frequently asked questions about Java 8 Stream on StackOverflow:
Convert Java 8 List<V>
into Map<K, V>
Map<String, Choice> result = choices.stream().collect(Collectors.toMap(Choice::getName, Function.identity()));
Map<String, Choice> result = StreamEx.of(choices).toMap(Choice::getName);
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();
IntStreamEx.range(1, 1_000_000).parallel(new ForkJoinPool(2)) .filter(PrimesPrint::isPrime).toList();
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.of(persons).distinctBy(Person::getName);
Stream.of(objects) .filter(Client.class::isInstance) .map(Client.class::cast) .map(Client::getID) .forEach(System.out::println);
StreamEx.of(objects) .select(Client.class) .map(Client::getID) .forEach(System.out::println);
Simple examples of Lambda expressions and Stream classes in Java
Related videos:Miaowei Tea House Javascript Practical Video Tutorial
The above is the detailed content of Java 8 Stream practical summary and principle application. For more information, please follow other related articles on the PHP Chinese website!