是否可以在任何Java IDE中折叠源代码中的类型定义?

asm*_*ier 3 java ide folding

最近我经常要读这样的Java代码:

LinkedHashMap<String, Integer> totals =  new LinkedHashMap<String, Integer>(listOfRows.get(0))
for (LinkedHashMap<String, Integer> row : (ArrayList<LinkedHashMap<String,Integer>>) table.getValue()) {    
    for(Entry<String, Integer> elem : row.entrySet()) {
        String colName=elem.getKey();
        int Value=elem.getValue();
        int oldValue=totals.get(colName);

        int sum = Value + oldValue;
        totals.put(colName, sum);
    }
}
Run Code Online (Sandbox Code Playgroud)

由于长和嵌套的类型定义,简单的算法变得非常模糊.所以我希望我可以使用我的IDE删除或折叠类型定义,以查看没有类型的Java代码:

totals =  new (listOfRows.get(0))
for (row : table.getValue()) {    
    for(elem : row.entrySet()) {
        colName=elem.getKey();
        Value=elem.getValue();
        oldValue=totals.get(colName);

        sum = Value + oldValue;
        totals.put(colName, sum);
    }
}
Run Code Online (Sandbox Code Playgroud)

当然,最好的方法是折叠类型定义,但是当将鼠标移到变量上时,将类型显示为工具提示.是否有可以执行此操作的IDE的IDE IDE或插件?

oxb*_*kes 5

IntelliJ IDEA会将声明右侧的类型转换为<~>.以便:

Map<Integer, String> m = new HashMap<Integer, String>();
Run Code Online (Sandbox Code Playgroud)

将显示折叠为:

Map<Integer, String> m = new HashMap<~>();
Run Code Online (Sandbox Code Playgroud)

这可以通过Editor/Code Folding/Generic Constructor和Method Parameters属性进行设置,IDE社区版本是免费的.


或者你可以使用Scala,它有类型推断:

val totals = new mutable.Map[String, Int]
for { 
    row <- table.getValue
    (colName, value) <- row.entrySet 
} totals += (colName -> (value + totals.get(colName) getOrElse 0)
Run Code Online (Sandbox Code Playgroud)