Primitive Specialization Mismatch in Java 8

Why is it PrimitiveIterator.OfIntexpanding Iterator<Integer>but IntStreamnot spreading Stream<Integer>?

I am trying to create a primitive collection type (similar to the interfaces from the Apache Commons Primitives library ( http://commons.apache.org/dormant/commons-primitives/ ) and trying to be consistent and compatible with the collection library, but I cannot decide if I do my ByteListextend List<Byte>or not.

I suppose this is because it Iteratorhas direct syntax support in the language (i.e. for loops using iterators), so it’s worth making the iterator compatible with this syntax even if it forces the box, but I'm curious if there are more deep reason. Thank!

+4
source share
1 answer

It is rare to say why the JDK API-interfaces have been developed as they did, but in this case we can easily see that the attempt to work together API-interface Stream<Integer>and IntStreamwill be challenging because the definitions of both interfaces method has several ambiguities.

Consider the following:

interface Stream<T> {
    Stream<T> distinct();
    Optional<T> findFirst();
}

interface IntStream extends Stream<Integer> {
    IntStream distinct();
    OptionalInt findFirst();
}

The second interface will not compile events, since the method signature is the same, but the return type is different in the second interface.

, , lambdas. Lambdas , . :

interface Stream<T> {
    Stream<T> filter(Predicate<T> p);
    <S> Stream<S> map(Function<T,S> mapper);
}

interface IntStream extends Stream<Integer> {
    IntStream filter(IntPredicate p);
    IntStream map(IntUnaryOperator mapper);
}

, , stream.filter(n -> n > 10), Predicate<Integer>, IntPredicate, API - , . (int n) -> n > 10, .

, API Stream IntStream.

0

Source: https://habr.com/ru/post/1658630/


All Articles