1

I have two linkedHashMaps: First sorted linkedhashmap1 (Integer, Double); second linkedhashmap2(Integer, Object).

I want to join both linkedHashmaps to get Sorted linkedhashmap3(Integer, Object) in such a way that both linkedhashmap1 and linkedhashmap2 will get joined at Integer value which is the key and are the same. The number of records in both linkedhashmaps is the same.

Adnan Ali Ch.
  • 111
  • 1
  • 9
  • 1
    There is no such thing as "sorted hashmap". Also, what is a question precisely? What have you tried? – kan Feb 28 '12 at 11:42
  • Impossible to understand what is being asked, "...and the is same.", what does that mean? What are you trying to accomplish, what have your tried, what problems are you having? – pap Feb 28 '12 at 11:46
  • 1
    Do you mean LinkedHashMap? What should be the type of the *value* in the hashmap3? – Sergey Kalinichenko Feb 28 '12 at 11:46
  • I am sorry its LinkedHashMap instead of hashmap..! – Adnan Ali Ch. Feb 28 '12 at 11:48
  • Please edit your question to reflect this. So both LinkedHashMaps are sorted how? You added the items in an already sorted order? Maybe give a minimal code example – Tim Feb 28 '12 at 11:58
  • Actually I need some Objects to be extracted from Database in my LinkedHashMap2 containing attributes(id, Geometry, length) in Objects which are stored in LinkedHashMap2 and also I have another LinkedHashMap1 which contains(id, length) which is sorted linkedhashmap1. Now I have to get linkedhashmap3 from linkedHashmap1 joining linkedhashmap2 in a way, that I will resultant get linkedhashmap3...so that I can use geometry information from this linkedhashmap... – Adnan Ali Ch. Feb 28 '12 at 11:58
  • linkedhashmap3 contains Objects which are sorted based on id as key... – Adnan Ali Ch. Feb 28 '12 at 12:04

3 Answers3

1

Make a copy of hashmap2 and call it hashmap3. Then iterate thru all elements of hashmap1 and add it to hashmap3, by casting each Double value to an Object instance.

For example:

// create a new map
Map<Integer, Object> hashmap3 = new HashMap<Integer, Object>();
// add all elements in hashmap2 to hasmap3
hashmap3.putAll(hashmap2);

// iterate thru all elements of hashmap1
for (Map.Entry<Integer, Double> entry : hashmap1.entrySet()) {
    // and add each element with the same key,value pair to hashmap3
    hashmap3.put(entry.getKey(), (Object)(entry.getValue()));
}

Please note that if hashmap1 and hashmap2 share some common keys, value of hashmap2[key] will be overwritten by the value of hashmap1[key].

Murat Derya Özen
  • 2,154
  • 8
  • 31
  • 44
0

A fast and dirty approach would be :

. Make a new Map (hashmap3)
. Loop through the hashmap1 
. Register every item of the hashmap1, setting the key-value pairs.
. Loop through the hashmap2 map and for each item in hashmap2
   . Compare the values of the items in hashmap2 and hashmap3 of the same index  
   . If the value of hashmap2 is less than the value of the same index of hashmap3 
      . Put it in hashmap3 with the same index number it had in hashmap2
      . Shift each item in hashmap3 by 1 that have greater value than the previously inserted item.
   . If the value of hashmap2 is greater than the value of the same index of hashmap3
      .Loop in hashmap3 until you find the first item that is greater than the value of the item in hashmap2
      .Put new item on the same index you've found previously.
      .Shift each remaining item by 1 in hashmap3 that have greater value than the previously inserted. 
Korhan Ozturk
  • 11,148
  • 6
  • 36
  • 49
0

Here is how you can do it using a Pair<A,B> class from this answer:

This code assumes that each key is present in both maps.

Map<Integer, Pair<Double,Object> > hashmap3 =
    new LinkedHashMap<Integer, Pair<Double,Object> >();

for (Map.Entry<Integer, Double> entry : hashmap1.entrySet()) {
    Integer key = entry.getKey();
    Object obj = hashmap2.get(key);
    hashmap3.put(key, new Pair<Double,Object>(entry.getValue(), obj));
}
Community
  • 1
  • 1
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523