The below code works, but I have notification from SonarLint because I use an anonymous class in stream instead of lambda expression, and I don't see how to improve the below code avoiding the notification:
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)));
Explication of code:
I use the properties class from java.util and unfortunately, the entrySet of properties returns Entry<Object, Object>, not Entry<String, String>. I want to "join" the two properties objects putting key and value in lower case. So, the map allows to convert Entry<Object, Object> in Entry<String,String>. That's why, there is an anonymous class.