如何在LinkedHashMap中替换字符?

huangapple go评论61阅读模式
英文:

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 :

&quot;A&quot; - quantity
&quot;B&quot; - quantity
&quot;C&quot; - quantity
&quot;D&quot; - quantity
&quot;E&quot; - quantity
&quot; &quot; - quantity

Here is my code:

public LinkedHashMap&lt;Character, Integer&gt; output() {
    LinkedHashMap&lt;Character, Integer&gt; map = new LinkedHashMap&lt;&gt;();
    for (int i = 0; i &lt; 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&#39;s solution
public LinkedHashMap&lt;Character, Integer&gt; output() {
    LinkedHashMap&lt;Character, Integer&gt; map = new LinkedHashMap&lt;&gt;();
    int spaceCount = 0;
    for (int i = 0; i &lt; inputString.length(); i++) {
        char wordToLetter = inputString.toLowerCase().charAt(i);
        if (wordLetter == &#39; &#39;) {
            spaceCount++;
        } else if (map.containsKey(wordToLetter)) {
            int quantity = map.get(wordToLetter);
            map.put(wordToLetter, quantity + 1);
        } else {
            map.put(wordToLetter, 1);
        }
    }
    if (spaceCount &gt; 0) {
        map.put(&#39; &#39;, 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 -&gt; (i == null) ? 1 : i + 1);
        } 
        ...

huangapple
  • 本文由 发表于 2020年10月4日 19:52:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/64194259.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定