0% found this document useful (0 votes)
2 views1 page

Java Streams Collector & Wildcards Explained

The Collector framework in Java Streams allows for the aggregation and transformation of stream results into various formats like List, Set, or Map using the Stream.collect() operation. Bounded wildcards in Java include unbounded wildcards (?), which represent any type and allow read-only access, upper-bounded wildcards (? extends T), which represent T or its subtypes for reading, and lower-bounded wildcards (? super T), which represent T or its supertypes for adding elements. Each wildcard type has specific use cases and limitations regarding element addition and reading.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views1 page

Java Streams Collector & Wildcards Explained

The Collector framework in Java Streams allows for the aggregation and transformation of stream results into various formats like List, Set, or Map using the Stream.collect() operation. Bounded wildcards in Java include unbounded wildcards (?), which represent any type and allow read-only access, upper-bounded wildcards (? extends T), which represent T or its subtypes for reading, and lower-bounded wildcards (? super T), which represent T or its supertypes for adding elements. Each wildcard type has specific use cases and limitations regarding element addition and reading.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

Explain the Collector framework in Java Streams.

The Collector Framework in Java's Stream API provides a set of utilities to


aggregate, transform, and collect the results of stream operations into a desired
format,
such as a List, Set, Map, or even a single value. Collectors are part of the
[Link] package and are typically used with the [Link]() terminal
operatio

Explain bounded and unbounded wildcards in Java?


Unbounded Wildcard (?)
Definition: Represents any type.
Usage: When you don’t care about the specific type and just need to work with a
collection of any type.
Example: List<?> list = new ArrayList<>();
Limitations: You can’t add elements (except null). You can only read as Object.

Upper-Bounded Wildcard (? extends T)


Definition: Represents any type that is T or a subtype of T.
Usage: When you want to read from a collection and are interested in subtypes of T
(e.g., List<? extends Number> can be List<Integer> or List<Double>).
Example: List<? extends Number> list = new ArrayList<Integer>();
Limitations: You cannot add elements because the specific subtype is unknown.

Lower-Bounded Wildcard (? super T)


Definition: Represents any type that is T or a supertype of T.
Usage: When you want to add elements of type T to a collection (e.g., List<? super
Integer> can accept Integer, Number, or Object).
Example: List<? super Integer> list = new ArrayList<Number>();
Limitations: You can’t safely read elements, as they are of type Object or a
supertype of T.
Summary:
?: Can be any type (read-only, no adding).
? extends T: Subtype of T (read-only).
? super T: Supertype of T (write-only, can add T).

You might also like