The Streams API is an API introduced in Java 8 that supports functional-style operations on streams of values, such as filter-map-reduce pipelines on collections.

learn more… | top users | synonyms

2
votes
2answers
58 views

Are Java 8 streams and lambdas deceiving? [on hold]

I've been using the Java 8's lambdas and streams for a while because my master degree project and I noticed a few things that are not that broadly discussed on the internet. I'm using Netbeans to ...
1
vote
2answers
68 views

How to build a Map that replicates a Function in Java's Lambda API

From a java.util.function.BiFunction that maps a pair of Enums into a value, I want to build a EnumMap that reflects that mapping. For instance, let E1 and E2 be enum types and T any given type: ...
3
votes
4answers
102 views

Java 8 lambda: convert Collection to Map of element, iteration position

How do you convert a collection like ["a", "b", "c"] to a map like {"a": 0, "b":1, "c":2} with the values being the order of iteration. Is there a one liner with streams and collectors in JDK8 for it? ...
0
votes
1answer
50 views

Null pointer Exception during Stream.forEach

I am trying to simulate a median of infinite nos.This is my code package hard; import java.util.PriorityQueue; import java.util.Queue; import java.util.Random; import java.util.stream.Stream; public ...
2
votes
1answer
45 views

Operations on Multiple Streams

Here's what I am doing: I have an event from an RSS feed that is telling me that a Ticket was edited. To get the changes made to that ticket, I have to call a REST service. So I wanted to do it with ...
0
votes
1answer
64 views

Parallel Java Streams on 1 Core

Suppose I have a naive factorial function: import java.util.stream.LongStream; public class FactorialTest { static long factorial(long n, boolean parallel) { return (parallel ...
1
vote
4answers
69 views

Return empty element from Java 8 map operation

Using Java 8 stream what is the best way to map a List<Integer> when you have no output for the input Integer ? Simply return null? But now my output list size will be smaller than my input ...
2
votes
2answers
70 views

Using Java8 to convert a list of object into a string obtained from the toString() method

There are a lot of useful new stuffs in Java8. E.g., I can iterate with a stream over a list of objects and then sum the values from a specific field of the Object's instances. E.g. public class ...
1
vote
2answers
32 views

java 8 stream interference versus non-interference

I understand why the following code is ok. Because the collection is being modified before calling the terminal operation. List<String> wordList = ...; Stream<String> words = ...
3
votes
1answer
57 views

Java 8 extracting/coverting List<Object> into Map<String, List<String>> using stream()

Trying to figure out how can i use the new Java 8 feature .stream() in my code effectively. Here is my code in general List<Item> list = db.query(sqlStatement, (rs, i) -> new Item(rs)); ...
2
votes
1answer
58 views

Java 8 Stream API to denormalize Map<A, Set<B>> to Map<B, A> without a Pair

I want to do something like the following, but I want to know if there is a more elegant way to do it without the Pair class. The Pair class no longer exists so I would have to create my own class, ...
3
votes
2answers
75 views

Where is the Java 8 Consumer with more than one argument?

Such as in .Net, which provides several implementations of the Action delegate (equivalent to Java Consumer functional interface) with different number and type of arguments, I was expecting that Java ...
19
votes
5answers
434 views

Should I return a Collection or a Stream?

Suppose I have a method that returns a read-only view into a member list: class Team { private List<Player> players = new ArrayList<>(); // ... public List<Player> ...
15
votes
1answer
471 views

Java 8: Formatting lambda with newlines and indentation

What I would like to achieve with lambda indentation is the following: Multi-line statement: String[] ppl = new String[] { "Karen (F)", "Kevin (M)", "Lee (M)", "Joan (F)", "Des (M)", "Rick (M)" }; ...
2
votes
3answers
100 views

Can Java 8 streams process in pairs

Just kicking the tires of Java 8, and noticed Lamdas and streams functional programing. Was wondering if a simple command line args consumer could use streams. Cant figure out howto get at two ...
0
votes
2answers
50 views

How Java8's Collection.parallelStream works?

Collection class comes with a new method "parallelStream" in Java SDK 8. It is obvious that this new method provides a mechanism to consume collections in parallel. But, I wonder about how Java ...
5
votes
2answers
110 views

Where does say that Java's parallel stream operations use fork/join?

Here's my understanding of the Stream framework of Java 8: Something creates a source Stream The implementation is responsible for providing a BaseStream#parallel() method, which in turns returns a ...
15
votes
1answer
187 views

Why does Collection.parallelStream() exist when .stream().parallel() does the same thing?

In Java 8, the Collection interface was extended with two methods that return Stream<E>: stream(), which returns a sequential stream, and parallelStream(), which returns a possibly-parallel ...
0
votes
1answer
46 views

Not being able to stop at exception when using consecutive stream.flatMap

When debugging my code in IntelliJ IDEA I can't stop at an exception in certain cases when consecutive flatMap methods are used on a stream. Instead of stopping at exception, it stops in API's ...
2
votes
1answer
59 views

Aggregate List<X> to List<X> with Java 8 Stream API

I've got the following class: class Money { CurrencyUnit currencyUnit; BigDecimal amount; } In my application, I get some random list of Money objects: currencyUnit | amount ...
6
votes
4answers
134 views

Can I duplicate a Stream in Java 8?

Sometimes I want to perform a set of operations on a stream, and then process the resulting stream two different ways with other operations. Can I do this without having to specify the common initial ...
8
votes
1answer
169 views

A java List that implements the new Stream interface?

I just took some time to start looking into the java-8 buzz about streams and lambdas. And have a couple of questions... The first thing that surprised me is that you cannot apply the Stream ...
5
votes
2answers
133 views

Is mapToDouble() really necessary for summing a List<Double> with Java 8 streams?

As far as I can tell, the way to sum a List<Double> using Java 8 streams is this: List<Double> vals = . . . ; double sum = vals.stream().mapToDouble(Double::doubleValue).sum(); To me, ...
3
votes
0answers
107 views

Parallelism and Flatmap in Java 8 Streams

Hi I have a question about parallelism when using flatmap Consider the following Example IntStream.of(-1, ...
3
votes
4answers
182 views

Does Java SE 8 have Pairs or Tuples?

I am playing around with lazy functional operations in Java SE 8, and I want to map an index i to a pair / tuple (i, value[i]), then filter based on the second value[i] element, and finally output ...
1
vote
1answer
43 views

Interrupt parallel Stream execution

Consider this code : Thread thread = new Thread(() -> tasks.parallelStream().forEach(Runnable::run)); tasks are a list of Runnables that should be executed in parallel. When we start this ...
0
votes
2answers
63 views

Custom task Executor or “am I reinventing the wheel?”

I'm trying to create an utility class that will be able to handle bundles of runnables and execute them in different combinations (sync, async). For example : imagine that this is a json like ...
1
vote
1answer
45 views

Does a sequential stream in Java 8 use the combiner parameter on calling collect?

If I call collect on a sequential stream (eg. from calling Collection.stream()) then will it use the combiner parameter I pass to collect? I presume not but I see nothing in the documentation. If ...
1
vote
1answer
52 views

Java - java.lang.IllegalStateException: source already consumed or closed

I have some code: Stream<String> previewImagesURLsList = uploadedVideoItemObj.getPreviewImagesURLsList().parallel(); Stream<HashMap<String, Object>> imagesStream = ...
1
vote
1answer
33 views

Check order of two elements in Java-8-Stream

I want to check the order of two elements in a Java-8-Stream. I have this Iterator-Solution: public static boolean isBefore (A a1, A a2, ...
1
vote
1answer
62 views

Java Stream collect after flatMap returns List<Object> instead of List<String>

I tried the following code using Java 8 streams: Arrays.asList("A", "B").stream() .flatMap(s -> Arrays.asList("X", "Y").stream().map(s1 -> s + s1)).collect(Collectors.toList()); ...
5
votes
3answers
114 views

Iterate two Java-8-Streams together [duplicate]

I'd like to iterate two Java-8-Streams together, so that I have in each iteration-step two arguments. Something like that, where somefunction produces something like Stream<Pair<A,B>>. ...
2
votes
2answers
76 views

How to shuffle a stream using the Stream API?

I decided to take the functional approach in generating a string or random characters, so far I came up with this, it should perform better than boxing and then using a StringJoiner as collector: ...
4
votes
2answers
93 views

Java 8 Streams: multiple filters vs. complex condition

Sometimes you want to filter a Stream with more than one condition: myList.stream().filter(x -> x.size() > 10).filter(x -> x.isCool()) ... or you could do the same with a complex condition ...
6
votes
2answers
443 views

Java 8 streams serial vs parallel performance

On my machine, the program below prints: OptionalLong[134043] PARALLEL took 127869 ms OptionalLong[134043] SERIAL took 60594 ms It's not clear to my why executing the program in serial is faster ...
4
votes
1answer
65 views

Java Stream-API performance with infinite series

I am observing some peculiar behavior with Java8 and the new Stream-API. I would expect the performance of the following two statements to be identical, but it's not. LongStream.iterate(1, n -> n ...
1
vote
1answer
91 views

Java8 reducing a stream

I have the following methods: static IntStream streamedDivisors(final int n) { return IntStream.range(2, n).parallel().filter(input -> n % input == 0); } static int streamedPhi(final int n) { ...
3
votes
1answer
73 views

Difference between iterable.forEach() and iterable.stream().forEach() [duplicate]

It looks like I can call list.forEach(a -> a.stuff()) directly on my collection, instead of list.stream().forEach(a -> a.stuff()). When would I use one over the other (parallelStream() aside..)? ...
2
votes
1answer
91 views

How to Convert Stream Stream<HashMap<String, Object>> to HashMap Array HashMap<String, Object>[]?

I am newbie in Java 8 Streams. Please advice, how to Convert Stream Stream<HashMap<String, Object>> to HashMap Array HashMap<String, Object>[] ? For example, I has some stream in ...
4
votes
1answer
58 views

Java 8 functional: How to compute a list of dependent evolutions of an object?

I want to write the following code in a functional way with streams and lambdas: Thing thing = new Thing(); List<Thing> things = new ArrayList<>(); things.add(thing); for (int i = 0; i ...
1
vote
1answer
145 views

Java 8, collect method, creating a new arraylist and mutation [duplicate]

I just want to clarify something regarding the code below: public static void ageOverTen(final List<Human> man) { final List<Human> byAge = man.stream() .filter(age -> ...
1
vote
1answer
33 views

selecting columns into a list of records with java.util.stream

From a List<record> public final class Record { ... // Several fields private final SimpleSetProperty<String> _outOfDate; Record( ..., SortedSet< String > outOfDate, ... ...
11
votes
2answers
151 views

Is the accumulator of reduce in Java 8 allowed to modify its arguments?

In Java 8, Stream has a method reduce: T reduce(T identity, BinaryOperator<T> accumulator); Is the accumulator operator allowed to modify either of its arguments? I presume not since the ...
3
votes
1answer
69 views

Java 8 equivalent to parameterless Action with void return type

What is the Java 8 equivalent to the CLI parameterless Action with void return type?
1
vote
2answers
97 views

Java 8 Lambda Replacement for Apache Commons CollectionsUtils.select

I have the following code using Apache Commons lib: public static List<Address> addressesHasType(final List<Address> addresses, final AddressType... types) { return ...
4
votes
1answer
101 views

Why is `Stream.collect` type-safe and `Stream.toArray(IntFunction<A[]>)` is not?

Consider the following code fragment String strings[] = {"test"}; final List<String> collect = java.util.Arrays.stream(strings).collect(java.util.stream.Collectors.toList()); final Double[] ...
-1
votes
1answer
26 views

Supplying call latency as a IntStream

I am trying to make use of Java 8 and streams and one of the things I am trying to replace is a system we have where we Use an aspect to measure call latency (per config period of time) to out ...
3
votes
1answer
88 views

Finding average using reduce and collect

I am trying to understand the new Java 8 Stream APIs. http://docs.oracle.com/javase/tutorial/collections/streams/reduction.html I found the example of finding average of numbers using collect API. ...
3
votes
2answers
290 views

How to map to multiple elements with Java 8 streams?

I have a class like this: class MultiDataPoint { private DateTime timestamp; private Map<String, Number> keyToData; } and i want to produce , for each MultiDataPoint class DataSet { ...
0
votes
2answers
62 views

How to use an infinite java.util.stream.Stream?

Say I have an infinite Stream: Stream<Socket> stream = Stream.generate(() -> { try { return serverSocket.accept(); } catch (IOException e) { throw new ...