I have a list of string and Map<String,String> where I need to replace values of a String with the values of map if list of elements are matching with key element of map.
List<String> mylist = Arrays.asList("key1", "key2", "key3", "key4");
Map<String, String> map2 = new HashMap<>();
map2.put("key1", "value1");
map2.put("key2", "value2");
map2.put("key11", "value11");
var classVar = new Object() {
String sb = "";
};
classVar.sb = "Select * from table1 where key1='apple' and key2='mango' and key11='banana'";
mylist.forEach(e -> {
map2.forEach((k, v) -> {
if (e.equalsIgnoreCase(k)) {
classVar.sb = classVar.sb.replaceAll(k, v);
}
});
});
final string should be:
Select * from table1 where value1='apple' and value2='mango' and key11='banana'
Key11
should not be replaced as it is not there in the mylist
.
How to do the same using map()
/stream()
etc. functionality of Java 8 instead of a forEach
loop? I have a large no of string elements in list and map which I need to replace in the final string dynamically.