java-stream

Why does this parallel stream run 15x SLOWER? [duplicate]

允我心安 提交于 2019-12-05 20:07:33
This question already has answers here : Java 8's streams: why parallel stream is slower? (4 answers) How do I write a correct micro-benchmark in Java? (11 answers) Why is parallel stream slower? (3 answers) Closed 2 years ago . I was attempting to demonstrate to a Java novice streams and such. He said he had read that streams can be slower than for loops for primitive types. So, I set immediately to prove that wrong! What I got was a shock! The for-loop and the serial stream run roughly the same, but the parallel stream is consistently and dramatically slower. Can someone explain why? @Test

Including elements from unmodified lists in modified lists in a 2D list with Streams

我们两清 提交于 2019-12-05 19:15:35
I asked this question about doing the same thing with 1D lists, figuring that it would be easy to apply the answers to 2D lists, but it was not. Basing my code off of Holger's answer, I came up with this solution: //list is of type ArrayList<List<Integer>> list.stream() .map(l -> Stream.concat( l.subList(0, l.size() - 1).stream().map(i -> i - 2), Stream.of(l.get(l.size() - 1)).collect(Collectors.toList()) )) .collect(Collectors.toList()); I get the compile-time error Cannot infer type argument(s) for <R> map(Function<? super T,? extends R>) , however, when I try this. How is can I perform this

How to convert Array to HashMap using Java 8 Stream

别等时光非礼了梦想. 提交于 2019-12-05 19:07:18
问题 I am writing a function to convert array to Map using Java 8 Stream. Here is what I wanted public static <K, V> Map<K, V> toMap(Object... entries) { // Requirements: // entries must be K1, V1, K2, V2, .... ( even length ) if (entries.length % 2 == 1) { throw new IllegalArgumentException("Invalid entries"); } // TODO Arrays.stream(entries).???? } Valid usages Map<String, Integer> map1 = toMap("k1", 1, "k2", 2); Map<String, String> map2 = toMap("k1", "v1", "k2", "v2", "k3", "v3"); Invalid

Union query with multiple selects post java 8

早过忘川 提交于 2019-12-05 18:48:14
Here is a query that I want to try out in MySQL SELECT A.x FROM A WHERE A.y = 'P' UNION SELECT A.x FROM A WHERE A.y = 'Q' The above is a cut-down, much simpler version of the original query that I am trying. In my original query, each SELECT statement involves multiple tables with INNER JOIN If the possible number of values in 'y' column of table 'A' that I need to query upon is 'n', then my query will involve doing 'n-1' unions on 'n' SELECT statements I know that JOOQ can do union of multiple SELECT statements. But is there a good way to do this post Java 8 style? maybe using Steam.collect()

Create a sorted Set while using streams

≡放荡痞女 提交于 2019-12-05 18:42:39
I have a User class with name, type and age and then a long list of these users are my input List<User> users = db.getUsers(); . I am trying to create a set of all unique users from this, but the problem is I am looking for sorting them based on the age also. I have currently used- Set<User> set = users.stream().collect(Collectors.toSet()); How to sort this set at the same time as well, any idea? It doesn't make sense to speak of order within a non sorted set. You should be using something like TreeSet if you want a set ordered by age. Comparator<User> byAge = Comparator.comparingInt(User:

Java groupingBy: sum multiple fields

眉间皱痕 提交于 2019-12-05 18:29:16
This question is an extension to the post: Java 8 groupingby with returning multiple field . For the same problem, how do you return a list of Customer ? For example, it should return: Customer("A",4500,6500) Customer("B",3000,3500) Customer("C",4000,4500) @Pankaj Singhal's post is the right idea if you want a Map<String, Customer> as the result set +1. However, I would extract the merging logic into its own function e.g. in the Customer class you would have a function as such: public static Customer merge(Customer first, Customer second) { first.setTotal(first.getTotal() + second.getTotal());

Can Stream#limit return fewer elements than expected?

我怕爱的太早我们不能终老 提交于 2019-12-05 17:49:04
If the Stream s below has at least n elements, what are the situations where the stream sLimit may have less than n elements, if any? Stream sLimit = s.limit(n); Reason for the question: in this answer , I read that: Despite the appearances, using limit(10) doesn't necessarily result in a SIZED stream with exactly 10 elements -- it might have fewer. I think Holger's and Sotirios' answers are accurate, but inasmuch as I'm the guy who made the statement, I guess I should explain myself. I'm mainly talking about spliterator characteristics , in particular the SIZED characteristic. This is

Complexity of grouping in Java8

时间秒杀一切 提交于 2019-12-05 17:22:26
问题 I would like to learn the time complexity of the given statement below.(In Java8) list.stream().collect(groupingBy(...)); Any idea? 回答1: There is no general answer to that question, as the time complexity depends on all operations. Since the stream has to be processed entirely, there is a base time complexity of O(n) that has to be multiplied by the costs of all operations done per element. This, assuming that the iteration costs itself are not worse than O(n) , which is the case for most

Java stream reduce

感情迁移 提交于 2019-12-05 17:17:56
问题 I have the following example data set that I want to transform / reduce using Java stream api based on direction's value Direction int[] IN 1, 2 OUT 3, 4 OUT 5, 6, 7 IN 8 IN 9 IN 10, 11 OUT 12, 13 IN 14 to Direction int[] IN 1, 2, OUT 3, 4, 5, 6, 7 IN 8, 9, 10, 11 OUT 12, 13 IN 14 code that I've written so far enum Direction { IN, OUT } class Tuple { Direction direction; int[] data; public Tuple merge(Tuple t) { return new Tuple(direction, concat(getData(), t.getData())); } } private static

Java streams findAny() encounters null pointer exception after filter() operation filters out everything

做~自己de王妃 提交于 2019-12-05 17:14:14
问题 I am having trouble figuring why findAny() throws a null pointer exception after filter() operation on a stream. In this particular test case, the filter operation should have filtered out everything, leaving no results for findAny() . Optional<JsonNode> encryption = sseEncryptionList.stream() .filter(n -> n.textValue().equals("AES256")) //Filters out everything .findAny(); //Throws null pointer exception The stack trace: Exception in thread "main" java.lang.NullPointerException at example