collectors

How collectors are used when turning the stream in parallel

我怕爱的太早我们不能终老 提交于 2019-12-03 12:26:00
I actually tried to answer this question How to skip even lines of a Stream<String> obtained from the Files.lines . So I though this collector wouldn't work well in parallel: private static Collector<String, ?, List<String>> oddLines() { int[] counter = {1}; return Collector.of(ArrayList::new, (l, line) -> { if (counter[0] % 2 == 1) l.add(line); counter[0]++; }, (l1, l2) -> { l1.addAll(l2); return l1; }); } but it works. EDIT: It didn't actually work; I got fooled by the fact that my input set was too small to trigger any parallelism; see discussion in comments . I thought it wouldn't work

How to get a List<E> from a HashMap<String,List<E>>

余生长醉 提交于 2019-12-03 10:50:17
问题 I want to extract a List<E> from a Map<String,List<E>> ( E is a random Class) using stream() . I want a simple one-line method using java 8's stream. What I have tried until now : HashMap<String,List<E>> map = new HashMap<>(); List<E> list = map.values(); // does not compile list = map.values().stream().collect(Collectors.toList()); // does not compile 回答1: map.values() returns a Collection<List<E>> not a List<E> , if you want the latter then you're required to flatten the nested List<E> into

Java 8 streams group by 3 fields and aggregate by sum and count produce single line output

僤鯓⒐⒋嵵緔 提交于 2019-12-03 09:49:45
I know there a similar questions asked in the forum but none of them seem to be addressing my problem fully. Now I'm very new to Java 8, so please bear with me. I have a list of Products, for example: Input: name category type cost prod1 cat2 t1 100.23 prod2 cat1 t2 50.23 prod1 cat1 t3 200.23 prod3 cat2 t1 150.23 prod1 cat2 t1 100.23 Output: Single line (name, category, type) summing the cost and count of products. Product { public String name; public String category; public String type; public int id; public double cost; } I need to group this by name, category and type and produce a single

Stream groupingBy: reducing to first element of list

烈酒焚心 提交于 2019-12-03 08:22:11
问题 I have a List<Valuta> which can be represented (simplified) JSON-style: [ { codice=EUR, description=Euro, ratio=1 }, { codice=USD, description=Dollars, ratio=1.1 } ] I want to transform that in a Map<String, Valuta> like this: { EUR={ codice=EUR, description=Euro, ratio=1 }, USD={ codice=USD, description=Dollars, ratio=1.1 }} I wrote this one-liner: getValute().stream().collect(Collectors.groupingBy(Valuta::getCodice)); but this returns a Map<String, List<Valuta>> instead of what I need. I

Java 8 Stream API toMap converting to TreeMap

扶醉桌前 提交于 2019-12-03 07:14:33
public class Message { private int id; private User sender; private User receiver; private String text; private Date senddate; .. } I have List<Message> list= new ArrayList<>(); I need to transform them to TreeMap<User,List<Message>> map I know how to do transform to HashMap using list.stream().collect(Collectors.groupingBy(Message::getSender)); But I need TreeMap with: Key - User with newest message senddate first Value - List sorted by senddate newest first Part of User class public class User{ ... private List<Message> sendMessages; ... public List<Message> getSendMessages() { return

Hashmap with Streams in Java 8 Streams to collect value of Map

老子叫甜甜 提交于 2019-12-03 05:27:53
问题 Let consider a hashmap Map<Integer, List> id1 = new HashMap<Integer,List>(); I inserted some values into both hashmap. For Example, List<String> list1 = new ArrayList<String>(); list1.add("r1"); list1.add("r4"); List<String> list2 = new ArrayList<String>(); list2.add("r2"); list2.add("r5"); List<String> list3 = new ArrayList<String>(); list3.add("r3"); list3.add("r6"); id1.put(1,list1); id1.put(2,list2); id1.put(3,list3); id1.put(10,list2); id1.put(15,list3); Q1) Now I want to apply a filter

Collectors.toSet() and HashSet

天大地大妈咪最大 提交于 2019-12-03 03:23:45
问题 Take the following line of sample code: Set<String> someSet = someColletion.stream().map(p -> p.toString()).collect(Collectors.toSet()); I want a HashSet . Taking a debugger to the code, I am indeed getting a HashSet . I had a look at java.util.stream.Collectors.toSet() to observe the following code: public static <T> Collector<T, ?, Set<T>> toSet() { return new CollectorImpl<>((Supplier<Set<T>>) HashSet::new, Set::add, (left, right) -> { left.addAll(right); return left; }, CH_UNORDERED_ID);

How to get a List<E> from a HashMap<String,List<E>>

馋奶兔 提交于 2019-12-03 01:18:23
I want to extract a List<E> from a Map<String,List<E>> ( E is a random Class) using stream() . I want a simple one-line method using java 8's stream. What I have tried until now : HashMap<String,List<E>> map = new HashMap<>(); List<E> list = map.values(); // does not compile list = map.values().stream().collect(Collectors.toList()); // does not compile map.values() returns a Collection<List<E>> not a List<E> , if you want the latter then you're required to flatten the nested List<E> into a single List<E> as follows: List<E> result = map.values() .stream() .flatMap(List::stream) .collect

Hashmap with Streams in Java 8 Streams to collect value of Map

心已入冬 提交于 2019-12-02 18:43:40
Let consider a hashmap Map<Integer, List> id1 = new HashMap<Integer,List>(); I inserted some values into both hashmap. For Example, List<String> list1 = new ArrayList<String>(); list1.add("r1"); list1.add("r4"); List<String> list2 = new ArrayList<String>(); list2.add("r2"); list2.add("r5"); List<String> list3 = new ArrayList<String>(); list3.add("r3"); list3.add("r6"); id1.put(1,list1); id1.put(2,list2); id1.put(3,list3); id1.put(10,list2); id1.put(15,list3); Q1) Now I want to apply a filter condition on the key in hashmap and retrieve the corresponding value(List). Eg: Here My query is key=1,

Collectors.toSet() and HashSet

折月煮酒 提交于 2019-12-02 16:56:09
Take the following line of sample code: Set<String> someSet = someColletion.stream().map(p -> p.toString()).collect(Collectors.toSet()); I want a HashSet . Taking a debugger to the code, I am indeed getting a HashSet . I had a look at java.util.stream.Collectors.toSet() to observe the following code: public static <T> Collector<T, ?, Set<T>> toSet() { return new CollectorImpl<>((Supplier<Set<T>>) HashSet::new, Set::add, (left, right) -> { left.addAll(right); return left; }, CH_UNORDERED_ID); } The contract guarantees a Set , and implementation decides on a HashSet ; seems reasonable. However,