'Difference between two hashmaps and overwrite value on match
Im relativly new to Java and i ran into some problems. The goal is to compare the parameters, e.g. -DmyCustomArg="my_value", i specify in the jvm with the map and overwrite the matching key with new value.
So i have two hashmaps, one is the System.getProperties() and the other is a map with parameters. Now I want to compare both and replace matching values.
I have already tried
Map properties = System.getProperties();
Map<String, String> parameter = new HashMap<>();
parameter.put("1", "value one");
parameter.put("2", "value two");
parameter.put("myCustomArg", "value three");
for (int i = 0; i < parameter.keySet().toArray().length; i++) {
if (properties.containsKey(parameter.keySet().toArray()[i])) {
parameter.replace(properties.get( ??? ))
}
}
But now I can't get any further. How do I get to the value where it matched so I can replace it? Do I have to create a third map where the results are stored or is there an easier way?
Solution 1:[1]
If you need to override the properties is enough to use the same key value in the map. Map will replace previous value.
E.g:
Map systemProperties = System.getProperties();
System.out.println("java.runtime.version before: " + systemProperties.get("java.runtime.version"));
systemProperties.put("java.runtime.version", "17.0.0");
// Print the new value
System.out.println("java.runtime.version after: " + systemProperties.get("java.runtime.version"));
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | oaPiet |
