Since a few versions, IntelliJ has a very helpful feature: when you put the individual method calls of a stream()
statement on separate lines, IntelliJ puts type information on each line:
But when you don't call stream()
directly, like when it is returned from another method, that information is omitted:
Is there a way to convince IntelliJ to show such type information for such situations, too?
As pure text, with manually inserted comments to "show" the problem with pure text:
public Stream<Entry<String, String>> withTypeInformation() {
return generateMap() // Map<String, String>
.entrySet() // Set<Entry<String, String>>
.stream() // Stream<Set<Entry<String, String>>>
.filter(e -> !e.getKey().equals("foo")) // Stream<Set<Entry<String, String>>>
.filter(e -> !e.getKey().equals("bar")) // Stream<Set<Entry<String, String>>>
.filter(e -> !e.getKey().equals("now"));
}
public Stream<Entry<String, String>> withoutTypeInformation() {
return withTypeInformation() // no such info
.filter(e -> !e.getKey().equals("foo")) // not here either
.filter(e -> !e.getKey().equals("bar")) // guess what, nothing, too
.filter(e -> !e.getKey().equals("now"));
}
And note: the first method uses a generator method that returns a map instance. There IntelliJ is smart enough to give me the type information?!
Generating Streams stream() − Returns a sequential stream considering collection as its source. parallelStream() − Returns a parallel Stream considering collection as its source.
Stream of(T t) returns a sequential Stream containing a single element. Syntax : static Stream of(T t) Parameters: This method accepts a mandatory parameter t which is the single element in the Stream. Return Value: Stream of(T t) returns a sequential Stream containing the single specified element.
Java 8 offers the possibility to create streams out of three primitive types: int, long and double. As Stream<T> is a generic interface, and there is no way to use primitives as a type parameter with generics, three new special interfaces were created: IntStream, LongStream, DoubleStream.
Actually, there is a heuristic, that makes IDEA not show this hints. If the count of different types the single chain is less than 3, they won't be shown. It is required to avoid spamming this hints, when the type of expression is obvious (e. g. builders).
In IntelliJ IDEA 2019.2 count of different types, required to show hints can be adjusted in settings (if set it to 1, hints will be always shown).
Note: to get to that setting, one has to turn to Preferences -> Editor -> Inlay Hints -> Java and change the "unique type count" for "Method hints".
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With