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 Listd94943c0b4933ad8cac500132f64757f
into Mapb56561a2c0bc639cf0044c0859afb88f
Map<String, Choice> result = choices.stream().collect(Collectors.toMap(Choice::getName, Function.identity()));Use StreamEx API:
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();Use StreamEx API:
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));Use StreamEx API:
StreamEx.of(persons).distinctBy(Person::getName);
Stream.of(objects) .filter(Client.class::isInstance) .map(Client.class::cast) .map(Client::getID) .forEach(System.out::println);Use StreamEx API:
StreamEx.of(objects) .select(Client.class) .map(Client::getID) .forEach(System.out::println);Related articles:
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!