collectors

Stream.skip behavior with unordered terminal operation

杀马特。学长 韩版系。学妹 提交于 2019-11-27 04:02:58
I've already read this and this questions, but still doubt whether the observed behavior of Stream.skip was intended by JDK authors. Let's have simple input of numbers 1..20: List<Integer> input = IntStream.rangeClosed(1, 20).boxed().collect(Collectors.toList()); Now let's create a parallel stream, combine the unordered() with skip() in different ways and collect the result: System.out.println("skip-skip-unordered-toList: " + input.parallelStream().filter(x -> x > 0) .skip(1) .skip(1) .unordered() .collect(Collectors.toList())); System.out.println("skip-unordered-skip-toList: " + input

Splitting List into sublists along elements

橙三吉。 提交于 2019-11-27 00:04:55
I have this list ( List<String> ): ["a", "b", null, "c", null, "d", "e"] And I'd like something like this: [["a", "b"], ["c"], ["d", "e"]] In other words I want to split my list in sublists using the null value as separator, in order to obtain a list of lists ( List<List<String>> ). I'm looking for a Java 8 solution. I've tried with Collectors.partitioningBy but I'm not sure it is what I'm looking for. Thanks! Alexis C. The only solution I come up with for the moment is by implementing your own custom collector. Before reading the solution, I want to add a few notes about this. I took this

What is the difference between Collectors.toConcurrentMap and converting a Map to ConcurrentHashMap via Collectors.toMap supplier option?

£可爱£侵袭症+ 提交于 2019-11-26 21:39:14
问题 I want to convert a Map into a ConcurrentHashMap via Java 8 Stream and Collector interface, and there are two options I can use. The first: Map<Integer, String> mb = persons.stream() .collect(Collectors.toMap( p -> p.age, p -> p.name, (name1, name2) -> name1+";"+name2, ConcurrentHashMap::new)); And the second: Map<Integer, String> mb1 = persons.stream() .collect(Collectors.toConcurrentMap( p -> p.age, p -> p.name)); Which one is the better option? When should I use each option? 回答1: There is

How to add elements of a Java8 stream into an existing List

本小妞迷上赌 提交于 2019-11-26 18:52:01
问题 Javadoc of Collector shows how to collect elements of a stream into a new List. Is there an one-liner that adds the results into an existing ArrayList? 回答1: NOTE: nosid's answer shows how to add to an existing collection using forEachOrdered() . This is a useful and effective technique for mutating existing collections. My answer addresses why you shouldn't use a Collector to mutate an existing collection. The short answer is no , at least, not in general, you shouldn't use a Collector to

Collectors.toMap with same keys(print same key)

一曲冷凌霜 提交于 2019-11-26 17:15:08
问题 I have this code to get map: List<MyObject> myList = myMethod.getList(); myList.stream().collect( Collectors.toMap( MyObject::getKey, MyObject::getValue, (e1, e2) -> { System.out.println("Duplicate keys !!!"); return e1; }, LinkedHashMap::new ) ); how i can print message "Duplicate keys" with duplicate key? 回答1: how i can print message "Duplicate keys" with duplicate key? With your current code, you will get the message "Duplicate keys" with a list of MyObject which contains at least 2

What kind of List<E> does Collectors.toList() return?

落花浮王杯 提交于 2019-11-26 15:46:56
问题 I am reading State of the Lambda: Libraries Edition, and am being surprised by one statement: Under the section Streams , there is the following: List<Shape> blue = shapes.stream() .filter(s -> s.getColor() == BLUE) .collect(Collectors.toList()); The document does not state what shapes actually is, and I do not know if it even matters. What confuses me is the following: What kind of concrete List does this block of code return? It assigns the variable to a List<Shape> , which is completely

Java8: HashMap<X, Y> to HashMap<X, Z> using Stream / Map-Reduce / Collector

独自空忆成欢 提交于 2019-11-26 15:12:06
问题 I know how to "transform" a simple Java List from Y -> Z , i.e.: List<String> x; List<Integer> y = x.stream() .map(s -> Integer.parseInt(s)) .collect(Collectors.toList()); Now I'd like to do basically the same with a Map, i.e.: INPUT: { "key1" -> "41", // "41" and "42" "key2" -> "42 // are Strings } OUTPUT: { "key1" -> 41, // 41 and 42 "key2" -> 42 // are Integers } The solution should not be limited to String -> Integer . Just like in the List example above, I'd like to call any method (or

Why does Collectors.toMap report value instead of key on Duplicate Key error?

核能气质少年 提交于 2019-11-26 14:44:35
问题 This is really a question about a minor detail, but I'm under the impression to get something wrong here. If you add duplicate keys using Collectors.toMap-method it throws an Exception with message "duplicate key ". Why is the value reported and not the key? Or even both? This is really misleading, isn't it? Here's a little test to demonstrate the behaviour: import java.util.Arrays; import java.util.List; import java.util.Map; import java.util.stream.Collectors; public class TestToMap {

Java 8 grouping using custom collector?

女生的网名这么多〃 提交于 2019-11-26 11:30:11
问题 I have the following class. class Person { String name; LocalDate birthday; Sex gender; String emailAddress; public int getAge() { return birthday.until(IsoChronology.INSTANCE.dateNow()).getYears(); } public String getName() { return name; } } I\'d like to be able to group by age and then collect the list of the persons names rather than the Person object itself; all in a single nice lamba expression. To simplify all of this I am linking my current solution that store the result of the

Splitting List into sublists along elements

南楼画角 提交于 2019-11-26 09:17:27
问题 I have this list ( List<String> ): [\"a\", \"b\", null, \"c\", null, \"d\", \"e\"] And I\'d like something like this: [[\"a\", \"b\"], [\"c\"], [\"d\", \"e\"]] In other words I want to split my list in sublists using the null value as separator, in order to obtain a list of lists ( List<List<String>> ). I\'m looking for a Java 8 solution. I\'ve tried with Collectors.partitioningBy but I\'m not sure it is what I\'m looking for. Thanks! 回答1: The only solution I come up with for the moment is by