如何启用“类型信息"?从方法返回的流? [英] How to enable "type information" for streams returned from methods?

查看:96
本文介绍了如何启用“类型信息"?从方法返回的流?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于有几个版本,IntelliJ具有一个非常有用的功能:将stream()语句的各个方法调用放在单独的行上时,IntelliJ会在每行上放置类型信息:

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:

但是当您不直接调用stream()时(例如从另一个方法返回时),该信息将被省略:

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

是否有一种方法可以说服IntelliJ在这种情况下显示此类类型信息?

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"));
}

并注意:第一种方法使用了生成器方法,该方法返回地图实例. IntelliJ足够聪明,可以给我类型信息吗?!

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

推荐答案

实际上,有一种启发式方法,使IDEA不会显示此提示.如果单链的不同类型计数少于3,则不会显示它们.当表达式的类型很明显时(例如,构建器),有必要避免向这些提示发送垃圾邮件.

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

在IntelliJ IDEA 2019.2中,显示提示所需的不同类型计数可以在设置中进行调整(如果将其设置为1,则提示将始终显示).

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

注意:要进行该设置,必须先转到首选项"->编辑器"->镶嵌提示"->"Java",然后将方法提示"的唯一类型计数"更改为

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

这篇关于如何启用“类型信息"?从方法返回的流?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆