Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Why map interface and its implementor has no iterator method?

 
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I just reread the K&B book for final stage before take the exam. I read in the collections chapter and I find the think that I was suspected for a long time (but I not realize it):

When you use the the map interface's implementor(e.g. HashMap or Hashtable). When you want to traverse through its elements you have to use the keySet() method to send the key element to set variable and then use the iterator() method for traversing it.

Why Map interface has no iterator() method? It will be easier to iterate the map elements if it has a iterator() method of it own.

Thank you
 
author
Posts: 23919
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

When you use the the map interface's implementor(e.g. HashMap or Hashtable). When you want to traverse through its elements you have to use the keySet() method to send the key element to set variable and then use the iterator() method for traversing it.

Why Map interface has no iterator() method? It will be easier to iterate the map elements if it has a iterator() method of it own.



Interestingly, while the keySet() and entrySet() method do return a collection object, it is actually the same collection. Both of those methods return an instance of an inner class that accesses the Map data directly. It does not "send the key element to set variable", the "set" that is returned is just a different interface to the same collection.

This is also true for the iterator that is returned from the key and entry set -- it is an instance of an inner class of the map. It accesses the map data directly.

Henry
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[Tanakorn]: Why Map interface has no iterator() method? It will be easier to iterate the map elements if it has a iterator() method of it own.

Well, Map is not a Collection, and not Iterable, therefore it has no particular reason to implement iterator(). It does, however, have three different methods that return Collection views (with iterator() methods, naturally) of some (or all) of the data in the Map:

entrySet()
keySet()
values()

Use whichever one provides the data you need. Of course entrySet() contains all the data, but if you only need keys, or only need values, then you can use the more specific method to be more efficient.
[ February 10, 2007: Message edited by: Jim Yingst ]
 
Tanakorn Numrubporn
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Henry Wong:


Interestingly, while the keySet() and entrySet() method do return a collection object, it is actually the same collection. Both of those methods return an instance of an inner class that accesses the Map data directly. It does not "send the key element to set variable", the "set" that is returned is just a different interface to the same collection.

Henry



Thank you very much. I think that I get the benefit from this post, the keySet() will return the instance of inner class of map is new knowledge for me . And I think Arrays.asList() has same behavior like keySet(), which return the instance of inner class and refer to the same data, is it right?

Thank you again.
 
Rancher
Posts: 43027
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Arrays.asList takes Object[] as parameter, so you can't pass in a Map as parameter.
 
Jim Yingst
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[Tanakorn]: And I think Arrays.asList() has same behavior like keySet(), which return the instance of inner class and refer to the same data, is it right?

Yes, like keySet(), asList() returns an instance of a List class (a static nested class in this case) which provides a view of the same data in the original array. If you call set() to change data in the List, you will chang ethe data in the array as well.

In comparison, the toArray() method in Collection does not return a view, instead it copies its data into a new array. If you change the contents of the array (e.g. setting array[0] = foo), you do not affect the original Collection. I suspect that the designers of Collection might have liked to implement toArray() using a view, but it wasn't possible since you can't create a class which extends or implements an array.
 
Tanakorn Numrubporn
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you very much.

Tanakorn Numrubporn
 
Humans and their filthy friendship brings nothing but trouble. My only solace is this tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic