Skip to content

Tag: consumer

How to throw an Exception in a Consumer Java 8

Is there any way to throw an Exception while using a consumer in java 8? For example: This gives me a compiler error saying: Unhandled exception type Exception What is the correct way to throw an exception in this case? Answer Since Exception and its subclass (other than RuntimeException) are checked Exceptio…

Does the JDK provide a dummy consumer?

I have a need in a block of code to consume ‘n’ items from a stream then finish, in essence: In my situation, I can’t change the signature to return Stream<T> and simply return stream.skip(n); I have to actually throw away some elements from the stream (not simple logic) – to be …