I’m trying to perform a map operation on each entry in a Map object. I need to take a prefix off the key and convert the value from one type to another. My code is taking configuration entries from a Map<String, String> and converting to a Map<String, AttributeType> (AttributeType is just a class holding some information. Further explanation is not
Tag: java-stream
What’s the difference between map() and flatMap() methods in Java 8?
In Java 8, what’s the difference between Stream.map() and Stream.flatMap() methods? Answer Both map and flatMap can be applied to a Stream<T> and they both return a Stream<R>. The difference is that the map operation produces one output value for each input value, whereas the flatMap operation produces an arbitrary number (zero or more) values for each input value. This
How to generate random array of ints using Stream API Java 8?
I am trying to generate random array of integers using new Stream API in Java 8. But I haven’t understood this API clearly yet. So I need help. Here is my code. But this code returns array of objects. What is wrong with it? Answer If you want primitive int values, do not call IntStream::boxed as that produces Integer objects
What is difference between Collection.stream().forEach() and Collection.forEach()?
I understand that with .stream(), I can use chain operations like .filter() or use parallel stream. But what is difference between them if I need to execute small operations (for example, printing the elements of the list)? Answer For simple cases such as the one illustrated, they are mostly the same. However, there are a number of subtle differences that
Java 8’s streams: why parallel stream is slower?
I am playing with Java 8’s streams and cannot understand the performance results I am getting. I have 2 core CPU (Intel i73520M), Windows 8 x64, and 64-bit Java 8 update 5. I am doing simple map over …
Merge lists with stream API
I have the following situation I have to merge all the lists lst from the ListContainer objects from a Map map. Any idea how, using Java 8 stream API? Answer I think flatMap() is what you’re looking for. For example:
Using Streams with primitives data types and corresponding wrappers
While playing around with Java8’s Streams-API, I stumbled over the following: To convert an array of primitive wrapper classe objects into a Stream I just have to call Stream.of(array). But to convert an array of primitive data types, I have to call .of(array) from the corresponding wrapper (class) stream class (<– that sounds silly). An example: My question(s): Why is
Collect successive pairs from a stream
Given a stream such as { 0, 1, 2, 3, 4 }, how can I most elegantly transform it into given form: { new Pair(0, 1), new Pair(1, 2), new Pair(2, 3), new Pair(3, 4) } (assuming, of course, I’ve defined class Pair)? Edit: This isn’t strictly about ints or primitive streams. The answer should be general for a stream
Java 8 Iterable.forEach() vs foreach loop
Which of the following is better practice in Java 8? Java 8: Java 7: I have lots of for loops that could be “simplified” with lambdas, but is there really any advantage of using them? Would it improve their performance and readability? EDIT I’ll also extend this question to longer methods. I know that you can’t return or break the