We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › Understanding HashMap
Page Index Toggle Pages: 1
Understanding HashMap (Read 526 times)
Understanding HashMap
Jul 2nd, 2009, 11:37pm
 
hi im trying to understand how hashmap works. In the reference it says it stores a collection of objects but in the example it stores strings. I also tried to store numbers and it works  why is that? does objects in this cases  doesnt  mean isntances of a class?
Is this hashmap the same as dictionary?

and my last question is : i been trying to understand the example of hashmap in the reference but there are some commands that doenst have reference like: entrySet, Map, Entry , next .

Can anybody please translate how does the code works, and what does this commands does?


many thanks



here is the code:
Code:

HashMap hm = new HashMap();

hm.put(22, 1);
hm.put("Cait", 35);
hm.put("Casey", 36);

Iterator i = hm.entrySet().iterator(); // Get an iterator

while (i.hasNext()) {
Map.Entry me = (Map.Entry)i.next();
print(me.getKey() + " is ");
println(me.getValue());
}




Re: Understanding HashMap
Reply #1 - Jul 3rd, 2009, 1:58am
 
Lot of questions... (I like it!)

OK, let's take them one by one:

- String are plain objects (with special status in Java, overriding + and having a special init syntax...).
- In old versions of Java, you would have an error on your code. Since Java 1.5, you are lucky, it automatically wraps your numbers in objects (class Integer) and unwraps them when you get them.
It is called autoboxing (putting in a box) and unboxing.
That's one of the rare new features of Java 1.5 we can use in Processing (no syntax change).

You can find the complete reference on HashMap, Map and other classes in the Java™ Platform, Standard Edition 6  API Specification.

In short, Iterator allows... to iterate on a Collection, Map is an interface, a kind of blueprint for related Collections like HashMap (which is a concrete implementation) while Map.Entry is just a key-value pair definition.

Also print/println and string concatenation just use the toString() method all objects in Java has, that's why it works whatever the kind of objects you have used as key or value.
Page Index Toggle Pages: 1