java-stream

Java Stream: difference between forEach and forEachOrdered

若如初见. 提交于 2019-12-06 05:06:56
Premise : I've already read this question and others, but I need some clarifications. I understand that Stream.forEach method makes the difference (not only) when dealing with parallel streams, and this explains why this //1 Stream.of("one ","two ","three ","four ","five ","six ") .parallel() .forEachOrdered(item -> System.out.print(item)); prints one two three four five six But when it comes to intermediate operations, the order is not guaranteed anymore when stream is parallelized. So this code //2 Stream.of("one ","two ","three ","four ","five ","six ") .parallel() .peek(item -> System.out

Populate a map conditionally using streams - Java 8

♀尐吖头ヾ 提交于 2019-12-06 04:46:40
问题 I'm trying to translate this (simplified) code to use Java-8 streams: Map<String, String> files = new ConcurrentHashMap<String, String>(); while(((line = reader.readLine()) != null) { if(content != null) files.put("not null"+line, "not null"+line); else files.put("its null"+line, "its null"+line); } reader.close(); Here is what I've tried: files = reader.lines().parallel().collect((content != null)? (Collectors.toConcurrentMap(line->"notnull"+line, line->line+"notnull")) : (Collectors

Logging inside Stream

你离开我真会死。 提交于 2019-12-06 04:44:42
问题 I am trying to convert this piece of code to Java 8 stream: if(list!=null && list.size()>0){ Actor actor = null; for(Actor actor:list){ log.info("Actor being read: " actor.getCode()); List areaList = areaDAO.getArea(actor.getCode()); if (areaList.size() > 0) { actor.setArea((String) areaList.get(0)); log.info("Area{" + areaList.get(0) + "} is fetched for actor{" + actor.getCode() + "}."); } this.getContext().setReadCount(1); } } However I am not sure how to deal with logging in this case? Is

Java streams to map

社会主义新天地 提交于 2019-12-06 04:30:01
I'm having a problem with the Map<Integer, List<String>> personenPerLeeftijd , the compiler says the method, Persoon::getLeeftijd, cannot be resolved. I don't really know what else I can do, sorry for the Dutch words! I you need any more info pls ask so public class TestPersoon2 { public static void main(String[] args) { final List<Persoon> personen = Personen.getPersonen(); Map<String, Persoon> map = personen.stream().collect(Collectors.toMap(p -> p.getNaam() + "-" + p.getLeeftijd(), p -> p)); for (String s : map.keySet()) { System.out.printf("%-15s -> %s\n", s, map.get(s)); } Map<Integer,

Performing more than one reduction in a single pass

蓝咒 提交于 2019-12-06 04:16:08
问题 What is the idiom for performing more than one reduction in a single pass of a stream? Is it just to have one big reducer class, even if this violates SRP if more than one type of reduction computation is required? 回答1: Presumably you want to avoid making multiple passes, as the pipeline stages might be expensive. Or you want to avoid collecting the intermediate values in order to run them through multiple collectors, since the cost of storing all the values might be too high. As Brian Goetz

How to include elements from unmodified list in modified list with Java Streams?

我与影子孤独终老i 提交于 2019-12-06 04:08:58
I am trying to do a map operation on a List<Integer> : list.stream().map(i -> i - 2).collect(Collectors.toList()); Instead of performing the operation on the last element of the list, I would like it to just be passed through. ...Collectors.toList()).add(i) doesn't work, of course, because i is out of scope. For example, the input list [1, 2, 3, 4] should output [-1, 0, 1, 4] You could stream the original list and limit the stream to exclude the last element, do the mapping and collect to a new ArrayList , then add the last element of the original list to the last position of the new, mutable

Java 8 Streams map API - interpretation of method reference

不羁的心 提交于 2019-12-06 04:05:51
Sample code: class Outer { public Integer i; Outer(Integer i) { this.i = i; } public int getVal() { return i; } } class MyClass { public Integer f(Outer o) { return o.getVal();}; public void main() { MyClass g = new MyClass(); List<Integer> l1 = Arrays.asList(new Outer(2)).stream().map(g::f).collect(Collectors.toList()); List<Integer> l2 = Arrays.asList(new Outer(2)).stream().map(Outer::getVal).collect(Collectors.toList()); } } Using either of the method references of Outer::instanceMethod that takes no argument and is basically a Supplier<T> functional interface. [1] MyClass::instanceMethod

Divide LongStream into substreams with maximal length

…衆ロ難τιáo~ 提交于 2019-12-06 04:05:44
There are some SQL statements in my program that contain IN -clauses with given Ids. The problem is that in some cases there might be more than 1000 Ids which causes Oracle to crash with ORA-01795. Too many items. So I want to divide this list into multiple sub-lists. Example: I have 2403 Ids Result would be three lists: 0 - 999 1000 - 1999 2000 - 2402 I have written a piece of code that works, but looks terrible. Is there any better solution for this problem? Maybe something with Collectors & groupingby or anything like that? My code: Map<Integer, List<Long>> result = new HashMap<>();

Collecting stream back into the same collection type

╄→尐↘猪︶ㄣ 提交于 2019-12-06 03:59:42
问题 Suppose I have a collection of the unknown type. What I want to do is stream it, do some stuff on the stream, and collect it back into the same collection type as my original collection. For instance: Collection<? extends Integer> getBigger(Collection<? extends Integer> col, int value) { return col.stream().filter(v -> v > value).collect(????); } The idea of this incomplete code example is to return a List if col is of List class (or any subclass of it), a Set if col is of Set class, etc...

How to Group Objects in a List into other Lists by Attribute using streams & Java 8?

丶灬走出姿态 提交于 2019-12-06 03:56:06
问题 I want to group a List of Objects containing a time attribute into 5-Minute intervals, preferably using streams and collectors. The only possible solution I found on StackOverflow is to calculate how many intervals (sublists) I need, add every object to every one of these Lists and filter out the ones that dont fit into the respective timeframe, which is not exactly a nice solution. (You can find the Thread here: How to group elements of a List by elements of another in Java 8) I thought of