Replace HashMap keys with values in Property file in Java
I have to replace HashMap keys based on a property file mapping with old - new key mapping. Is the below approach best way of replacing keys?
KeyMapping.properties
newKey1 oldLKey1
newKey2 oldKey2
//Load property mapping file
ResourceBundle properties = ResourceBundle.getBundle("KeyMapping");
Enumeration<String> newKeys = properties.getKeys();
Map<String, Object> result = new LinkedHashMap<>();
while (newKeys.hasMoreElements()) {
String newKey = (String) newKeys.nextElement();
Iterator<Entry<String, Object>> iterator = mapToReplaceKeys.entrySet().iterator();
while(iterator.hasNext()) {
Entry<String, Object> entry = iterator.next();
//If key matches the key in property file
if (entry.getKey().equals(newKey)) {
//remove the entry from map mapToReplaceKeys
iterator.remove();
//add the key with the 'oldKey' and existing value
result.put(properties.getString(newKey), entry.getValue());
}
}
}
java java-8 hashmap
add a comment |
I have to replace HashMap keys based on a property file mapping with old - new key mapping. Is the below approach best way of replacing keys?
KeyMapping.properties
newKey1 oldLKey1
newKey2 oldKey2
//Load property mapping file
ResourceBundle properties = ResourceBundle.getBundle("KeyMapping");
Enumeration<String> newKeys = properties.getKeys();
Map<String, Object> result = new LinkedHashMap<>();
while (newKeys.hasMoreElements()) {
String newKey = (String) newKeys.nextElement();
Iterator<Entry<String, Object>> iterator = mapToReplaceKeys.entrySet().iterator();
while(iterator.hasNext()) {
Entry<String, Object> entry = iterator.next();
//If key matches the key in property file
if (entry.getKey().equals(newKey)) {
//remove the entry from map mapToReplaceKeys
iterator.remove();
//add the key with the 'oldKey' and existing value
result.put(properties.getString(newKey), entry.getValue());
}
}
}
java java-8 hashmap
add a comment |
I have to replace HashMap keys based on a property file mapping with old - new key mapping. Is the below approach best way of replacing keys?
KeyMapping.properties
newKey1 oldLKey1
newKey2 oldKey2
//Load property mapping file
ResourceBundle properties = ResourceBundle.getBundle("KeyMapping");
Enumeration<String> newKeys = properties.getKeys();
Map<String, Object> result = new LinkedHashMap<>();
while (newKeys.hasMoreElements()) {
String newKey = (String) newKeys.nextElement();
Iterator<Entry<String, Object>> iterator = mapToReplaceKeys.entrySet().iterator();
while(iterator.hasNext()) {
Entry<String, Object> entry = iterator.next();
//If key matches the key in property file
if (entry.getKey().equals(newKey)) {
//remove the entry from map mapToReplaceKeys
iterator.remove();
//add the key with the 'oldKey' and existing value
result.put(properties.getString(newKey), entry.getValue());
}
}
}
java java-8 hashmap
I have to replace HashMap keys based on a property file mapping with old - new key mapping. Is the below approach best way of replacing keys?
KeyMapping.properties
newKey1 oldLKey1
newKey2 oldKey2
//Load property mapping file
ResourceBundle properties = ResourceBundle.getBundle("KeyMapping");
Enumeration<String> newKeys = properties.getKeys();
Map<String, Object> result = new LinkedHashMap<>();
while (newKeys.hasMoreElements()) {
String newKey = (String) newKeys.nextElement();
Iterator<Entry<String, Object>> iterator = mapToReplaceKeys.entrySet().iterator();
while(iterator.hasNext()) {
Entry<String, Object> entry = iterator.next();
//If key matches the key in property file
if (entry.getKey().equals(newKey)) {
//remove the entry from map mapToReplaceKeys
iterator.remove();
//add the key with the 'oldKey' and existing value
result.put(properties.getString(newKey), entry.getValue());
}
}
}
java java-8 hashmap
java java-8 hashmap
edited Nov 22 '18 at 4:07
nullpointer
44.6k1096184
44.6k1096184
asked Nov 21 '18 at 23:38
user679526user679526
45731133
45731133
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
What you're essentially doing is this:
Map<String, Object> result = Collections.list(properties.getKeys())
.stream()
.flatMap(element -> mapToReplaceKeys.entrySet()
.stream()
.filter(entry -> entry.getKey().equals(element)))
.collect(toMap(e -> properties.getString(e.getKey()),
Map.Entry::getValue,
(l, r) -> r,
LinkedHashMap::new));
or you could also do:
Map<String, Object> result = new LinkedHashMap<>();
newKeys.asIterator()
.forEachRemaining(e -> mapToReplaceKeys.forEach((k, v) -> {
if(k.equals(e)) result.put(properties.getString(k), v);
}));
Iterating over aMap
, to find an equal key … that feels so … wrong…
– Holger
Nov 22 '18 at 13:11
@Holger now that you mention it, I feel so silly...
– Aomine
Nov 22 '18 at 13:32
add a comment |
Don’t iterate over a Map
, just to check the keys for equality. That’s what the Map
’s dedicated lookup methods are for:
ResourceBundle properties = ResourceBundle.getBundle("KeyMapping");
Map<String, Object> result = new LinkedHashMap<>();
for(String newKey: properties.keySet()) {
Object value = mapToReplaceKeys.remove(newKey);
if(value != null) result.put(properties.getString(newKey), value);
}
Since you want to remove the mappings, you can just use remove
on the Map
, which will do nothing and just return null
when the key is not present.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53421961%2freplace-hashmap-keys-with-values-in-property-file-in-java%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
What you're essentially doing is this:
Map<String, Object> result = Collections.list(properties.getKeys())
.stream()
.flatMap(element -> mapToReplaceKeys.entrySet()
.stream()
.filter(entry -> entry.getKey().equals(element)))
.collect(toMap(e -> properties.getString(e.getKey()),
Map.Entry::getValue,
(l, r) -> r,
LinkedHashMap::new));
or you could also do:
Map<String, Object> result = new LinkedHashMap<>();
newKeys.asIterator()
.forEachRemaining(e -> mapToReplaceKeys.forEach((k, v) -> {
if(k.equals(e)) result.put(properties.getString(k), v);
}));
Iterating over aMap
, to find an equal key … that feels so … wrong…
– Holger
Nov 22 '18 at 13:11
@Holger now that you mention it, I feel so silly...
– Aomine
Nov 22 '18 at 13:32
add a comment |
What you're essentially doing is this:
Map<String, Object> result = Collections.list(properties.getKeys())
.stream()
.flatMap(element -> mapToReplaceKeys.entrySet()
.stream()
.filter(entry -> entry.getKey().equals(element)))
.collect(toMap(e -> properties.getString(e.getKey()),
Map.Entry::getValue,
(l, r) -> r,
LinkedHashMap::new));
or you could also do:
Map<String, Object> result = new LinkedHashMap<>();
newKeys.asIterator()
.forEachRemaining(e -> mapToReplaceKeys.forEach((k, v) -> {
if(k.equals(e)) result.put(properties.getString(k), v);
}));
Iterating over aMap
, to find an equal key … that feels so … wrong…
– Holger
Nov 22 '18 at 13:11
@Holger now that you mention it, I feel so silly...
– Aomine
Nov 22 '18 at 13:32
add a comment |
What you're essentially doing is this:
Map<String, Object> result = Collections.list(properties.getKeys())
.stream()
.flatMap(element -> mapToReplaceKeys.entrySet()
.stream()
.filter(entry -> entry.getKey().equals(element)))
.collect(toMap(e -> properties.getString(e.getKey()),
Map.Entry::getValue,
(l, r) -> r,
LinkedHashMap::new));
or you could also do:
Map<String, Object> result = new LinkedHashMap<>();
newKeys.asIterator()
.forEachRemaining(e -> mapToReplaceKeys.forEach((k, v) -> {
if(k.equals(e)) result.put(properties.getString(k), v);
}));
What you're essentially doing is this:
Map<String, Object> result = Collections.list(properties.getKeys())
.stream()
.flatMap(element -> mapToReplaceKeys.entrySet()
.stream()
.filter(entry -> entry.getKey().equals(element)))
.collect(toMap(e -> properties.getString(e.getKey()),
Map.Entry::getValue,
(l, r) -> r,
LinkedHashMap::new));
or you could also do:
Map<String, Object> result = new LinkedHashMap<>();
newKeys.asIterator()
.forEachRemaining(e -> mapToReplaceKeys.forEach((k, v) -> {
if(k.equals(e)) result.put(properties.getString(k), v);
}));
edited Nov 22 '18 at 0:47
answered Nov 22 '18 at 0:36
AomineAomine
41.8k74071
41.8k74071
Iterating over aMap
, to find an equal key … that feels so … wrong…
– Holger
Nov 22 '18 at 13:11
@Holger now that you mention it, I feel so silly...
– Aomine
Nov 22 '18 at 13:32
add a comment |
Iterating over aMap
, to find an equal key … that feels so … wrong…
– Holger
Nov 22 '18 at 13:11
@Holger now that you mention it, I feel so silly...
– Aomine
Nov 22 '18 at 13:32
Iterating over a
Map
, to find an equal key … that feels so … wrong…– Holger
Nov 22 '18 at 13:11
Iterating over a
Map
, to find an equal key … that feels so … wrong…– Holger
Nov 22 '18 at 13:11
@Holger now that you mention it, I feel so silly...
– Aomine
Nov 22 '18 at 13:32
@Holger now that you mention it, I feel so silly...
– Aomine
Nov 22 '18 at 13:32
add a comment |
Don’t iterate over a Map
, just to check the keys for equality. That’s what the Map
’s dedicated lookup methods are for:
ResourceBundle properties = ResourceBundle.getBundle("KeyMapping");
Map<String, Object> result = new LinkedHashMap<>();
for(String newKey: properties.keySet()) {
Object value = mapToReplaceKeys.remove(newKey);
if(value != null) result.put(properties.getString(newKey), value);
}
Since you want to remove the mappings, you can just use remove
on the Map
, which will do nothing and just return null
when the key is not present.
add a comment |
Don’t iterate over a Map
, just to check the keys for equality. That’s what the Map
’s dedicated lookup methods are for:
ResourceBundle properties = ResourceBundle.getBundle("KeyMapping");
Map<String, Object> result = new LinkedHashMap<>();
for(String newKey: properties.keySet()) {
Object value = mapToReplaceKeys.remove(newKey);
if(value != null) result.put(properties.getString(newKey), value);
}
Since you want to remove the mappings, you can just use remove
on the Map
, which will do nothing and just return null
when the key is not present.
add a comment |
Don’t iterate over a Map
, just to check the keys for equality. That’s what the Map
’s dedicated lookup methods are for:
ResourceBundle properties = ResourceBundle.getBundle("KeyMapping");
Map<String, Object> result = new LinkedHashMap<>();
for(String newKey: properties.keySet()) {
Object value = mapToReplaceKeys.remove(newKey);
if(value != null) result.put(properties.getString(newKey), value);
}
Since you want to remove the mappings, you can just use remove
on the Map
, which will do nothing and just return null
when the key is not present.
Don’t iterate over a Map
, just to check the keys for equality. That’s what the Map
’s dedicated lookup methods are for:
ResourceBundle properties = ResourceBundle.getBundle("KeyMapping");
Map<String, Object> result = new LinkedHashMap<>();
for(String newKey: properties.keySet()) {
Object value = mapToReplaceKeys.remove(newKey);
if(value != null) result.put(properties.getString(newKey), value);
}
Since you want to remove the mappings, you can just use remove
on the Map
, which will do nothing and just return null
when the key is not present.
answered Nov 22 '18 at 13:10
HolgerHolger
163k23231438
163k23231438
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53421961%2freplace-hashmap-keys-with-values-in-property-file-in-java%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown