How do i iterate a collection tat consists of hashmap values?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • shiniskumar
    New Member
    • Feb 2007
    • 58

    How do i iterate a collection tat consists of hashmap values?

    Ive got a collection that is populated by some key/value pairs.
    How can i iterate ie to get its each key/value pair?
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by shiniskumar
    Ive got a collection that is populated by some key/value pairs.
    How can i iterate ie to get its each key/value pair?
    Do you have just a HashMap or a collection of HashMaps?

    Comment

    • shiniskumar
      New Member
      • Feb 2007
      • 58

      #3
      Originally posted by r035198x
      Do you have just a HashMap or a collection of HashMaps?
      collection of hashmaps added to a collection.
      i wanna iterate tat collection to get each key/value pairs

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by shiniskumar
        collection of hashmaps added to a collection.
        i wanna iterate tat collection to get each key/value pairs

        So have one hashmap with key-value pairs?

        You can use the values method to get a collection view of all the values

        Comment

        • shiniskumar
          New Member
          • Feb 2007
          • 58

          #5
          Originally posted by r035198x
          So have one hashmap with key-value pairs?

          You can use the values method to get a collection view of all the values
          actually my collection is an object[] which in which each element is a key/value pair.
          eg it is an Object[1o] where
          Object[0]=HashMap(k,v)
          Object[1]=HashMap(k,v)
          Object[2]=HashMap(k,v).. ......so on
          Now how do i iterate this.

          Comment

          • r035198x
            MVP
            • Sep 2006
            • 13225

            #6
            Originally posted by shiniskumar
            actually my collection is an object[] which in which each element is a key/value pair.
            eg it is an Object[1o] where
            Object[0]=HashMap(k,v)
            Object[1]=HashMap(k,v)
            Object[2]=HashMap(k,v).. ......so on
            Now how do i iterate this.
            You can iterate arrays using a for loop. Then for each HashMap in the array you can use the values method on it and iterate through them

            Comment

            • dmjpro
              Top Contributor
              • Jan 2007
              • 2476

              #7
              if u have collections of hashmap....

              then try to iterate the collection first ......

              while(obj.hasNe xt())
              {
              HashMap m = (HashMap)obj.ne xt();
              Collection c = m.values();
              Iterator i = c.iterator();
              while(i.hasNext ())
              {
              Object value = i.next();
              //Object key = m.get();
              //I also face the problem here to extract a key from a value.
              }
              }

              i think u also face the same problem as i face... m i right?????

              plz anyone help ...........

              thnaxxxx ......

              Comment

              • dmjpro
                Top Contributor
                • Jan 2007
                • 2476

                #8
                i got the solution ......


                while(obj.hasNe xt())
                {
                HashMap m = (HashMap)obj.ne xt();
                Set s = m.keySet();
                Iterator i = s.iterator();
                while(i.hasNext ())
                {
                Object key = i.next();
                Object value = m.get(key);
                }
                }

                all the best ........

                Comment

                • r035198x
                  MVP
                  • Sep 2006
                  • 13225

                  #9
                  Guys le't keep all code in code tags.

                  Comment

                  • dmjpro
                    Top Contributor
                    • Jan 2007
                    • 2476

                    #10
                    sorryyyyy ... again

                    Comment

                    • shiniskumar
                      New Member
                      • Feb 2007
                      • 58

                      #11
                      Originally posted by dmjpro
                      sorryyyyy ... again
                      oops sorry for tat

                      Comment

                      • dmjpro
                        Top Contributor
                        • Jan 2007
                        • 2476

                        #12
                        actually i again again forget to quote the code .....that's why i m sorrryyyyy

                        Comment

                        • itsraghz
                          New Member
                          • Mar 2007
                          • 124

                          #13
                          two more ways you can do is..


                          (i) use the keySet() method which would give you the set of keys in the Collection.

                          Code:
                          public void testKeySet(HashMap hashMap)
                          {
                          Set keySet = hashMap.keySet();
                          Iterator keySetIterator = keySet.iterator();
                          while(keySetIterator.hasNext())
                          {
                          Object key = keySetIterator.next();
                          Object value = hashMap.get(key);
                          System.out.println("key="+key+",value="+value);
                          }
                          }
                          Note: TypeCast the Key,Values accordingly as how the hashMap was prepared [to avoid classCastExcept ion at runtime in your applications].

                          Here for the purpose of demo, i have directly used java.lang.Objec t to deal with them both.


                          (ii) use the entrySet() method which is more or less the same as values() method but it returns a java.util.Set() instead of java.util.Colle ction object. In both ways, you can obtain an iterator from the returned objects and then proceed with iterator.next() with proper typecastings.


                          Hope this helps...

                          Comment

                          • shiniskumar
                            New Member
                            • Feb 2007
                            • 58

                            #14
                            Originally posted by itsraghz
                            two more ways you can do is..


                            (i) use the keySet() method which would give you the set of keys in the Collection.

                            Code:
                            public void testKeySet(HashMap hashMap)
                            {
                            Set keySet = hashMap.keySet();
                            Iterator keySetIterator = keySet.iterator();
                            while(keySetIterator.hasNext())
                            {
                            Object key = keySetIterator.next();
                            Object value = hashMap.get(key);
                            System.out.println("key="+key+",value="+value);
                            }
                            }
                            Note: TypeCast the Key,Values accordingly as how the hashMap was prepared [to avoid classCastExcept ion at runtime in your applications].

                            Here for the purpose of demo, i have directly used java.lang.Objec t to deal with them both.


                            (ii) use the entrySet() method which is more or less the same as values() method but it returns a java.util.Set() instead of java.util.Colle ction object. In both ways, you can obtain an iterator from the returned objects and then proceed with iterator.next() with proper typecastings.


                            Hope this helps...

                            my collection on expanding becomes
                            Code:
                            ArrayList<E> "voucherTransactionCols"= ArrayList<E>  (id=122)
                            	E[] elementData= Object[10]  (id=133)
                            		Object [0]= HashMap<K,V>  (id=135)
                            		Object [1]= HashMap<K,V>  (id=137)
                            			Set<Entry<K,V>> entrySet= HashMap$EntrySet  (id=150)
                            			Set<K> keySet= null
                            			float loadFactor= 0.75
                            			int modCount= 2
                            			int size= 2
                            			HashMap$Entry<K,V>[] table= HashMap$Entry<K,V>[16]  (id=151)
                            				HashMap$Entry<K,V> [0]= null
                            				HashMap$Entry<K,V> [1]= null
                            				HashMap$Entry<K,V> [2]= null
                            				HashMap$Entry<K,V> [3]= null
                            				HashMap$Entry<K,V> [4]= null
                            				HashMap$Entry<K,V> [5]= null
                            				HashMap$Entry<K,V> [6]= HashMap$Entry<K,V>  (id=152)
                            					int hash= 361649174
                            					K key= "liability"
                            					HashMap$Entry<K,V> next= null
                            					V value= "A"
                            				HashMap$Entry<K,V> [7]= null
                            				HashMap$Entry<K,V> [8]= HashMap$Entry<K,V>  (id=153)
                            					int hash= -1446859816
                            					K key= "lamount"
                            					HashMap$Entry<K,V> next= null
                            					V value= ""
                            				HashMap$Entry<K,V> [9]= null
                            				HashMap$Entry<K,V> [10]= null
                            				HashMap$Entry<K,V> [11]= null
                            				HashMap$Entry<K,V> [12]= null
                            				HashMap$Entry<K,V> [13]= null
                            				HashMap$Entry<K,V> [14]= null
                            				HashMap$Entry<K,V> [15]= null
                            			int threshold= 12
                            			Collection<V> values= null
                            		Object [2]= HashMap<K,V>  (id=138)
                            		Object [3]= HashMap<K,V>  (id=139)
                            		Object [4]= HashMap<K,V>  (id=140)
                            		Object [5]= null
                            		Object [6]= null
                            		Object [7]= null
                            		Object [8]= null
                            		Object [9]= null
                            	int modCount= 5
                            	int size= 5
                            on doing the following iteration i get
                            Code:
                            for (Iterator iter = voucherTransactionCols.iterator(); iter.hasNext();) {
                            						Object object = (Object) iter.next();
                            						HashMap campusModel = (HashMap) object;
                            						Set keySet = campusModel.keySet();
                            						Iterator keySetIterator = keySet.iterator();
                            						Object key = keySetIterator.next();
                            but i get key as "lamount" and value as "".
                            i want to gert key as "liabilty" and value as "A".
                            how do i do tis?

                            Comment

                            Working...