0

I'm struck with my project... have to maintain large hashmap of lang arrays.... i try this with sample but its not working.... I cant store keys in arrays.... thanks in advance...

class K{
  int key;
}

class V{
  int data;
}

class hashmp{
  public static void main(String args[]){
       HashMap<K,V> hm=new HashMap<K,V>();
       K key1=new K();
       for(int i=0;i<5;i++){    
         V val=new V();
         key1.key=i;
         val.data=i+5;
         hm.put(key1,val);
       }
       System.out.println();
       for(int i=0;i<5;i++){
         key1.key=i;
         V pt=hm.get(key1);
         System.out.println("\n"+hm.containsKey(key1)+key1.key);    
         if(hm.containsKey(key1))       
            System.out.print(pt.data);
       }
   }
 }

for that i'm getting....

true0
9
true1
9
true2
9
true3    
9  
true4    
9

updated code.....

   class K{ 
       long key;   
   }   

   class V{ 
      long[] v=new long[10];      
  }                                   

  public void  putHash(V val1){      
         //some code         
     V s=new V();               
     K gt=new K();            
     gt.key=val1.v[0];                
    if(hm.containsKey(keyArr[(int)gt.key])){         
                 s=hm.get(gt);    //get value of key if exists              
       //some code to modify s               
    }                 
   gt.key=val1.v[0];             
   hm.put(gt,s);  // put the modified value back to same key    
}      

Thank you all to provide me the answers... finally it works well... Thanks for everyone's contribution....

Guna Gadin
  • 17
  • 3
  • You have just an instance of `Key` class, so all of them equals (first condition in default implementation of `equals` method is being the same object). Move `new Key()` inside the loop, and everything goes fine (by the way it is much better to override `equals` method for a map key). – Amir Pashazadeh Feb 09 '14 at 06:43
  • Is that enough to use **get()** method to lookup my entries.... I want to retrieve data of specific key.... – Guna Gadin Feb 09 '14 at 13:14

4 Answers4

1

HashMap Stores the reference of your key. So If you change the key it will be changed in the HashMap. If you want this to change move the statment K key1=new K(); inside the for loop.

ahmedalkaff
  • 313
  • 1
  • 3
0

first do the following :

HashMap<K, V> hm = new HashMap<K, V>();
            K key1 = null;
            for (int i = 0; i < 5; i++) {
                key1 =new K();
                V val = new V();
                key1.key = i;
                val.data = i + 5;
                hm.put(key1, val);

            }

i think the problem is in the way you iterate through the items in the HashMap

try to do the following to iterate through the Map :

Iterator it = hm.entrySet().iterator();
            while (it.hasNext()) {
                Map.Entry pairs = (Map.Entry)it.next();
                System.out.println(((K)pairs.getKey()).key + " = " + ((V)pairs.getValue()).data);

                it.remove(); // avoids a ConcurrentModificationException
            }

and you will get the correct Values .

or you can refer to this answer here to see how to iterate through your keys or values .

and give me some feedback

Hope That Helps .

Community
  • 1
  • 1
  • He keeps using the same key object; there really is only one entry in the map. – chrylis -cautiouslyoptimistic- Feb 09 '14 at 10:38
  • That will let him iterate, and so it answers the question as asked, but since he's using reference equality on his keys, he's going to have trouble doing any lookups; on this kind of question, it's helpful to answer the underlying problem (in this case, writing his own `int` wrapper) if you can. – chrylis -cautiouslyoptimistic- Feb 09 '14 at 11:11
  • I have to store long arrays as values of hashmap for every key.... **HashMap hm=new HashMap**... I also want to modify individual element(eg: data[3] alone to be modified under a key) in array and store them back in hashmap.... Is Iterator helpful in this case.... thanks in advance.. – Guna Gadin Feb 09 '14 at 12:48
0

You're using the same K key1 object on each loop iteration, and you're modifying that same object even though it's already in the map. You need to create a different key K for each entry in the map (or just use Integer directly instead of making your own key class, which needs at least an equals and hashCode more than you have)--or, if you're doing everything sequentially numbered, just an array: V vals[].

chrylis -cautiouslyoptimistic-
  • 75,269
  • 21
  • 115
  • 152
0

For every entry you put in your map, you have to instantiate the key, otherwise your last entry will be overwritten.

ali4j
  • 522
  • 3
  • 15