slartidan
slartidan

Reputation: 21576

Special behavior of a stream if there are no elements

How can I express this with java8 streaming-API?

I want to perform itemConsumer for every item of a stream. If there are no items I want to perform emptyAction.

Of course I could write something like this:

Consumer<Object> itemConsumer = System.out::println;
Runnable emptyAction = () -> {System.out.println("no elements");};

Stream<Object> stream = Stream.of("a","b"); // or Stream.empty()
List<Object> list = stream.collect(Collectors.toList());
if (list.isEmpty())
    emptyAction.run();
else
    list.stream().forEach(itemConsumer);

But I would prefer to avoid any Lists.

I also thought about setting a flag in a peek method - but that flag would be non-final and therefore not allowed. Using a boolean container also seems to be too much of a workaround.

Upvotes: 7

Views: 4122

Answers (5)

user140547
user140547

Reputation: 8200

Another attempt to use reduce:

    Stream<Object> stream = Stream.of("a","b","c");
    //Stream<Object> stream = Stream.empty();


    Runnable defaultRunnable = () -> System.out.println("empty Stream");
    Consumer<Object> printConsumer = System.out::println;

    Runnable runnable = stream.map(x -> toRunnable(x, printConsumer)).reduce((a, b) -> () -> {
        a.run();
        b.run();
    }).orElse(defaultRunnable);

    runnable.run(); // prints a, b, c (or empty stream when it is empty)


    // for type inference
    static <T> Runnable toRunnable(T t, Consumer<T> cons){ 
        return ()->cons.accept(t);
    }

This approach does not use peek() which according to Javadoc "mainly exists to support debugging"

Upvotes: 0

Alex - GlassEditor.com
Alex - GlassEditor.com

Reputation: 15497

You could do this:

if(stream.peek(itemConsumer).count() == 0){
    emptyAction.run();
}

But it seems that count may be changed to skip the peek if it knows the size of the Stream in Java 9 (see here), so if you want it to work in the future you could use:

if(stream.peek(itemConsumer).mapToLong(e -> 1).sum() == 0){
    emptyAction.run();
}

Upvotes: 1

Holger
Holger

Reputation: 298143

There’s a simple straight-forward solution:

Spliterator<Object> sp=stream.spliterator();
if(!sp.tryAdvance(itemConsumer))
    emptyAction.run();
else
    sp.forEachRemaining(itemConsumer);

You can even keep parallel support for the elements after the first, if you wish:

Spliterator<Object> sp=stream.parallel().spliterator();
if(!sp.tryAdvance(itemConsumer))
    emptyAction.run();
else
    StreamSupport.stream(sp, true).forEach(itemConsumer);

In my opinion, it is much easier to understand as a reduce based solution.

Upvotes: 1

Tagir Valeev
Tagir Valeev

Reputation: 100189

A solution without any additional variables:

stream.peek(itemConsumer).reduce((a, b) -> a).orElseGet(() -> {
    emptyAction.run();
    return null;
});

Note that if the stream is parallel, then itemConsumer could be called simultaneously for different elements in different threads (like in forEach, not in forEachOrdered). Also this solution will fail if the first stream element is null.

Upvotes: 2

Boris the Spider
Boris the Spider

Reputation: 61148

You could coerce reduce to do this. The logic would be to reduce on false, setting the value to true if any useful data is encountered.

The the result of the reduce is then false then no items have been encountered. If any items were encountered then the result would be true:

boolean hasItems = stream.reduce(false, (o, i) -> {
    itemConsumer.accept(i);
    return true;
}, (l, r) -> l | r);
if (!hasItems) {
    emptyAction.run();
}

This should work fine for parallel streams, as any stream encountering an item would set the value to true.

I'm not sure, however, that I like this as it's a slightly obtuse use of the reduce operation.

An alternative would be to use AtomicBoolean as a mutable boolean container:

final AtomicBoolean hasItems = new AtomicBoolean(false);
stream.forEach(i -> {
    itemConsumer.accept(i);
    hasItems.set(true);
});
if (!hasItems.get()) {
    emptyAction.run();
}

I don't know if I like that more or less however.

Finally, you could have your itemConsumer remember state:

class ItemConsumer implements Consumer<Object> {

    private volatile boolean hasConsumedAny;

    @Override
    public void accept(Object o) {
        hasConsumedAny = true;
        //magic magic
    }

    public boolean isHasConsumedAny() {
        return hasConsumedAny;
    }
}

final ItemConsumer itemConsumer = new ItemConsumer();
stream.forEach(itemConsumer::accept);
if (!itemConsumer.isHasConsumedAny()) {
    emptyAction.run();
}

This seems a bit neater, but might not be practical. So maybe a decorator pattern -

class ItemConsumer<T> implements Consumer<T> {

    private volatile boolean hasConsumedAny;
    private final Consumer<T> delegate;

    ItemConsumer(final Consumer<T> delegate) {
        this.delegate = delegate;
    }

    @Override
    public void accept(T t) {
        hasConsumedAny = true;
        delegate.accept(t);
    }

    public boolean isHasConsumedAny() {
        return hasConsumedAny;
    }
}

final ItemConsumer<Object> consumer = new ItemConsumer<Object>(() -> /** magic **/);

TL;DR: something has to remember whether you encountered anything during the consumption of the Stream, be it:

  • the Stream itself in case of reduce;
  • AtomicBoolean; or
  • the consumer

I think the consumer is probably best placed, from a logic point of view.

Upvotes: 3

Related Questions