Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

collctions

 
sudhakar naga
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi sir,

How to iterate the map collection.


please give me good solution
 
Vijitha Kumara
Bartender
Pie
Posts: 3918
10
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hint: Check the Map API
 
sudheer kiran
Ranch Hand
Posts: 237
Firefox Browser MyEclipse IDE Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
remember is not a collection of objects
its a collection of combination of keys and values
so you can iterate thru keys and get corresponding values.
or get a value set and iterate each one .

 
learning Freak
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Get the values contained in the Hashmap using the values() of HashMap class which returns Collection object.

Collection c = hmap.values();

obtain iterator for the collection.

Iterator it = c.iterator();

Iterate through Hashmap values iterator

while(it.hasNext()){
System.out.println(it.next());
}

 
learning Freak
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One more way Iterating with the Keys .

keySet() returns the set of keys contained in HashMap object .

Set s = hmap.keySet();

Iterator it = s.iterator();

while(it.hasNext()){
System.out.println(it.next());
}
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@learning Freak: Please see the JavaRanch naming policy and change your display name to conform with this policy. Thanks!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic