1

Consider the following class I have currently:

public interface Drawable { public void compileProgram(); public Program getProgram(); public int getDataSize(); public FloatBuffer putData(final FloatBuffer dataBuffer); public int getDataMode(); public static int countDataSize(final Collection<Drawable> drawables) { return drawables.stream().mapToInt((drawable) -> drawable.getDataSize()).sum(); } public static FloatBuffer putAllData(final List<Drawable> drawables) { FloatBuffer dataBuffer = BufferUtils.createFloatBuffer(countDataSize(drawables) * 3); drawables.stream().forEachOrdered((drawable) -> dataBuffer.put(drawable.putData(dataBuffer))); return (FloatBuffer)dataBuffer.clear(); } } 

Now take a look at countDataSize:

  • I have as input Collection<Drawable>, because I do not care about the order, I just want to sum up their size.
  • I use drawables.stream(), which would be redundant if I would've passed in a Stream<Drawable> argument.

And next take a look at putAllData:

  • As in put here I have List<Drawable> drawables, as the order this time does matter.
  • I use drawables.stream() to access them, this happens in order with forEachOrdered().

I wush to use Stream<Drawable> as input, however does passing in a Collection<Drawable> or List<Drawable> even work then? If it does, how can I ensure that in the first case the order does not matter, while in the second case it does matter?

1
  • 1
    Side comment: drawables.stream().mapToInt((drawable) -> drawable.getDataSize()).sum(); can be written drawables.stream().mapToInt(Drawable::getDataSize).sum(); Commented Jan 28, 2014 at 15:27

1 Answer 1

1

I wish to use Stream<Drawable> as input, however does passing in a Collection<Drawable> or List<Drawable> even work then?

No, streams are not part of the Collection class hierarchy, so if your method expects a Stream<e> it can't accept a Collection/List<E>.

Also note that passing streams around does not necessarily add so much benefit in your case because streams can only be consumed once. For example this would not work:

Stream<E> s = ...; putAllDate(s); countDataSize(s); //IllegalStateException 

The last statement would cause a java.lang.IllegalStateException: stream has already been operated upon or closed.

Sign up to request clarification or add additional context in comments.

3 Comments

Ok, that makes sense indeed, had not considered that before. Any clue how it would translate in bytecode, because enormous performance penalty are not worth the streams in this case?
@skiwi I have not measured but the performance penalty is probably minimal - in the end it is going to iterate over your collection and add ints, more or less the same way you would write it as a loop.
@skiwi The big performance improvement will come with countDataSize if you have a lot (>10,000) elements: write drawables.parallelStream().mapToInt(Drawable::getDataSize).sum(); to get parallel execution.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.