0

Usually when you have multiple values for the same key in hash map the answers I have found would use a List or ArrayList to store the value part. For example:

Map<Object,ArrayList<Object>> multiMap = new HashMap<Object,ArrayList<Object>>();

I was wondering it was possible to keep the normal definition of hashmap and append any new values to the old value (comma separated). Something like this:

Map<String, String> memOf = new HashMap<String, String>();
    Map<String, String> subOrg = new HashMap<String, String>();
    Map<String, String> email = new HashMap<String, String>();

 String line="";
    String source="";
    String sub="";
    String obj="";
    String hashKey="";
    String hashVal="";


    for (Text value : values){ //start iterate over values 
        line=value.toString();
        String[] parts=line.trim().split(",");



    source=parts[0].trim();
    sub=parts[1].trim();
    obj=parts[2].trim();
    hashKey=sub;



    if (source.equals("memberOf")){
        hashVal=memOf.get(hashKey);
        if (hashVal!=null){
        hashVal=hashVal+","+obj;
        memOf.put(hashKey, hashVal);
        } 
    }

}

The previous code is to populate the hashmaps and next to read individual values use split(",")and store them in a String array. something like:

for (String key1 : memOf.keySet()) {             
    x=key1;
    String[] y=memOf.get(x).toString().split(",");

    int numberOfItems = y.length;
        for (int i=0; i<numberOfItems; i++){  
    System.out.println(y[i]); 
    }
}
zaranaid
  • 65
  • 1
  • 13
  • What about if you want to store a value containing a comma? – Andy Turner Dec 12 '17 at 07:51
  • 4
    And how could this be a better idea than using a list as value. Now you need to split every time you want to get or remove an element, and you have to deal with values containing commas somehow. It's more cumbersome to use, and less efficient. There is really no reason to do that. – JB Nizet Dec 12 '17 at 07:53
  • It seems you've already done it. What is preventing you here? – Dogukan Zengin Dec 12 '17 at 07:53
  • I think OP asks for an existing way to do so, something like `map.addValueToExisting(value);`. He is doing it in a non-efficient manner, and asks if it is possible to do so otherwise. – Yassine Badache Dec 12 '17 at 07:55
  • 1
    Perhaps look into multimaps, e.g. [Guava's](https://google.github.io/guava/releases/23.0/api/docs/com/google/common/collect/Multimap.html) – Andy Turner Dec 12 '17 at 07:57
  • If you look that way, then you dont need Map also. You can do it in String array and have first value as key and remaining as values. – Santosh Dec 12 '17 at 07:57
  • I see literally no reason why you would do this. Use a `Map>` and the new `Map.computeIfAbsent` method. – Boris the Spider Dec 17 '17 at 09:45
  • If @Yassine is correct, then you are looking for `Map.merge`. – Boris the Spider Dec 17 '17 at 09:47

1 Answers1

0

I was wondering it was possible to keep the normal definition of hashmap and append any new values to the old value (comma separated).

You already did it. In an inefficient manner, but you did it.

As pointed out by Andy Turner in the comments, you may look for implementations like Guava's MultiMap which allows to map a single key to multiple values.

Aside from that, the official Map documentation does not report such capabilities of adding one value to another, only replacing it. In the case of Integers, you can increment them pretty easily, but for the rest you'll have to either do it yourself or find a more efficient way (e.g. an ArrayList).

Yassine Badache
  • 1,810
  • 1
  • 19
  • 38