java-stream

Why is filtering by primality in an inifinite stream of numbers taking forever if processed in parallel?

Deadly 提交于 2019-12-21 03:52:36
问题 I'm creating an infinite stream of Integers starting at 200 Million, filter this stream using a naive primality test implementation to generate load and limit the result to 10. Predicate<Integer> isPrime = new Predicate<Integer>() { @Override public boolean test(Integer n) { for (int i = 2; i < n; i++) { if (n % i == 0) return false; } return true; } }; Stream.iterate(200_000_000, n -> ++n) .filter(isPrime) .limit(10) .forEach(i -> System.out.print(i + " ")); This works as expected. Now, if I

Converting a text file to Map<String, List<String>> using lambda

爱⌒轻易说出口 提交于 2019-12-21 03:52:07
问题 I am trying to convert the following text input file: A=groupA1 A=groupA2 A=groupA3 B=groupB1 B=groupB2 into Map<String, List<String>> by splitting each line on "=" So far I manged to get this sort of output: KEY: A VALUE: A=groupA1 VALUE: A=groupA2 VALUE: A=groupA3 KEY: B VALUE: B=groupB1 VALUE: B=groupB2 using such code: File reqFile = new File("test.config"); try (Stream<String> stream = Files.lines(reqFile.toPath())) { Map<String, List<String>> conf = stream.collect(Collectors.groupingBy

Java 8 Stream, How to get Top N count? [closed]

馋奶兔 提交于 2019-12-21 02:41:33
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed last year . I need your advice to simplify this code below. I have a player list with an ID of the games won. I want to extract the 2 best players from this list (the 2 players who have a better amount of match Id) Once extracted, I have to return the initial list to do other operations. I think it is possible to improve this

Java 8 Stream - Filter and foreach method not printing as expected

家住魔仙堡 提交于 2019-12-21 02:40:44
问题 I am executing the following program: Stream.of("d2", "a2", "b1", "b3", "c") .filter(s -> { System.out.println("filter: " + s); return true; }) .forEach(s -> System.out.println("forEach: " + s)); And the output I got is: filter: d2 forEach: d2 filter: a2 forEach: a2 filter: b1 forEach: b1 filter: b3 forEach: b3 filter: c forEach: c However, I was expecting the following output: filter: d2 filter: a2 filter: b1 filter: b3 filter: c forEach: d2 forEach: a2 forEach: b1 forEach: b3 forEach: c

One-to-one map with Java streams

半城伤御伤魂 提交于 2019-12-21 01:06:11
问题 Having a little trouble using the Stream API to get a one to one mapping. Basically, say you've got a class. public class Item { private final String uuid; private Item(String uuid) { this.uuid = uuid; } /** * @return universally unique identifier */ public String getUuid() { return uuid; } } I'd like a Map<String, Item> for easy look up. But given a Stream<Item> there doesn't seem a simple way to arrive at that Map<String, Item> . Obviously, Map<String, List<Item>> ain't no thing: public

Idiomatically creating a multi-value Map from a Stream in Java 8

半世苍凉 提交于 2019-12-20 23:23:08
问题 Is there any way to elegantly initialize and populate a multi-value Map<K,Collection<V>> using Java 8's stream API? I know it's possible to create a single-value Map<K, V> using the Collectors.toMap(..) functionalities: Stream<Person> persons = fetchPersons(); Map<String, Person> personsByName = persons.collect(Collectors.toMap(Person::getName, Function.identity())); Unfortunately, that method won't work well for possibly non-unique keys such as a person's name. On the other hand, it's

Idiomatically creating a multi-value Map from a Stream in Java 8

痞子三分冷 提交于 2019-12-20 23:22:08
问题 Is there any way to elegantly initialize and populate a multi-value Map<K,Collection<V>> using Java 8's stream API? I know it's possible to create a single-value Map<K, V> using the Collectors.toMap(..) functionalities: Stream<Person> persons = fetchPersons(); Map<String, Person> personsByName = persons.collect(Collectors.toMap(Person::getName, Function.identity())); Unfortunately, that method won't work well for possibly non-unique keys such as a person's name. On the other hand, it's

Java 8 Applying stream filter based on a condition

心不动则不痛 提交于 2019-12-20 18:26:47
问题 In Java 8, is there a way to apply the filter on a stream based on a condition, example I have this stream if (isAccessDisplayEnabled) { src = (List < Source > ) sourceMeta.getAllSources.parallelStream() .filter(k - > isAccessDisplayEnabled((Source) k)) .filter(k - > containsAll((Source) k, substrings, searchString)) .collect(Collectors.toList()); } else { src = (List < Source > ) sourceMeta.getAllSources.parallelStream() .filter(k - > containsAll((Source) k, substrings, searchString))

Java 8 Applying stream filter based on a condition

≡放荡痞女 提交于 2019-12-20 18:25:40
问题 In Java 8, is there a way to apply the filter on a stream based on a condition, example I have this stream if (isAccessDisplayEnabled) { src = (List < Source > ) sourceMeta.getAllSources.parallelStream() .filter(k - > isAccessDisplayEnabled((Source) k)) .filter(k - > containsAll((Source) k, substrings, searchString)) .collect(Collectors.toList()); } else { src = (List < Source > ) sourceMeta.getAllSources.parallelStream() .filter(k - > containsAll((Source) k, substrings, searchString))

Java 8 IntStream for an int range?

送分小仙女□ 提交于 2019-12-20 17:39:26
问题 Is there a way to create an IntStream for a range of ints? Like if I wanted to stream values 1 to 1000, I could invoke some IntStream static factory to stream that range? IntStream.forRange(1, 1000).forEach(//do something... 回答1: Never mind, I don't know why I missed it in the API documentation after reading it several times... IntStream.range(1,1000) 来源: https://stackoverflow.com/questions/28313726/java-8-intstream-for-an-int-range