Java's bracing convention is to put the opening brace on the same line, instead of what you have used (mostly). More importantly, have a consistent convention in your codebase. :) If you are more inclined to your current way, then do so throughout.
HashMap<Character, Integer> map
should be declared using theMap
interface, for simplification.The method name
getMaxViaHashmap()
can be improved upon, as there should not be a need to tell callers how the derivation is done (ViaHashmap
).If your method is meant to be used solely for the derivation, it should not print output to `System.out
System.out
. From my example above, I get the results out first, then display it to the user.Even if you want to do the equivalent of this manually:
if (map.containsKey(key)) { map.put(key, map.get(key) + newValue); } else { map.put(key, newValue); }
There are the newer
Map.compute()
orMap.merge()
methods to help you. For example:// input being the String Map<Character, Long> result = new HashMap<>(); for (int i = 0; i < input.length(); i++) { result.merge(Character.valueOf(input.charAt(i)), 1L, (a, b) -> a + b); }
Java's bracing convention is to put the opening brace on the same line, instead of what you have used (mostly). More importantly, have a consistent convention in your codebase. :) If you are more inclined to your current way, then do so throughout.
HashMap<Character, Integer> map
should be declared using theMap
interface, for simplification.The method name
getMaxViaHashmap()
can be improved upon, as there should not be a need to tell callers how the derivation is done (ViaHashmap
).If your method is meant to be used solely for the derivation, it should not print output to `System.out. From my example above, I get the results out first, then display it to the user.
Even if you want to do the equivalent of this manually:
if (map.containsKey(key)) { map.put(key, map.get(key) + newValue); } else { map.put(key, newValue); }
There are the newer
Map.compute()
orMap.merge()
methods to help you. For example:// input being the String Map<Character, Long> result = new HashMap<>(); for (int i = 0; i < input.length(); i++) { result.merge(Character.valueOf(input.charAt(i)), 1L, (a, b) -> a + b); }
Java's bracing convention is to put the opening brace on the same line, instead of what you have used (mostly). More importantly, have a consistent convention in your codebase. :) If you are more inclined to your current way, then do so throughout.
HashMap<Character, Integer> map
should be declared using theMap
interface, for simplification.The method name
getMaxViaHashmap()
can be improved upon, as there should not be a need to tell callers how the derivation is done (ViaHashmap
).If your method is meant to be used solely for the derivation, it should not print output to
System.out
. From my example above, I get the results out first, then display it to the user.Even if you want to do the equivalent of this manually:
if (map.containsKey(key)) { map.put(key, map.get(key) + newValue); } else { map.put(key, newValue); }
There are the newer
Map.compute()
orMap.merge()
methods to help you. For example:// input being the String Map<Character, Long> result = new HashMap<>(); for (int i = 0; i < input.length(); i++) { result.merge(Character.valueOf(input.charAt(i)), 1L, (a, b) -> a + b); }