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 › Hash Table Structure
Page Index Toggle Pages: 1
Hash Table Structure? (Read 960 times)
Hash Table Structure?
Jun 13th, 2008, 11:03pm
 
Is it possible to create a hash table in Processing? If that is not the correct term, here is what I want. I want to be able to quantify the number of objects in a list/array on the fly. So if my list/array consists of ip addresses:

209.241.119.228
141.211.250.129
97.102.93.122
97.102.93.122
208.54.94.15

I want to create the below structure where the ip addresses are the indices and the content of the index is the number of collisions so to speak, how many times that ip address occurs.

[209.241.119.228] "1"
[141.211.250.129] "1"
[97.102.93.122]   "2"
[208.54.94.15]    "1"
Re: Hash Table Structure?
Reply #1 - Jun 14th, 2008, 12:45am
 
what you want is called (in Java) a HashMap. that's how associative arrays are implemented in Java.

here is the java doc:
http://java.sun.com/j2se/1.4.2/docs/api/java/util/HashMap.html

and a simple example showing how to add elements
Quote:
HashMap hm = new HashMap();
String ip = "192.168.0.1";
Integer count = new Integer(1);
hm.put(ip, count);
// you can't put primitives in a HashMap, objects only.
// that's why I've used an Integer and not an int.
Re: Hash Table Structure?
Reply #2 - Jun 14th, 2008, 4:56pm
 
Actually, if you are using Java 1.5 or above, Java 'autoboxing' will silently convert between Number classes and their primitives. This means you can add and remove normal primitive numbers to a HashMap:

Code:

HashMap hm = new HashMap();
String ip = "192.168.0.1";
int count=1;
hm.put(ip, count);


This can make the code for manipulating HashMaps a little simpler:

Code:

String newIP = "192.168.0.1";

if (hm.containsKey(newIP))
{
count = (Integer)hm.get(newIP);
hm.put(newIP,++count);
}


Once (or if) Processing can accept Java 1.5 syntax and above, the code can be made even simpler (and safer) using generics.
Page Index Toggle Pages: 1