Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

234
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!