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 & HelpOther Libraries › Dictionary class
Page Index Toggle Pages: 1
Dictionary class (Read 1564 times)
Dictionary class
Sep 12th, 2007, 6:40pm
 
Hello,

I am trying to find code for a dictionary class, similar to:

http://java.sun.com/j2se/1.4.2/docs/api/java/util/Dictionary.html

or also-known-as:

http://en.wikipedia.org/wiki/Associative_array

in order to associated key/value pairs with fast hash table lookup. Does anyone know of any code for this that will work with Processing?

Thanks,
John B.
Re: Dictionary class
Reply #1 - Sep 12th, 2007, 7:02pm
 
import java.util.HashMap;
HashMap hm = new HashMap();
hm.put("mykey","myvalue");
println(hm.get("mykey"));
Re: Dictionary class
Reply #2 - Sep 12th, 2007, 9:43pm
 
Thanks, Dave!

I've never used Java before Processing (and I've only used Processing for a day), but I poked around a bit and couldn't immediately see how to use anything other than String key/value pairs in Processing.

<googles>

Sun's documentation for the HashMap class defines the params for HashMap.Put() as such: Object key, Object value, so I assume that when your example, above, uses Strings, some sort of implicit coercion is occurring or perhaps Java's strings are objects and Integers are primitive types (not objects).

<fiddles>

Let's try this:

Code:

import java.util.HashMap;
import java.lang.Number;

HashMap hm = new HashMap();

hm.put(new Integer(7),new Integer(10));
println(hm.get(new Integer(7)));

Well, storing the Integer works when I wrap it in an Integer object, but that seems pretty circuitous. Even doing the following to coerce Integers to Strings:

Code:

hm.put(str(7),str(10));
println(hm.get(str(7)));

Leaves me a bit concerned about performance -- I'm hoping there is an easier way, perhaps there is a Java class that will store Integer/Integer key/value pairs or Integer/Object key/value pairs. Perhaps I will have to roll my own Dictionary/HashMap class?

Thanks,
John B.
Re: Dictionary class
Reply #3 - Sep 12th, 2007, 11:57pm
 
>> new Integer(10)

Yep, that would be the "right" way to do it.

int i = 10; // a primitive
Integer j = new Integer(10); // an object

It may seem cumbersome but it's not really, given the flexibility it exposes:  you can use any combination of objects as keys in a single hash.  (you could even use another HashMap!)  Strings work because they too are objects.

>> roll my own Dictionary/HashMap class?

Now THAT sounds circuitous!  Cheesy

Seriously though, there are plenty of other data structures (and if your keys are all ints, maybe even just a simple array would do) and one is likely to be close enough to use as is.
Page Index Toggle Pages: 1