Cannot convert IntStream to some Object Stream

I'm trying to use an IntStream to instantiate a stream of objects:

Stream<MyObject> myObjects = 
       IntStream
        .range(0, count)
        .map(id -> new MyObject(id));

But it says that it cannot convert MyObject to int.

Answers


The IntStream class's map method maps ints to more ints, with a IntUnaryOperator (int to int), not to objects.

Generally, all streams' map method maps the type of the stream to itself, and mapToXyz maps to a different type.

Try the mapToObj method instead, which takes an IntFunction (int to object) instead.

.mapToObj(id -> new MyObject(id));

Need Your Help

How to get the name of enumeration value in Swift?

swift enumeration

If I have an enumeration with raw Integer values:

Is it possible to get the non-enumerable inherited property names of an object?

javascript oop object properties

In JavaScript we have a few ways of getting the properties of an object, depending on what we want to get.