Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

232
Visualizações
How can a interface Stream extend Base stream interface having type bounds just as the stream interface i.e. Stream <T>?

I was going through Streams Documentation and had observed that Stream is a typed interface that extends Base Stream with types as T and again Stream <T>

public interface Stream<T> extends BaseStream<T,Stream<T>> {
}

How can a interface Stream can extend Base stream interface having type bounds just as the stream interface i.e. Stream <T>?

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

Why couldn't it?

This is a trick used in many places, for example enums, which are defined as:

public abstract class Enum<E extends Enum<E>>

The general idea is to establish a type parameter that represents your own type.

That's why that parameter's letter is S. That stands for "Self".

Hence, if we look at DoubleStream, it is defined as:

public interface DoubleStream extends BaseStream<Double, DoubleStream> {}

The point of doing this is for 'self returning' methods, or at least, for methods that need to return the same type. There are loads of those in the stream concept, such as peek, or limit, or onClose.

You could just.. not do that, and define things as follows:

public interface BaseStream<T> {
  BaseStream parallel();
}

But the problem is, this downgrades types. If I have an IntStream and I invoke .parallel() on it, the compiler thinks the type of that is BaseStream. This isn't usually relevant (in that IntStream can tighten the return type, that's valid as per the Java Lang Spec), but sometimes it is.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda