Stack Exchange network consists of 183 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.
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 the Map 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.outSystem.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:
// 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 the Map 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:
// 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 the Map 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:
// 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);
}