Skip to main content
replaced http://codereview.stackexchange.com/ with https://codereview.stackexchange.com/
Source Link

edit: @Misha@Misha's answer is a welcome improvement to mine, do take a look at that too.

edit: @Misha's answer is a welcome improvement to mine, do take a look at that too.

edit: @Misha's answer is a welcome improvement to mine, do take a look at that too.

added 145 characters in body
Source Link
h.j.k.
  • 19.2k
  • 3
  • 36
  • 93

edit: @Misha's answer is a welcome improvement to mine, do take a look at that too.

edit: @Misha's answer is a welcome improvement to mine, do take a look at that too.

added 1 character in body
Source Link
h.j.k.
  • 19.2k
  • 3
  • 36
  • 93
  • 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:

      if (map.containsKey(key)) {
          map.put(key, map.get(key) + newValue);
      } else {
          map.put(key, newValue);
      }
    

    There are the newer Map.compute() or Map.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 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:

      if (map.containsKey(key)) {
          map.put(key, map.get(key) + newValue);
      } else {
          map.put(key, newValue);
      }
    

    There are the newer Map.compute() or Map.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 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:

      if (map.containsKey(key)) {
          map.put(key, map.get(key) + newValue);
      } else {
          map.put(key, newValue);
      }
    

    There are the newer Map.compute() or Map.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);
      }
    
added 378 characters in body
Source Link
h.j.k.
  • 19.2k
  • 3
  • 36
  • 93
Loading
Source Link
h.j.k.
  • 19.2k
  • 3
  • 36
  • 93
Loading