Iterating Over a Map

which would you choose

Page 1 of 1

3 Replies - 702 Views - Last Post: 12 March 2010 - 05:01 AM Rate Topic: -----

#1 cfoley   User is offline

  • Cabbage
  • member icon

Reputation: 2410
  • View blog
  • Posts: 5,050
  • Joined: 11-December 07

Iterating Over a Map

Posted 12 March 2010 - 04:36 AM

Here are two pieces of code for iterating over a map.

This one goes through the keys and gets the values. Standard map notation. Easy to follow but maybe a little slow.

	<K,V>void doStuffWithMapA(Map<K,V> myMap) {
		for(K key : myMap.keySet()) {
			V value = myMap.get(key);
			// do stuff with key and value
		}
	}


This one just takes the key/value pairs and then uses getters to separate them. Slightly less standard. An extra line of code, maybe faster.

	<K,V> void doStuffWithMapB(Map<K,V> myMap) {
		for(Map.Entry<K,V> entry : myMap.entrySet()) {
			K key = entry.getKey();
			V value = entry.getValue();
			// do stuff with key and value
		}
	}


Which one would you use and why? Or would you do something different.

Is This A Good Question/Topic? 0
  • +

Replies To: Iterating Over a Map

#2 m-e-g-a-z   User is offline

  • Winning
  • member icon


Reputation: 497
  • View blog
  • Posts: 1,457
  • Joined: 19-October 09

Re: Iterating Over a Map

Posted 12 March 2010 - 04:47 AM

What i would do is use an Iterator to iterate through each keySet and value having a while loop.

This post has been edited by m-e-g-a-z: 12 March 2010 - 04:48 AM

Was This Post Helpful? 0
  • +
  • -

#3 cfoley   User is offline

  • Cabbage
  • member icon

Reputation: 2410
  • View blog
  • Posts: 5,050
  • Joined: 11-December 07

Re: Iterating Over a Map

Posted 12 March 2010 - 04:53 AM

you mean like this?
Can I ask why you choose that way?

	<K,V>void doStuffWithMapC(Map<K,V> myMap) {
		Iterator<K> keyIterator = myMap.keySet().iterator();
		while(keyIterator.hasNext()) {
			K key = keyIterator.next();
			V value = myMap.get(key);
			// do stuff with key and value
		}
	}


Was This Post Helpful? 0
  • +
  • -

#4 m-e-g-a-z   User is offline

  • Winning
  • member icon


Reputation: 497
  • View blog
  • Posts: 1,457
  • Joined: 19-October 09

Re: Iterating Over a Map

Posted 12 March 2010 - 05:01 AM

I use them for "better" design purposes really and for some strange reason, been using them alot lately as ive been doing alot of jdbc. When it comes to Time Complexity, if i have alot of data within the map, then the for loop would deffinately be a better choice. You will deffinately notice a difference in using an Iterator and for loop when you have for example 10000 numbers you are going through. The for loop would go through without checking if their is a next as it uses the maps size.

It all depends on how much data is in the map.

The while loop is considerably more legible.
The for loop minimizes the scope of the Iterator to the loop itself.


Edit - typo.

This post has been edited by m-e-g-a-z: 12 March 2010 - 05:20 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1