collectors

Confused by Java8 Collectors.toMap

假装没事ソ 提交于 2019-11-29 00:19:48
I have a collection that looks like below, and I want to filter out the everything except the dates that aren't the end of the months. 2010-01-01=2100.00, 2010-01-31=2108.74, 2010-02-01=2208.74, 2010-02-28=2217.92, 2010-03-01=2317.92, 2010-03-31=2327.57, 2010-04-01=2427.57, 2010-04-30=2437.67, 2010-05-01=2537.67, 2010-05-31=2548.22, 2010-06-01=2648.22, 2010-06-30=2659.24, 2010-07-01=2759.24, 2010-07-31=2770.72, 2010-08-01=2870.72, 2010-08-31=2882.66, 2010-09-01=2982.66, 2010-09-30=2995.07, 2010-10-01=3095.07, 2010-10-31=3107.94, 2010-11-01=3207.94, 2010-11-30=3221.29 I have the following

Java 8 Streams: Map the same object multiple times based on different properties

点点圈 提交于 2019-11-28 19:15:39
I was presented with an interesting problem by a colleague of mine and I was unable to find a neat and pretty Java 8 solution. The problem is to stream through a list of POJOs and then collect them in a map based on multiple properties - the mapping causes the POJO to occur multiple times Imagine the following POJO: private static class Customer { public String first; public String last; public Customer(String first, String last) { this.first = first; this.last = last; } public String toString() { return "Customer(" + first + " " + last + ")"; } } Set it up as a List<Customer> : // The list of

Alternative for throwingMerger in Java 8

不羁岁月 提交于 2019-11-28 12:50:45
I'm implementing own collector that uses merge function . Unfortunately, for some of my cases, I can't reuse the following JDK merger function that thrown IllegalStateException . java.util.stream.Collectors#throwingMerger It happens due to the fact that it has private access modifier and access from other(not inner) classes is restricted. However, javadoc says the following: This can be used to enforce the assumption that the elements being collected are distinct But, as I see, java doc is out of date. It can't be used. The question is whether JDK provides access to similar functionality for

Accumulate a Java Stream and only then process it

a 夏天 提交于 2019-11-28 10:31:31
问题 I had a document that looked like the following: data.txt 100, "some text" 101, "more text" 102, "even more text" I processed it using regex and returned a new processed documents as the follow: Stream<String> lines = Files.lines(Paths.get(data.txt); Pattern regex = Pattern.compile("([\\d{1,3}]),(.*)"); List<MyClass> result = lines.map(regex::matcher) .filter(Matcher::find) .map(m -> new MyClass(m.group(1), m.group(2)) //MyClass(int id, String text) .collect(Collectors.toList()); This returns

java 8 Collector<String, A, R> is not a functional interface, who can tell why?

*爱你&永不变心* 提交于 2019-11-28 07:31:09
问题 The follow code : public class Test { public static void main(String[] args) { Stream.of(1,2,3).map(String::valueOf).collect(Collectors::toList) } } intellij tell me : Collector<String, A, R> is not a functional interface but when i modify the code as follows, everything is ok, i don't know why? public class Test { public static void main(String[] args) { Stream.of(1,2,3).map(String::valueOf).collect(Collectors.<String>toList()); } } 回答1: The reason that the first syntax is illegal is that

Is there a Collector that collects to an order-preserving Set?

主宰稳场 提交于 2019-11-28 04:26:40
Collectors.toSet() does not preserve order. I could use Lists instead, but I want to indicate that the resulting collection does not allow element duplication, which is exactly what Set interface is for. You can use toCollection and provide the concrete instance of the set you want. For example if you want to keep insertion order: Set<MyClass> set = myStream.collect(Collectors.toCollection(LinkedHashSet::new)); For example: public class Test { public static final void main(String[] args) { List<String> list = Arrays.asList("b", "c", "a"); Set<String> linkedSet = list.stream().collect

How to create a map with Java stream API using a value outside the stream?

会有一股神秘感。 提交于 2019-11-28 02:31:51
问题 I want to init a Map<String, BigDecimal> and want to always put the same BigDecimal value from outside of the stream. BigDecimal samePrice; Set<String> set; set.stream().collect(Collectors.toMap(Function.identity(), samePrice)); However Java complains as follows: The method toMap(Function, Function) in the type Collectors is not applicable for the arguments (Function, BigDecimal) Why can't I use the BigDecimal from outside? If I write: set.stream().collect(Collectors.toMap(Function.identity()

Collectors.toMap with same keys(print same key)

让人想犯罪 __ 提交于 2019-11-28 01:12:11
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? Nicolas Filotto 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 instances of MyObject that have equal objects as value for getKey() , for example Arrays.asList

How to get the key in Collectors.toMap merge function?

牧云@^-^@ 提交于 2019-11-28 00:51:09
When a duplicate key entry is found during Collectors.toMap() , the merge function (o1, o2) is called. Question: how can I get the key that caused the duplication? String keyvalp = "test=one\ntest2=two\ntest2=three"; Pattern.compile("\n") .splitAsStream(keyval) .map(entry -> entry.split("=")) .collect(Collectors.toMap( split -> split[0], split -> split[1], (o1, o2) -> { //TODO how to access the key that caused the duplicate? o1 and o2 are the values only //split[0]; //which is the key, cannot be accessed here }, HashMap::new)); Inside the merge function I want to decide based on the key which

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

混江龙づ霸主 提交于 2019-11-28 00:14:40
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? There is a difference between them when dealing with parallel streams. toMap -> is a non-concurrent collector