I'm having a Java 8 stream of numbers:
Stream<Number> numbers = ...;
I'd like to iterate the stream and invoke a specific consumer based on the type of each element. I.e. for Integer elements I'd like to invoke a Consumer<Integer>, for Long a Consumer<Long> etc.
There is the forEach() method but this expects a Consumer<? super Number>, requiring that implementation (usually a Lambda expression) to instanceof-switch on the exact type itself.
In terms of an API, I'm essentially looking for something like this:
numbers.forEach( callbacks -> {
callbacks.on( Integer.class, i -> { /* handle Integer */ } );
callbacks.on( Long.class, l -> { /* handle Long */ } )
} );
Is there any existing API which would allow me to register a specific consumer per stream element sub-type in a way similar to this?