collectors

Java 8 collector for Guava ImmutableTable using HashBasedTable as accumulator gives IllegalAccessError

∥☆過路亽.° 提交于 2020-02-23 05:57:32
问题 Process list of string via method which returns ImmutableTable<R,C,V> . For instance ImmutableTable<Integer,String,Boolean> process(String item) { /*...*/} . Collect the result i.e, merge all results (individual table may contain duplicates) and return ImmutableTable . My current implementation works when there are no duplicates: final ImmutableTable<Integer, String, Boolean> result = itemsToProcess.parallelStream() .map(item -> ProcessorInstanceProvider.get() .buildTable(item)) .collect

Java 8 collector for Guava ImmutableTable using HashBasedTable as accumulator gives IllegalAccessError

隐身守侯 提交于 2020-02-23 05:57:07
问题 Process list of string via method which returns ImmutableTable<R,C,V> . For instance ImmutableTable<Integer,String,Boolean> process(String item) { /*...*/} . Collect the result i.e, merge all results (individual table may contain duplicates) and return ImmutableTable . My current implementation works when there are no duplicates: final ImmutableTable<Integer, String, Boolean> result = itemsToProcess.parallelStream() .map(item -> ProcessorInstanceProvider.get() .buildTable(item)) .collect

Collect Lines using Multimap Collector

喜欢而已 提交于 2020-01-11 05:22:10
问题 Is there a way to covert the below to using collectors yet? List<String[]> lines = getLines(); Multimap<String,String> multimap = ArrayListMultimap.create(); lines.forEach(line -> multimap.put(line[0],line[1]); ); 回答1: You can use Multimaps.toMultimap collector: ListMultimap<String, String> multimap = lines.stream() .collect(Multimaps.toMultimap( l -> l[0], l -> l[1], ArrayListMultimap::create )); Or if you don't need mutability, use ImmutableListMultimap.toImmutableListMultimap collector:

How to use Java 8 Streams groupingBy to map multiple database records to a single object with a List<String> property for one column

半世苍凉 提交于 2020-01-06 01:53:56
问题 My problem essentially comes down to this simplified example. I have data coming back from a DB which has some duplicate information in the rows. In this example I have a list of TeamRow objects that come back from the DB. I can easily group these using Collectors.groupingBy : public class TeamRow { private int id; private String name; private String player; public TeamRow(int id, String name, String player) { this.id = id; this.name = name; this.player = player; } public int getId() {return

Creating Map composed of 2 Lists using stream().collect in Java

故事扮演 提交于 2020-01-02 02:11:27
问题 As for example, there are two lists: List<Double> list1 = Arrays.asList(1.0, 2.0); List<String> list2 = Arrays.asList("one_point_zero", "two_point_zero"); Using Stream, I want to create a map composed of these lists, where list1 is for keys and list2 is for values. To do it, I need to create an auxiliary list: List<Integer> list0 = Arrays.asList(0, 1); Here is the map: Map<Double, String> map2 = list0.stream() .collect(Collectors.toMap(list1::get, list2::get)); list0 is used in order list1:

How to get a custom type instead of Integer when using Collectors.summingInt?

我与影子孤独终老i 提交于 2020-01-02 01:05:14
问题 I am currently creating a Map<String, Map<LocalDate, Integer>> like this, where the Integer represents seconds: Map<String, Map<LocalDate, Integer>> map = stream.collect(Collectors.groupingBy( x -> x.getProject(), Collectors.groupingBy( x -> x.getDate(), Collectors.summingInt(t -> t.getDuration().toSecondOfDay()) ) )); How could I instead create a Map<String, Map<LocalDate, Duration>> ? 回答1: To change that Integer from Collectors.summingInt to a Duration , you simply need to replace that

Merging lists under same objects in a list using Java streams

孤街醉人 提交于 2019-12-30 10:45:10
问题 I have two objects like following: public class A { private Integer id; private String name; private List<B> list; public A(Integer id, String name, List<B> list) { this.id = id; this.name = name; this.list = list; } //getters and setters } and public class B { private Integer id; private String name; public B(Integer id, String name) { this.id = id; this.name = name; } //getters and setters } So, A holds a list of B and there is a list of A populated as follows: List<A> list = new ArrayList<

Collect a Stream of Map<K,V> to Map<K,List<V>>

左心房为你撑大大i 提交于 2019-12-30 06:35:10
问题 I have a Stream< Map< K, V > > and I'm trying to merge those maps together, but preserve duplicate values in a list, so the final type would be Map< K, List<V> > . Is there a way to do this? I know the toMap collector has a binary function to basically choose which value is returned, but can it keep track of the converted list? i.e. if a is a Stream< Map< String, Int > > a.flatMap(map -> map.entrySet().stream()).collect( Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (val1, val2) ->

Java Collectors.groupingBy()---is List ordered?

China☆狼群 提交于 2019-12-30 02:45:29
问题 For the Collectors.groupingBy() that returns Map<K,List<T>> is it implied that the List<T> is in order that the stream is evaluated? I see no explicit description of the ordering of the list, whereas the concurrent version explicitly states no ordering. If it weren't ordered somehow, I'd expect it to be a Collection though, and I don't see what other ordering it could possibly be, other than order received. I'm hoping it's guaranteed that the last value in each list is the last value received

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

独自空忆成欢 提交于 2019-12-28 08:40:02
问题 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. 回答1: 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)