This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Beginning Java and the fly likes HASHMAP Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "HASHMAP" Watch "HASHMAP" New topic
Author

HASHMAP

pras
Ranch Hand

Joined: Apr 04, 2007
Posts: 188
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

Joined: Apr 14, 2004
Posts: 10336

Not an advanced question. Moving...


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3487
    
  13
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 ]

Joanne
 
jQuery in Action, 2nd edition
 
subject: HASHMAP