java-stream

Java - Merge objects of list given a condition

白昼怎懂夜的黑 提交于 2020-06-23 08:11:08
问题 I uttlerly convinced that my question its quite simple but im unable to do it with streams (if there is a way to do it without stream will be helpfull too) Suppose that we have this list of users public class Users { String firstName; String lastName; double accountBalance; String type; String extraField; } and suppose that we have the following data in my List < Users > "Users": [{ "firstName": "Scott", "lastName": "Salisbury", "accountBalance": "100", "type" : "A" }, { "firstName": "John",

Java 8 parallel stream of list of method param

て烟熏妆下的殇ゞ 提交于 2020-06-23 04:16:49
问题 I have a method: invokList(List<Object> list); This method is inside a jar and I have no access to the source code of it. So for that, I need to execute the invokList in a parallel way, can someone help for this? The idea is to split the list to many lists and execute invokList in parallel. I have made this example: import java.util.Arrays; import java.util.Collections; import java.util.List; public class Test { public static void main(String[] args) { List<Integer> list = Arrays.asList(1,2,3

Filtering a list using Java 8 lambda expressions

非 Y 不嫁゛ 提交于 2020-06-22 15:28:12
问题 I have a Project class: class Project { List<Name> names; int year; public List<Name> getNames(){ return names; } } Then I have another main function where I have a List<Project> and have to filter that list of projects on the basis of year and get names list as the result. Can you please tell me how to do it using java 8 lambda expressions? Thanks 回答1: Well, you didn't state the exact filtering condition, but assuming you wish to filter elements by a given year: List<Name> names = projects

Interleave elements in a stream with separator

穿精又带淫゛_ 提交于 2020-06-22 13:46:26
问题 Is there a nice way to use Java streams to interleave elements in a stream with a separator of the same type? // Expected result in is list: [1, 0, 2, 0, 3] List<Integer> is = Stream.of(1, 2, 3).intersperse(0).collect(toList()); This is similar to the intersperse function in Haskell and other functional languages. I have seen many examples of how to join strings in a similar way but have not found any solutions for general lists. 回答1: You can do it with flatMap, but you'll get an additional

Interleave elements in a stream with separator

谁说胖子不能爱 提交于 2020-06-22 13:46:11
问题 Is there a nice way to use Java streams to interleave elements in a stream with a separator of the same type? // Expected result in is list: [1, 0, 2, 0, 3] List<Integer> is = Stream.of(1, 2, 3).intersperse(0).collect(toList()); This is similar to the intersperse function in Haskell and other functional languages. I have seen many examples of how to join strings in a similar way but have not found any solutions for general lists. 回答1: You can do it with flatMap, but you'll get an additional

Cannot return Stream of subclass objects after filtering

橙三吉。 提交于 2020-06-22 13:07:28
问题 I have a method that returns a Stream of type A . I also have a subtype B of A . The method creates a Stream that it populates with instances of B , which are also of type A due to inheritance. This works fine, until I introduce a filter on the Stream . Then, the compiler decides that the Stream is of type B and not A , and inheritance does not seem to matter. Why does this happen? Here is a minimal reproducible example: import java.util.ArrayList; import java.util.List; import java.util

Stream of maps to map

こ雲淡風輕ζ 提交于 2020-06-22 11:11:36
问题 How can I flatten a Stream of Map s (of the same types) to a single Map in Java 8? Map<String, Long> toMap(Stream<Map<String, Long>> stream) { return stream. ??? } 回答1: My syntax may be a bit off, but flatMap should do most of the work for you : Map<String, Long> toMap(Stream<Map<String, Long>> stream) { return stream.flatMap (map -> map.entrySet().stream()) // this would create a flattened // Stream of all the map entries .collect(Collectors.toMap(e -> e.getKey(), e -> e.getValue())); //

Java streams - peek and skip

拜拜、爱过 提交于 2020-06-18 02:45:07
问题 I'm studying for an exam and I am confused over peek. Demo code: Stream.of(1, 2, 3, 4, 5, 6, 7, 8, 9) .peek(x -> System.out.print("A" + x)) .skip(6) .peek(x -> System.out.print("B" + x)) .forEach(x -> System.out.println("C" + x)); Output: A1A2A3A4A5A6A7B7C7 A8B8C8 A9B9C9 Can someone explain what's happening here? All I know is that skip(6) skips the first 6 elements and peek will print the values of the stream at that given moment. 回答1: I believe that example is unnecessarily obtuse. Here's

Java streams - peek and skip

一曲冷凌霜 提交于 2020-06-18 02:44:05
问题 I'm studying for an exam and I am confused over peek. Demo code: Stream.of(1, 2, 3, 4, 5, 6, 7, 8, 9) .peek(x -> System.out.print("A" + x)) .skip(6) .peek(x -> System.out.print("B" + x)) .forEach(x -> System.out.println("C" + x)); Output: A1A2A3A4A5A6A7B7C7 A8B8C8 A9B9C9 Can someone explain what's happening here? All I know is that skip(6) skips the first 6 elements and peek will print the values of the stream at that given moment. 回答1: I believe that example is unnecessarily obtuse. Here's

Java streams - peek and skip

冷暖自知 提交于 2020-06-18 02:44:00
问题 I'm studying for an exam and I am confused over peek. Demo code: Stream.of(1, 2, 3, 4, 5, 6, 7, 8, 9) .peek(x -> System.out.print("A" + x)) .skip(6) .peek(x -> System.out.print("B" + x)) .forEach(x -> System.out.println("C" + x)); Output: A1A2A3A4A5A6A7B7C7 A8B8C8 A9B9C9 Can someone explain what's happening here? All I know is that skip(6) skips the first 6 elements and peek will print the values of the stream at that given moment. 回答1: I believe that example is unnecessarily obtuse. Here's