Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

HASHMAP

 
pras
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
package prasannamaytuesday;
import java.util.*;
/**
* <p>Title: </p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright (c) 2007</p>
*
* <p>Company: </p>
*
* @author not attributable
* @version 1.0
*/

/**
* <p>Title: </p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright (c) 2007</p>
*
* <p>Company: </p>
*
* @author not attributable
* @version 1.0
*/
public class HashMapDemo {
public static void main(String[] args) {

HashMap hm = new HashMap();
hm.put("ABC", new Double(2323.34));
hm.put("BWE", new Double(234.65));
hm.put("XVF", new Double(1200.34));
hm.put("Kfl", new Double(99.34));
hm.put("Prwr", new Double( -19.34));
Set set = hm.entrySet();

Iterator i = set.iterator();

while (i.hasNext()) {
Map.Entry me = (Map.Entry) i.next();
System.out.println(me.getKey() + " : " + me.getValue());
}
if( hm.containsKey("zcz"))
{
System.out.println("key found");
}


hm.clear();

Set set1 = hm.entrySet();

Iterator i1 = set.iterator();
System.out.println("New values after Hash Map is cleared : \n");
while (i1.hasNext()) {
Map.Entry me = (Map.Entry) i1.next();
System.out.println(me.getKey() + " : " + me.getValue());
}

}
}


why is set used when we already have a hashmap?
one more doubt is
what is happening in this part of code?
Map.Entry me = (Map.Entry) i1.next();
what is an iterator?why it is used? what if we have a single key and values in hashmap?
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not an advanced question. Moving...
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An iterator is a class that provides a way to access a collection of objects one at a time but in no particular order.

The HashMap class does not directly provide such a capability because there are three possible collection of objects that the user might want - the keys, the values or the entries. The HashMap class therefore provides a way of getting collections of each of these - keySet, values and entrySet respectively. Once you have the collection that you require you can then iterate over these by calling its iterator method.

If you have a single key value pair in your HashMap, then calling HashMap.entrySet will return an iterator containing one object. So calling will return a Map.Entry object (which contains the key and value) the first time you call it and null if you call it again (because you will have iterated over the whole set). That's why it is usual to call the iterator's hasNext method before each call to next() to check if there are any objects left.
[ May 22, 2007: Message edited by: Joanne Neal ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic