英文:
How to replace chars in LinkedHashMap?
问题
public LinkedHashMap<Character, Integer> output() {
LinkedHashMap<Character, Integer> map = new LinkedHashMap<>();
for (int i = 0; i < inputString.length(); i++) {
char wordToLetter = inputString.toLowerCase().charAt(i);
if (map.containsKey(wordToLetter)) {
int quantity = map.get(wordToLetter);
map.put(wordToLetter, ++quantity);
} else {
map.put(wordToLetter, 1);
}
}
// Move spaces to the end of the map
if (map.containsKey(' ')) {
int spaceQuantity = map.get(' ');
map.remove(' ');
map.put(' ', spaceQuantity);
}
return map;
}
英文:
I wrote a method which makes a map of chars of an input string, but I need that all spaces of this string has been put at the end of the map.
Example of input : "abc de"
Example of output :
"A" - quantity
"B" - quantity
"C" - quantity
"D" - quantity
"E" - quantity
" " - quantity
Here is my code:
public LinkedHashMap<Character, Integer> output() {
LinkedHashMap<Character, Integer> map = new LinkedHashMap<>();
for (int i = 0; i < inputString.length(); i++) {
char wordToLetter = inputString.toLowerCase().charAt(i);
if (map.containsKey(wordToLetter)) {
int quantity = map.get(wordToLetter);
map.put(wordToLetter, ++quantity);
} else {
map.put(wordToLetter, 1);
}
}
return map;
}
答案1
得分: 1
这是基于评论的最佳解决方案:
// Oleg的解决方案
public LinkedHashMap<Character, Integer> output() {
LinkedHashMap<Character, Integer> map = new LinkedHashMap<>();
int spaceCount = 0;
for (int i = 0; i < inputString.length(); i++) {
char wordToLetter = inputString.toLowerCase().charAt(i);
if (wordToLetter == ' ') {
spaceCount++;
} else if (map.containsKey(wordToLetter)) {
int quantity = map.get(wordToLetter);
map.put(wordToLetter, quantity + 1);
} else {
map.put(wordToLetter, 1);
}
}
if (spaceCount > 0) {
map.put(' ', spaceCount);
}
return map;
}
关键在于不要在最后之前将空格(如果有的话)的条目添加到映射中。这意味着它将成为最后的条目。
实际上,我们可以进行如下优化:
...
} else if (map.containsKey(wordToLetter)) {
int quantity = map.get(wordToLetter);
map.put(wordToLetter, quantity + 1);
} else {
int quantity = map.getOrDefault(wordToLetter, 0);
map.put(wordToLetter, quantity + 1);
}
...
这更加简洁,并减少了查找次数。事实上,我们可以做得更好:
...
} else {
map.compute(wordToLetter, (k, v) -> (v == null) ? 1 : v + 1);
}
...
英文:
This is the best solution, based on the comments:
// Oleg's solution
public LinkedHashMap<Character, Integer> output() {
LinkedHashMap<Character, Integer> map = new LinkedHashMap<>();
int spaceCount = 0;
for (int i = 0; i < inputString.length(); i++) {
char wordToLetter = inputString.toLowerCase().charAt(i);
if (wordLetter == ' ') {
spaceCount++;
} else if (map.containsKey(wordToLetter)) {
int quantity = map.get(wordToLetter);
map.put(wordToLetter, quantity + 1);
} else {
map.put(wordToLetter, 1);
}
}
if (spaceCount > 0) {
map.put(' ', spaceCount);
}
return map;
}
The trick is to not add the entry for spaces (if any) to the map until right at the end. That means it will be the last entry.
We can actually optimize this as follows:
...
} else if (map.containsKey(wordToLetter)) {
int quantity = map.get(wordToLetter);
map.put(wordToLetter, quantity + 1);
} else {
map.put(wordToLetter, 1);
}
...
becomes
...
} else {
int quantity = map.getOrDefault(wordToLetter, 0);
map.put(wordToLetter, quantity + 1);
}
...
This is more concise, and reduces the number of lookups. Indeed, we can do even better:
...
} else {
map.compute(wordToLetter, i -> (i == null) ? 1 : i + 1);
}
...
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论