-2

I understand its there to circumvent the double wrapping performance issue of Optional<Integer> however in this example:

OptionalInt x = IntStream.range(1, 1000000)
        .map(y -> y * 2)
        .filter(y -> y > 10)
        .findAny();

The intermediaries are the ones dealing with lots of values i.e. IntStream, IntUnaryOperator etc. The OptionalInt is only created once at the end.

Is it really worth having OptionalInt just for that single return object at the end?

Andy Cribbens
  • 1,370
  • 2
  • 11
  • 22
  • 1
    There are many other uses for `OptionalInt` than this one. And since it existed anyway, they might just as well use it here too. – Ole V.V. Jan 09 '21 at 20:10
  • I am unable to think of a use other than a streaming context and even there it looks like it has limited use. Do you have a link to some other uses? – Andy Cribbens Jan 09 '21 at 20:13
  • I could return it from a method but the caller would lack some useful methods. So I would return Optional. I could understand if it was a million of them in a streaming context but even there only one is created. – Andy Cribbens Jan 09 '21 at 20:14
  • For me personally, I think OptionalInt is a much cleaner way to tell a possible unpresent Return value than primitive magic numbers like `-1` or boxed `null`. – Felix Jan 09 '21 at 20:43
  • @codeflush.dev null is never boxed. The optional (either flavour) can be empty. – Andy Cribbens Jan 09 '21 at 20:48
  • I meant boxed type (Integer) to be able to Return `null`. – Felix Jan 09 '21 at 20:49

2 Answers2

1

First of all, remember that there might be no value present after the filter method usage (or any that returns a different number of objects like reduce). It is not guaranteed that a value would be always present. That's behind the design of returning something that says that the value might be there instead of null that likely causes errors.

Is it really worth having OptionalInt just for that single return object at the end?

Yes, it is worth it. OptionalInt is a representation of Optional<Integer> that returns a primitive int instead (of course the value must be present). We can say it wraps int. Since boxing is related only to primitives, I guess introducing such object/s is for sake of convenience and boxing operation overhead avoidance.

OptionalInt optionalInt = IntStream.range(0, 10)
        .filter(i -> i > 5)
        .map(i -> i + 1)
        .findAny();
int resultInt1 = optionalInt.getAsInt();  // PROS: explicit method name advantage
Optional<Integer> optional =  IntStream.range(0, 10)
        .filter(i -> i > 5)
        .mapToObj(i -> i + 1)             // CONS: boxing (int -> Integer)
        .findAny();
int resultInt2 = optional.get();          // CONS: boxing (Integer -> int)
                                          // CONS: not explicit method name
Nikolas Charalambidis
  • 40,893
  • 16
  • 117
  • 183
  • If there is no value after the filter then that is handled with an empty IntStream. My question was around the fact that I can only see one OptionalInt being created so all that is saved is one box 'Integer' – Andy Cribbens Jan 09 '21 at 20:10
  • 2
    As I said, the first thing is boxing operation avoidance, the second thing is the convenience of returning a primitive type and explicit method naming. – Nikolas Charalambidis Jan 09 '21 at 20:19
0

What are you going to return if it doesn't find any?

int x = IntStream.range(1, 1000000)
        .map(y -> y * 2)
        .filter(y -> y > 10)
        .findAny().orElse(-23);
WJS
  • 36,363
  • 4
  • 24
  • 39
  • But then you have to box the result. Here it either returns a positive integer or -23 as I am not returning an OptionalInt but the value or a default. – WJS Jan 09 '21 at 20:09
  • So the performance improvement is the two boxes you would remove here: .findAny().orElse(-23); ? Again it seems a small improvement. If it were for the million records I could understand. – Andy Cribbens Jan 09 '21 at 20:13
  • 3
    Well, why map to an Integer if you don't have too? – WJS Jan 09 '21 at 20:19
  • @AndyCribbens but it may be used for the Million records. OptionalInt is defined ins the spec, so its for every possible type of application – Felix Jan 09 '21 at 20:46