collectors

Java 8 list to nested map

假装没事ソ 提交于 2020-08-02 07:10:00
问题 I hava a list of Class A like class A { private Integer keyA; private Integer keyB; private String text; } I want to transfer aList to nested Map mapped by keyA and keyB So I create below code. Map<Integer, Map<Integer,List<A>>> aMappedByKeyAAndKeyB = aList.stream() .collect(Collectors.collectingAndThen(Collectors.groupingBy(A::getKeyA), result -> { Map<Integer, Map<Integer, List<A>>> nestedMap = new HashMap<Integer, Map<Integer, List<A>>>(); result.entrySet().stream().forEach(e -> {nestedMap

Java 8 list to nested map

只愿长相守 提交于 2020-08-02 07:04:14
问题 I hava a list of Class A like class A { private Integer keyA; private Integer keyB; private String text; } I want to transfer aList to nested Map mapped by keyA and keyB So I create below code. Map<Integer, Map<Integer,List<A>>> aMappedByKeyAAndKeyB = aList.stream() .collect(Collectors.collectingAndThen(Collectors.groupingBy(A::getKeyA), result -> { Map<Integer, Map<Integer, List<A>>> nestedMap = new HashMap<Integer, Map<Integer, List<A>>>(); result.entrySet().stream().forEach(e -> {nestedMap

How to convert List<V> into Map<K, List<V>>, with Java 8 streams and custom List and Map suppliers?

你。 提交于 2020-07-28 06:21:49
问题 It's easy to convert List<V> into Map<K, List<V>> . For example: public Map<Integer, List<String>> getMap(List<String> strings) { return strings.stream() .collect(Collectors.groupingBy(String::length)); } But I want to do it with my own List and Map suppliers . I have come up with this: public Map<Integer, List<String>> getMap(List<String> strings) { return strings.stream() .collect(Collectors.toMap( String::length, item -> {List<String> list = new ArrayList<>(); list.add(item); return list;}

How to convert List<V> into Map<K, List<V>>, with Java 8 streams and custom List and Map suppliers?

倖福魔咒の 提交于 2020-07-28 06:21:05
问题 It's easy to convert List<V> into Map<K, List<V>> . For example: public Map<Integer, List<String>> getMap(List<String> strings) { return strings.stream() .collect(Collectors.groupingBy(String::length)); } But I want to do it with my own List and Map suppliers . I have come up with this: public Map<Integer, List<String>> getMap(List<String> strings) { return strings.stream() .collect(Collectors.toMap( String::length, item -> {List<String> list = new ArrayList<>(); list.add(item); return list;}

Can the list returned by a Java streams collector be made unmodifiable? [duplicate]

时光怂恿深爱的人放手 提交于 2020-06-17 03:47:07
问题 This question already has answers here : Creating an immutable list from an existing list using streams (2 answers) Closed 2 months ago . When working with Java streams, we can use a collector to produce a collection such as a stream. For example, here we make a stream of the Month enum objects, and for each one generate a String holding the localized name of the month. We collect the results into a List of type String by calling Collectors.toList(). List < String > monthNames = Arrays

Creating an immutable list from an existing list using streams

泪湿孤枕 提交于 2020-06-08 07:08:49
问题 There is a list of Person objects. List<Person> persons = generatePersons(); An unmodifiableList is created with it. List<Person> unmodifiableList = Collections.unmodifiableList(persons); I understand that unmodifiableList doesn't support add/remove/set operations. At the same time it is not immutable since it has a reference to an existing modifiable list persons and whenever changes are made to the persons list, the changes are reflected in unmodifiableList too. An immutable list is created

how to use java stream to group fields and create a summary

不想你离开。 提交于 2020-05-13 11:48:07
问题 public class Call { private String status; private String callName; } I have a list of calls and i have to create a summary, like this: public class CallSummary { private String callName; private List<ItemSummary> items; } public class itemSummary { private String status; private Integer percentage; } My goal is show a percentage of calls with some status like : INBOUND_CALL : { FAILED = 30% SUCCESS = 70% } how can i do it using java 8 stream and Collectors ? 回答1: The idea behind the grouping

Custom Collector to join stream on delimiter, suffix and prefix only if stream is not empty suffix

霸气de小男生 提交于 2020-03-05 05:40:15
问题 I have a stream of strings: Stream<String> stream = ...; I want to create a string using stream.collect(Collectors.joining(',', '[', ']')) only I want to return "No Strings" if the stream does not contain any elements. I notice that String java.util.stream.Stream.collect(Collector<? super String, ?, String> collector) method takes an argument of type java.util.stream.Collector For my project I need to this functionality in many places so I would need a class the implements the Collector