collectors

How would you make a java.util.stream.Collector that collects to com.google.common.collect.ImmutableSet?

谁都会走 提交于 2019-12-08 19:51:42
问题 That seems too tricky for me since ImmutableSet instances are only built with ImmutableSet.Builder instances, which don't implement Collection so you can't just use Collectors.toCollection(ImmutableSet::new) or Collectors.toCollection(ImmutableSet.Builder::new) . 回答1: This is built into guava now, ImmutableSet#toImmutableSet Use like, something.stream().collect(ImmutableSet.toImmutableSet()) 回答2: In fact, 3 months after :-), instead of defining a whole class for this, you can use Collector.of

Does Java groupingBy collector preserve list order?

自闭症网瘾萝莉.ら 提交于 2019-12-08 16:55:24
问题 Consider a list List<People> where the elements are sorted in ascending order of People.getAge() . If we group this list using Collectors.groupingBy(People::getCity) , would the resultant lists for each of the groups/cities remain sorted on age? In practice, it does seem to preserve the order. I'm looking for a guarantee. The Javadoc for the method says: If preservation of the order in which elements appear in the resulting Map collector is not required, using groupingByConcurrent(Function)

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

与世无争的帅哥 提交于 2019-12-08 10:11:54
问题 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

Java groupingBy: sum multiple fields

南笙酒味 提交于 2019-12-07 12:32:18
问题 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) 回答1: @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

Why doesn't Collectors.toList() work on primitive collections?

牧云@^-^@ 提交于 2019-12-07 00:01:05
问题 (This is probably related to https://stackoverflow.com/a/30312177/160137, but I'm afraid I still don't get it. So I'm asking my question this way, in the hope that it'll lead to an answer I can more easily understand.) Normally when I have a Stream I can convert it to a collection using one of the static methods in the Collectors class: List<String> strings = Stream.of("this", "is", "a", "list", "of", "strings") .collect(Collectors.toList()); The similar process doesn't work on primitive

Filter on map of map

江枫思渺然 提交于 2019-12-06 09:49:40
I have below map of map and want to filter it based on a value. The result should be assigned back to same map. Please let know what is the best approach for this. Map<String, Map<String, Employee>> employeeMap; < dep1, <"empid11", employee11> <"empid12",employee12> dep2, <"empid21", employee21> <"empid22",employee22> > Filter: employee.getState="MI" I tried like below but i was not able to access the employee object currentMap = currentMap.entrySet().stream() **.filter(p->p.getValue().getState().equals("MI"))** .collect(Collectors.toMap(p -> p.getKey(),p->p.getValue())); If you want to modify

Elegantly create map with object fields as key/value from object stream in Java 8

女生的网名这么多〃 提交于 2019-12-06 08:25:32
问题 I have the following class class Person { public String name; public int age; public List<String> hobbies; Person(String name, int age, List<String> hobbies) {this.name = name; this.age = age; this.hobbies = hobbies;} } How do I create a Map of age to hobbies like Map<Integer, Set<String>> ? The Java 8 way I cooked up is: Map<Integer, Set<String>> collect8 = persons.stream() .collect( toMap( p -> p.age, p -> p.hobbies.stream().collect(toSet()), (hobbies1, hobbies2) -> Stream.concat(hobbies1

Group, Sum byType then get diff using Java streams

大憨熊 提交于 2019-12-06 07:04:24
问题 I would like to use stream to group, get the sum by type, then find the result of the different by type. So this is my data set. Sample(SampleId=1, SampleTypeId=1, SampleQuantity=5, SampleType=ADD), Sample(SampleId=2, SampleTypeId=1, SampleQuantity=15, SampleType=ADD), Sample(SampleId=3, SampleTypeId=1, SampleQuantity=25, SampleType=ADD), Sample(SampleId=4, SampleTypeId=1, SampleQuantity=5, SampleType=SUBTRACT), Sample(SampleId=5, SampleTypeId=1, SampleQuantity=25, SampleType=SUBTRACT) Sample

An elegant way to specify initial capacity of Collector in java stream api

寵の児 提交于 2019-12-06 01:39:06
问题 I've tried to find a good way to set up initial capacity of collector in java stream api. The simplest example is there: data.stream() .collect(Collectors.toList()); I just want to pass an int with size of list into collector in order not to resize internal array. The first intention is to do it in such way: data.stream() .collect(Collectors.toList(data.size())); But unfortunately toList isn't overloaded to work with parameter. I found one solution but it smells: data.stream() .collect

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());