ariken929
ariken929

Reputation: 67

How to create a new HashMap whenever a duplicate key is found?

I have a txt file containing many records, each record has 5 rows and are name-value pairs.

I have parsed the file and put the names and values into a hashmap by using a scanner to detect if the file hasNextLine.

At this point because all the records are in the same txt file, the hashmap only contains the last record in the file. This is because the any duplicate keys that comes up will overwrite the value for that key.

My question is how to create a new hashmap for each record? This is the method i have to build the records:

  public void recordBuilder() throws FileNotFoundException{
    FileReader reader = new FileReader(aFile);
    Scanner outerScan = new Scanner(reader);
    HashMap<String,String> record = new HashMap<String,String>();
    while (outerScan.hasNextLine()){
        Scanner innerScan = new Scanner(outerScan.nextLine());
        try{
            innerScan.useDelimiter(":");
            String name = innerScan.next();
            String value = innerScan.next();
            String rName = name.trim();
            String rValue = value.replaceAll("[^\\w]", "");

            record.put(rName,rValue);
        }
        catch(Exception e){    
        }
    }
    for(String i : record.keySet()){
        System.out.println(i + "\t" + record.get(i));
    }
}

Upvotes: 2

Views: 788

Answers (2)

Jim Garrison
Jim Garrison

Reputation: 86774

If I understand your question correctly, you have a text file:

 name1:value1
 name2:value2
 name3:value3
 name4:value4
 name5:value5
 name1:value6
 name2:value7
 name3:value8
 name4:value9
 name5:value10
   etc -- name1-5 repeat, with possibly different values

So what you have conceptually is a collection objects, each of which is a set of five name/value pairs, and your objective is to load all of them into memory. You have two choices:

  1. A Map<String,Map<String,String>> - Use this if each group of five name/value pairs contains within it a unique key (maybe the value associated with name1, for instance) that can be used to differentiate the sets.
  2. A List<Map<String,String>> - Use this if the groups do not have a unique key, and the file is just a linear collection of such sets of name/value pairs.

Here's an example of the first option; adapting it for the second is left as an exercise:

String                         firstKey  = "name1";
Map<String,Map<String,String>> recordset = new HashMap<String,HashMap<String,String>>();
Map<String,String>             record    = null;
String                         key       = null;

while (outerScan.hasNextLine()){
    Scanner innerScan = new Scanner(outerScan.nextLine());
    try{
        innerScan.useDelimiter(":");
        String name = innerScan.next();
        String value = innerScan.next();
        String rName = name.trim();
        String rValue = value.replaceAll("[^\\w]", "");
        if (firstKey.equals(name))
        {
            if (record != null) recordset.put(key,record);
            record = new HashMap<String,String>();
            key    = rValue;
        }
        if (record == null) throw new RuntimeException(String.format("First line was not '%s'",firstKey));
        record.put(rName,rValue);
    }
    catch(Exception e){   
        /* DO NOT IGNORE EXCEPTIONS */
    }
    if (record != null) recordset.put(key,record);
}

Upvotes: 2

Debarshi Dutta
Debarshi Dutta

Reputation: 442

HashMap provides fast retrieval of value for a particular key.Here each key is mapped to a particular value.Thus you cannot have multiple keys with the same value.

Upvotes: 0

Related Questions