HashMap with key and value equal the values of two other hashmaps using Java

Go To StackoverFlow.com

0

I have two hashmaps and I would like to fill a third hashmap which keys will be the values of the first hash map and the values will be the values of the second hashmap splitted to an array. i.e.:

hashmap1 = {1=e1, 2=e2}
hashmap2 = {10=word1-word2-word3, 20=word4-word5-word6}
the result:
hashmap3 = {e1=word1-word2-word3, e2=word4-word5-word6}

This is what I did so far:

  static HashMap<Integer, String> catnamecatkeys = new HashMap<Integer, String>();

    static HashMap<Integer, String> keywords = new HashMap<Integer, String>();

    static HashMap<String, String> tempHash = new HashMap<String, String>();

    static HashMap<String, String[]> hash = new HashMap<String, String[]>();

    static String[] arr;

    public static void main(String[] args) {

    catnamecatkeys.put(1, "e1");
    catnamecatkeys.put(2, "e2");
    keywords.put(1, "word1-word2-word3");
    keywords.put(2, "word4-word5-word6");

    for (int key : catnamecatkeys.keySet()) {
        tempHash.put(catnamecatkeys.get(key),null);
    }

    for(String tempkey: tempHash.keySet()){          
        tempHash.put(tempkey,keywords.entrySet().iterator().next().getValue());
        arr = tempHash.get(tempkey).split("-");
        hash.put(tempkey, arr);
    }
    System.out.println(tempHash);
    for (String hashkey : hash.keySet()) {
        for (int i = 0; i < arr.length; i++) {
            System.out.println(hashkey + ":" + hash.get(hashkey)[i]);
        }


       }

    }

but the output is:

hashmap3 = {e1=word1-word2-word3, e2=word1-word2-word3}

Any Ideas please?

2012-04-05 17:43
by Daisy
Are you trying to relate the items in the map by their order of insertion? Once inserted into the HashMap class, the order of iteration is not equal to the order of insertion - Trevor Freeman 2012-04-05 17:59
If you are trying to relate the items in the map by their order of insertion (and not via the same key), then a HashMap will not work for you because the order of the iterator is not based on the order of insertion (the order of iteration is unspecified) - Trevor Freeman 2012-04-05 20:04


1

You should initialize Iterator outside the loop, Here is complete example -

static HashMap<Integer, String> catnamecatkeys = new HashMap<Integer, String>();

static HashMap<Integer, String> keywords = new HashMap<Integer, String>();

static HashMap<String, String> tempHash = new HashMap<String, String>();

static HashMap<String, String[]> hash = new HashMap<String, String[]>();

static String[] arr;
public static void main(String[] agrs)
{     
   catnamecatkeys.put(1, "e1");
        catnamecatkeys.put(2, "e2");
        keywords.put(1, "word1-word2-word3");
        keywords.put(2, "word4-word5-word6");

        for (int key : catnamecatkeys.keySet()) {
            tempHash.put(catnamecatkeys.get(key),null);
        }
     Set<Entry<Integer,String>> set =  keywords.entrySet();
      Iterator<Entry<Integer, String>>  iterator= set.iterator();
        for(String tempkey: tempHash.keySet()){          
            tempHash.put(tempkey,iterator.next().getValue());
            arr = tempHash.get(tempkey).split("-");
            hash.put(tempkey, arr);
        }
        System.out.println(tempHash);
        for (String hashkey : hash.keySet()) {
            for (int i = 0; i < arr.length; i++) {
                System.out.println(hashkey + ":" + hash.get(hashkey)[i]);
            }


           }
}
2012-04-05 18:04
by kundan bora


1

Your problem is this line:

keywords.entrySet().iterator().next().getValue()

is always going to return the same entry of the keywords HashMap. Try building your new hashmap with something like:

for (int i = 1; i < 3; i++) {
    tempHash.put(catnamecatkeys.get(i), keywords.get(i));
}
2012-04-05 17:56
by Tim Jones


0

According to your example where you have:

hashmap1 = {1=e1, 2=e2}
hashmap2 = {10=word1-word2-word3, 20=word4-word5-word6}
the result:
hashmap3 = {e1=word1-word2-word3, e2=word4-word5-word6}

There is no common key between hashmap1 and hashmap2, so we are trying to relate the value from hashmap1 with key "1" to the value in hashmap2 with key "10". There is no way to do this unless additional information about how to map the entries from hashmap1 to hashmap2 is retained. This additional information could be the insertion order into the map if a map that guarantees iteration order to be the same as insertion order is used (e.g. LinkedHashMap).

2012-04-05 20:20
by Trevor Freeman
Ads