java-stream

Why does this parallel stream run 15x SLOWER? [duplicate]

风格不统一 提交于 2019-12-10 10:23:08
问题 This question already has answers here : Java 8's streams: why parallel stream is slower? (4 answers) How do I write a correct micro-benchmark in Java? (11 answers) Why is parallel stream slower? (3 answers) Closed 2 years ago . I was attempting to demonstrate to a Java novice streams and such. He said he had read that streams can be slower than for loops for primitive types. So, I set immediately to prove that wrong! What I got was a shock! The for-loop and the serial stream run roughly the

Java 8 Streams map API - interpretation of method reference

一笑奈何 提交于 2019-12-10 10:16:49
问题 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

Java 8 Streams find element and add it to the start of the new List

落爺英雄遲暮 提交于 2019-12-10 10:07:04
问题 I am wondering if this is possible to solve this problem with one line using Java Streams : i need to find a String in a list of strings and create another list where the search string (if found) will be the first string on the new List and then the rest of the String values For Eg: List<String> oldList = Arrays.asList("Avacado", "Apple", "Orange", "Chocolate"); So now if if search using say a filter "Chocolate" its should return a new list which will contain the elements in order "Chocolate"

Java 8 streams - How to extract all objects inside a map of maps to a new map?

不想你离开。 提交于 2019-12-10 10:04:59
问题 I have a map of maps siteId -> (AppName -> App) I want to iterate all of the Apps in the inner map and create a new map of (appId -> App) I do it without stream Map<String, App> result = new HashMap<>(); siteIdToAppNameToAppMap.forEach((siteId, map) -> map.forEach((appName, app) -> result.put(app.token, app) ) ); How do I do it with stream? 回答1: What about something like this? siteIdToAppNameToAppMap.values() .stream() .flatMap(m -> m.values().stream()) .collect( Collectors.toMap(App:

Including elements from unmodified lists in modified lists in a 2D list with Streams

浪子不回头ぞ 提交于 2019-12-10 09:53:44
问题 I asked this question about doing the same thing with 1D lists, figuring that it would be easy to apply the answers to 2D lists, but it was not. Basing my code off of Holger's answer, I came up with this solution: //list is of type ArrayList<List<Integer>> list.stream() .map(l -> Stream.concat( l.subList(0, l.size() - 1).stream().map(i -> i - 2), Stream.of(l.get(l.size() - 1)).collect(Collectors.toList()) )) .collect(Collectors.toList()); I get the compile-time error Cannot infer type

Excluding extrema from HashSet with a Stream

无人久伴 提交于 2019-12-10 06:55:01
问题 I've been experimenting with Java 8 streams, is this is best way to remove the min and max scores. private final Set<MatchScore> scores = new HashSet<>(10); . . . public double OPR() { return scores.stream() .mapToDouble(MatchScore::getScore) .filter((num) -> { //Exclude min and max score return num != scores.stream() .mapToDouble(MatchScore::getScore) .max().getAsDouble() && num != scores.stream() .mapToDouble(MatchScore::getScore) .min().getAsDouble(); }) .average().getAsDouble(); } 回答1: A

Is use of AtomicInteger for indexing in Stream a legit way?

余生长醉 提交于 2019-12-10 06:18:25
问题 I would like to get an answer pointing out the reasons why the following idea described below on a very simple example is commonly considered bad and know its weaknesses. I have a sentence of words and my goal is to make every second one to uppercase. My starting point for both of the cases is exactly the same: String sentence = "Hi, this is just a simple short sentence"; String[] split = sentence.split(" "); The traditional and procedural approach is: StringBuilder stringBuilder = new

Java stream limit and skip behaviour when unordered and parallel

你离开我真会死。 提交于 2019-12-10 05:23:54
问题 So I have been playing around with running streams in parallel and monitoring their behaviour based on the API documentation and other supporting material I have read. I create two parallel streams and run distinct() , one where the stream is ordered and one where it is unordered. I then print the results using forEachOrdered() (to ensure I see the resulting encounter order of the stream after distinct has run), and can clearly see that the unordered version does not maintain the original

Java 8 - Calling a multi argument method from Collection.stream.map()

走远了吗. 提交于 2019-12-10 04:13:37
问题 I've been using the java 8 Streams for a while. I came across a situation where I need to stream through a List and pass each element to a static method along with another argument. Is it possible in java 8? ........ String designation = "Engineer"; List<String> names = new ArrayList<>(); names.add("ABC"); names.add("DEF"); names.add("GHI"); names.stream().map(MyClass::createReport); .......... class MyClass { public static void createReport(String name, String designation) { System.out

How to store enum to map using Java 8 stream API

别来无恙 提交于 2019-12-10 04:10:02
问题 I have an enum with another enum as a parameter public enum MyEntity{ Entity1(EntityType.type1, .... MyEntity(EntityType type){ this.entityType = entityType; } } I want to create a method that return the enum by type public MyEntity getEntityTypeInfo(EntityType entityType) { return lookup.get(entityType); } usually I would have written private static final Map<EntityType, EntityTypeInfo> lookup = new HashMap<>(); static { for (MyEntity d : MyEntity.values()){ lookup.put(d.getEntityType(), d);