HashMap: adding values ​​using shared keys and listing them

I have a file that has a String in the form of key/value , like people and a counter, for example,

 "Reggy, 15" "Jenny, 20" "Reggy, 4" "Jenny, 5" 

and in the output I had to sum all the account values ​​based on the key, so for our example the output would be

Reggie 19 Jenny 25

Here is my approach:

  • Read each line and for each line enter the key and count with a scanner and , as a separator
  • Now let's see if the key is already present, if then just add currentValues ​​to the previous values, if not, then accept currentValue as the HashMap value.

Implementation Example:

 public static void main(final String[] argv) { final File file = new File("C:\\Users\\rachel\\Desktop\\keyCount.txt"); try { final Scanner scanner = new Scanner(file); while (scanner.hasNextLine()) { if (scanner.hasNext(".*,")) { String key; final String value; key = scanner.next(".*,").trim(); if (!(scanner.hasNext())) { // pick a better exception to throw throw new Error("Missing value for key: " + key); } key = key.substring(0, key.length() - 1); value = scanner.next(); System.out.println("key = " + key + " value = " + value); } } } catch (final FileNotFoundException ex) { ex.printStackTrace(); } } 

Partly, I don’t understand how to separate a key / value pair by reading them and creating based on HashMap.

Also, the proposed approach is optimal, or is there a way to increase productivity.

+4
source share
6 answers

Since this is almost certainly a training exercise, I will avoid writing code, letting you have fun.

Create a HashMap<String,Integer> . Each time you see a key / value pair, check if the hash map has a value for the key (use "containsKey (key)"). If so, get this old value with get(key) , add a new value and save the result with put(key, newValue) . If there is no key yet, add a new one - again using put . Remember to make int if String value (use Integer.valueOf(value) for this).

As for optimization, any optimization at this stage would be premature: it does not even work! However, it’s hard to get much faster than the one loop you have, which is also pretty simple.

+8
source

For reading, personally, I would use:

Scanner.nextLine() , String.split(",") and Integer.valueOf(value)

+2
source

Try the following:

 Map<String, Long> map = new HashMap<String, Long>(); while (scanner.hasNextLine()) { if (scanner.hasNext(".*,")) { .... if(map.containsKey(key)) map.put(key, map.get(key) + Long.valueOf(value)); else map.put(key, Long.valueOf(value)); } } 
+2
source

The easiest thing I can think of is separation of values:

  BufferedReader reader = new BufferedReader(new FileReader(file)); Map<String, Integer> mapping = new HashMap<String,Integer>(); String currentLine; while ((currentLine = reader.readLine()) != null) { String[] pair = currentLine.split(","); if(pair.length != 2){ //could be less strict throw new DataFormatException(); } key = pair[0]; value = Integer.parseInt(pair[1]); if(map.contains(key)){ value += map.get(key); } map.put(key,value); } 

This is most likely not the most efficient way in terms of performance, but rather simple. Scanner usually used for parsing, but parsing doesn't look so complicated here, it's just a line break.

+2
source

Kind of a late but clean solution with time complexity O (n). This solution bypasses several arrays

  public class Solution { public static void main(String[] args) { // Anagram String str1 = "School master"; String str2 = "The classroom"; char strChar1[] = str1.replaceAll("[\\s]", "").toLowerCase().toCharArray(); char strChar2[] = str2.replaceAll("[\\s]", "").toLowerCase().toCharArray(); HashMap<Character, Integer> map = new HashMap<Character, Integer>(); for (char c : strChar1) { if(map.containsKey(c)){ int value=map.get(c)+1; map.put(c, value); }else{ map.put(c, 1); } } for (char c : strChar2) { if(map.containsKey(c)){ int value=map.get(c)-1; map.put(c, value); }else{ map.put(c, 1); } } for (char c : map.keySet()) { if (map.get(c) != 0) { System.out.println("Not anagram"); } } System.out.println("Is anagram"); } } 
0
source
 public Map<String, Integer> mergeMaps(@NonNull final Map<String, Integer> mapOne, @NonNull final Map<String, Integer> mapTwo) { return Stream.of(mapOne.entrySet(), mapTwo.entrySet()) .flatMap(Collection::stream) .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, Integer::sum)); } 
0
source

All Articles