Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to enable "type information" for streams returned from methods?

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:

with type information

But when you don't call stream() directly, like when it is returned from another method, that information is omitted:

without types

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?!

like image 375
GhostCat Avatar asked Jun 07 '19 06:06

GhostCat


People also ask

What is the return type of Stream?

Generating Streams stream() − Returns a sequential stream considering collection as its source. parallelStream() − Returns a parallel Stream considering collection as its source.

What does Stream of () method in Java?

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.

What are the types of streams in Java 8?

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.


1 Answers

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".

like image 65
Roman Ivanov Avatar answered Oct 20 '22 00:10

Roman Ivanov