Nan*_* Ba 5 lambda anonymous-function java-8 java-stream sonarlint
下面的代码有效,但是我收到SonarLint的通知,因为我在流中使用了一个匿名类而不是lambda表达式,我没有看到如何改进下面的代码来避免通知:
Properties prop = new Properties();
Properties temp = new Properties();
//... add some values and keys in prop and temp
prop.putAll(temp.entrySet().stream()
.filter( entry -> !prop.containsKey(entry.getKey()))
.map( new Function<Entry<Object, Object>, Entry<String, String>>(){
@Override
public Entry<String, String> apply(Entry<Object, Object> entry) {
return new Entry<String, String>() {
@Override
public String setValue(String value) {
return value.trim().toLowerCase();
}
@Override
public String getValue() {
return ((String) entry.getValue()).trim().toLowerCase();
}
@Override
public String getKey() {
return ((String) entry.getKey()).trim().toLowerCase();
}
};
}
})
.collect(Collectors.toMap(Entry<String,String>::getKey, Entry<String,String>::getValue)));
Run Code Online (Sandbox Code Playgroud)
代码的解释:
我使用java.util中的属性类,不幸的是entrySet
,属性返回Entry<Object, Object>
,而不是Entry<String, String>
.我想"加入"两个属性对象,将键和值放在小写中.因此,地图可以转换Entry<Object, Object>
在Entry<String,String>
.这就是为什么,有一个匿名类.
声纳建议更换
prop.putAll(temp.entrySet().stream()
.filter( entry -> !prop.containsKey(entry.getKey()))
.map( new Function<Entry<Object, Object>, Entry<String, String>>(){
@Override
public Entry<String, String> apply(Entry<Object, Object> entry) {
return new Entry<String, String>() {
@Override
public String setValue(String value) {
return value.trim().toLowerCase();
}
@Override
public String getValue() {
return ((String) entry.getValue()).trim().toLowerCase();
}
@Override
public String getKey() {
return ((String) entry.getKey()).trim().toLowerCase();
}
};
}
})
.collect(Collectors.toMap(Entry::getKey, Entry::getValue)));
Run Code Online (Sandbox Code Playgroud)
(我删除了收集器中不必要的类型参数)
同
prop.putAll(temp.entrySet().stream()
.filter( entry -> !prop.containsKey(entry.getKey()))
.map(entry -> new Entry<String, String>() {
@Override
public String setValue(String value) {
return value.trim().toLowerCase();
}
@Override
public String getValue() {
return ((String) entry.getValue()).trim().toLowerCase();
}
@Override
public String getKey() {
return ((String) entry.getKey()).trim().toLowerCase();
}
})
.collect(Collectors.toMap(Entry::getKey, Entry::getValue)));
Run Code Online (Sandbox Code Playgroud)
它使用lambda表达式替代匿名内部类实现Function
,而不是Entry
实现.
尽管如此,Entry
在这里手动实现接口是没有意义的,特别是setValue
在违反这种合同的实际不需要的方法中.您只需要一个不可变的Entry
实例,因此,您可以创建现有类的实例:
prop.putAll(temp.entrySet().stream()
.filter( entry -> !prop.containsKey(entry.getKey()))
.map(entry -> new AbstractMap.SimpleImmutableEntry<>(
((String) entry.getKey()).trim().toLowerCase(),
((String) entry.getValue()).trim().toLowerCase()))
.collect(Collectors.toMap(Entry::getKey, Entry::getValue)));
Run Code Online (Sandbox Code Playgroud)
作为最后一项改进,Entry
在传递给toMap
收集器的函数中执行转换时,可以完全摆脱实例:
prop.putAll(temp.entrySet().stream()
.filter( entry -> !prop.containsKey(entry.getKey()))
.collect(Collectors.toMap(
entry -> ((String) entry.getKey()) .trim().toLowerCase(),
entry -> ((String) entry.getValue()).trim().toLowerCase())));
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
1599 次 |
最近记录: |