473,387 Members | 1,798 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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?
Mar 30 '07 #1
13 32423
r035198x
13,262 8TB
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?
Mar 30 '07 #2
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
Mar 30 '07 #3
r035198x
13,262 8TB
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
Mar 30 '07 #4
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.
Mar 30 '07 #5
r035198x
13,262 8TB
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
Mar 30 '07 #6
dmjpro
2,476 2GB
if u have collections of hashmap....

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

while(obj.hasNext())
{
HashMap m = (HashMap)obj.next();
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 ......
Mar 30 '07 #7
dmjpro
2,476 2GB
i got the solution ......


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

all the best ........
Mar 30 '07 #8
r035198x
13,262 8TB
Guys le't keep all code in code tags.
Mar 30 '07 #9
dmjpro
2,476 2GB
sorryyyyy ... again
Mar 30 '07 #10
sorryyyyy ... again
oops sorry for tat
Mar 30 '07 #11
dmjpro
2,476 2GB
actually i again again forget to quote the code .....that's why i m sorrryyyyy
Mar 30 '07 #12
itsraghz
127 100+
two more ways you can do is..


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

Expand|Select|Wrap|Line Numbers
  1. public void testKeySet(HashMap hashMap)
  2. {
  3. Set keySet = hashMap.keySet();
  4. Iterator keySetIterator = keySet.iterator();
  5. while(keySetIterator.hasNext())
  6. {
  7. Object key = keySetIterator.next();
  8. Object value = hashMap.get(key);
  9. System.out.println("key="+key+",value="+value);
  10. }
  11. }
  12.  
Note: TypeCast the Key,Values accordingly as how the hashMap was prepared [to avoid classCastException at runtime in your applications].

Here for the purpose of demo, i have directly used java.lang.Object 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.Collection 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...
Mar 31 '07 #13
two more ways you can do is..


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

Expand|Select|Wrap|Line Numbers
  1. public void testKeySet(HashMap hashMap)
  2. {
  3. Set keySet = hashMap.keySet();
  4. Iterator keySetIterator = keySet.iterator();
  5. while(keySetIterator.hasNext())
  6. {
  7. Object key = keySetIterator.next();
  8. Object value = hashMap.get(key);
  9. System.out.println("key="+key+",value="+value);
  10. }
  11. }
  12.  
Note: TypeCast the Key,Values accordingly as how the hashMap was prepared [to avoid classCastException at runtime in your applications].

Here for the purpose of demo, i have directly used java.lang.Object 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.Collection 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
Expand|Select|Wrap|Line Numbers
  1. ArrayList<E> "voucherTransactionCols"= ArrayList<E>  (id=122)
  2.     E[] elementData= Object[10]  (id=133)
  3.         Object [0]= HashMap<K,V>  (id=135)
  4.         Object [1]= HashMap<K,V>  (id=137)
  5.             Set<Entry<K,V>> entrySet= HashMap$EntrySet  (id=150)
  6.             Set<K> keySet= null
  7.             float loadFactor= 0.75
  8.             int modCount= 2
  9.             int size= 2
  10.             HashMap$Entry<K,V>[] table= HashMap$Entry<K,V>[16]  (id=151)
  11.                 HashMap$Entry<K,V> [0]= null
  12.                 HashMap$Entry<K,V> [1]= null
  13.                 HashMap$Entry<K,V> [2]= null
  14.                 HashMap$Entry<K,V> [3]= null
  15.                 HashMap$Entry<K,V> [4]= null
  16.                 HashMap$Entry<K,V> [5]= null
  17.                 HashMap$Entry<K,V> [6]= HashMap$Entry<K,V>  (id=152)
  18.                     int hash= 361649174
  19.                     K key= "liability"
  20.                     HashMap$Entry<K,V> next= null
  21.                     V value= "A"
  22.                 HashMap$Entry<K,V> [7]= null
  23.                 HashMap$Entry<K,V> [8]= HashMap$Entry<K,V>  (id=153)
  24.                     int hash= -1446859816
  25.                     K key= "lamount"
  26.                     HashMap$Entry<K,V> next= null
  27.                     V value= ""
  28.                 HashMap$Entry<K,V> [9]= null
  29.                 HashMap$Entry<K,V> [10]= null
  30.                 HashMap$Entry<K,V> [11]= null
  31.                 HashMap$Entry<K,V> [12]= null
  32.                 HashMap$Entry<K,V> [13]= null
  33.                 HashMap$Entry<K,V> [14]= null
  34.                 HashMap$Entry<K,V> [15]= null
  35.             int threshold= 12
  36.             Collection<V> values= null
  37.         Object [2]= HashMap<K,V>  (id=138)
  38.         Object [3]= HashMap<K,V>  (id=139)
  39.         Object [4]= HashMap<K,V>  (id=140)
  40.         Object [5]= null
  41.         Object [6]= null
  42.         Object [7]= null
  43.         Object [8]= null
  44.         Object [9]= null
  45.     int modCount= 5
  46.     int size= 5
  47.  
  48.  
  49.  
on doing the following iteration i get
Expand|Select|Wrap|Line Numbers
  1. for (Iterator iter = voucherTransactionCols.iterator(); iter.hasNext();) {
  2.                         Object object = (Object) iter.next();
  3.                         HashMap campusModel = (HashMap) object;
  4.                         Set keySet = campusModel.keySet();
  5.                         Iterator keySetIterator = keySet.iterator();
  6.                         Object key = keySetIterator.next();
  7.  
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?
Apr 4 '07 #14

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Christian Gollwitzer | last post by:
Hi, I'm trying to loop over the elements in a hashmap of the STL-implementation by SGI. The point is, that because the key/value pair is stored as std::pair in the COntainer, the code becomes...
3
by: Billy Porter | last post by:
Greetings, If I populate an ArrayList with values from, let's say, the Keys enum: ArrayList a = new ArrayList(); a.Add(Keys.Home); a.Add(Keys.End); ....then how do I iterate through the...
2
by: James Doran | last post by:
Hello, I'd like to iterate through each Page of my ASP.NET project from within a Custom web control and access the Page.Controls collection. I've tried using Reflection on the web project...
7
by: Rich | last post by:
Hello, I have a form with 5 textboxes named txt0, txt1, txt2, txt3, tx4. In VB6 I could iterate through these with For i = 0 to 4 debug.print Me.controls("txt" & i).Name Next
11
by: Boni | last post by:
Dear all, following code iterates thru the hash table. Dim _Enumerator As IDictionaryEnumerator = _myhashtable.GetEnumerator While _Enumerator.MoveNext() ....
1
by: SirishNukala | last post by:
Hi .. I'm sireesh. Ive got a problem with javascript. The problem is as follows..! I've got a Textfeild and a HashMap in the jsp. The Textfeild has got a value "XYZ" and hasmap has got...
5
by: Smartpriya | last post by:
Hello, I am doing project in Swing and Access. I have some Tables in database.. Now I need to retrive some values from it and put in Hashmap. For ex: I have 3 tables like Node,...
2
by: SagarDoke | last post by:
Is it possible to put hashmap within a hashmap? My code is, while (rs.next()) { i++; current_client_id = rs.getInt(1); if(i==1)
1
by: evelina | last post by:
Hello, I need help. I have the following hashmap: HashMap<HashMap<Dimension, Integer>, String> mapList = new HashMap<HashMap<Dimension, Integer>, String>(); I want to extract Dimesion from the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.