java-stream

How does the reduce() method work in Java 8?

心已入冬 提交于 2019-12-18 11:22:09
问题 I try to understand how does the reduce() method work in java-8. For example I have this code: public class App { public static void main(String[] args) { String[] arr = {"lorem", "ipsum", "sit", "amet"}; List<String> strs = Arrays.asList(arr); int ijk = strs.stream().reduce(0, (a, b) -> { System.out.println("Accumulator, a = " + a + ", b = " + b); return a + b.length(); }, (a, b) -> { System.out.println("Combiner"); return a * b; }); System.out.println(ijk); } } And the output is this:

Which operations preserve order [duplicate]

泄露秘密 提交于 2019-12-18 11:13:53
问题 This question already has an answer here : How to ensure order of processing in java8 streams? (1 answer) Closed last year . TL;DR; I am looking for a place where I can lookup whether a certain intermediate or terminal operation. Where can I find such documentation? Edit This is not a duplicate of How to ensure order of processing in java8 streams?, since that question does not provide a comprehensive list of operations. Background The the package documentation says: Whether or not a stream

Can Java 8 Streams operate on an item in a collection, and then remove it?

坚强是说给别人听的谎言 提交于 2019-12-18 10:17:52
问题 Like just about everyone, I'm still learning the intricacies (and loving them) of the new Java 8 Streams API. I have a question concerning usage of streams. I'll provide a simplified example. Java Streams allows us to take a Collection , and use the stream() method on it to receive a stream of all of its elements. Within it, there are a number of useful methods, such as filter() , map() , and forEach() , which allow us to use lambda operations on the contents. I have code that looks something

Why is shared mutability bad?

 ̄綄美尐妖づ 提交于 2019-12-18 10:15:31
问题 I was watching a presentation on Java, and at one point, the lecturer said: "Mutability is OK, sharing is nice, shared mutability is devil's work." What he was referring to is the following piece of code, which he considered an "extremely bad habit": //double the even values and put that into a list. List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 1, 2, 3, 4, 5); List<Integer> doubleOfEven = new ArrayList<>(); numbers.stream() .filter(e -> e % 2 == 0) .map(e -> e * 2) .forEach(e ->

Java 8 stream unpredictable performance drop with no obvious reason

核能气质少年 提交于 2019-12-18 10:01:40
问题 I am using Java 8 streams to iterate over a list with sublists. The outer list size varies between 100 to 1000 (different test runs) and the inner list size is always 5. There are 2 benchmark runs which show unexpected performance deviations. package benchmark; import org.openjdk.jmh.annotations.*; import org.openjdk.jmh.infra.Blackhole; import java.io.IOException; import java.util.concurrent.ThreadLocalRandom; import java.util.*; import java.util.function.*; import java.util.stream.*;

Java lambda expression — mapping and then modifying a list?

左心房为你撑大大i 提交于 2019-12-18 07:02:22
问题 Using a Java 8 lambda expression, I'm trying to do something like this. List<NewObject> objs = ...; for (OldObject oldObj : oldObjects) { NewObject obj = oldObj.toNewObject(); obj.setOrange(true); objs.add(obj); } I wrote this code. oldObjects.stream() .map(old -> old.toNewObject()) .forEach({new.setOrange("true")}) .collect(Collectors.toList()); This is invalid code because I'm then trying to do .collect() on what's returned by .forEach() , but forEach is void and does not return a list. How

Java lambda expression — mapping and then modifying a list?

浪尽此生 提交于 2019-12-18 07:02:02
问题 Using a Java 8 lambda expression, I'm trying to do something like this. List<NewObject> objs = ...; for (OldObject oldObj : oldObjects) { NewObject obj = oldObj.toNewObject(); obj.setOrange(true); objs.add(obj); } I wrote this code. oldObjects.stream() .map(old -> old.toNewObject()) .forEach({new.setOrange("true")}) .collect(Collectors.toList()); This is invalid code because I'm then trying to do .collect() on what's returned by .forEach() , but forEach is void and does not return a list. How

How to skip even lines of a Stream<String> obtained from the Files.lines

时光毁灭记忆、已成空白 提交于 2019-12-18 05:50:58
问题 In this case just odd lines have meaningful data and there is no character that uniquely identifies those lines. My intention is to get something equivalent to the following example: Stream<DomainObject> res = Files.lines(src) .filter(line -> isOddLine()) .map(line -> toDomainObject(line)) Is there any “clean” way to do it, without sharing global state? 回答1: A clean way is to go one level deeper and implement a Spliterator . On this level you can control the iteration over the stream elements

What is the difference between .stream() and Stream.of?

只愿长相守 提交于 2019-12-18 05:47:24
问题 Which is the best way to create a stream out of a collection: final Collection<String> entities = someService.getArrayList(); entities.stream(); Stream.of(entities); 回答1: The second one does not do what you think it does! It does not give you a stream with the elements of the collection; instead, it will give you a stream with a single element, which is the collection itself (not its elements). If you need to have a stream containing the elements of the collection, then you must use entities

Why Comparator.comparing doesn't work with String::toLowerCase method reference?

泪湿孤枕 提交于 2019-12-18 05:47:05
问题 I am trying to sort an array of Strings by reverse order (ignoring case), without modifying it, and just printing it. So I am using Java8 stream. But I can't manage to do it. Here is my attempt : package experimentations.chapter02; import java.util.Arrays; import java.util.Comparator; import java.util.stream.Collectors; public class StringStream { public static void main(String[] args) { sortStrings(); } public static void sortStrings(){ String[] stringsArray = "The quick brown fox has a