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 & HelpPrograms › Map, HashMap etc.
Page Index Toggle Pages: 1
Map, HashMap etc. (Read 2396 times)
Map, HashMap etc.
Nov 27th, 2006, 8:18pm
 
I already submitted this to the bugzilla but I thought that someone might have a workaround as I'm under some time pressure with the project I'm working on.

---snip---
Code:
Map<String, Integer> m = new HashMap<String, Integer>();

Results in:
unxpected token: <


Also Code:
Map m = new HashMap();
m.put("fish", 2);

Results in:
Semantic Error: No applicable overload for a method with signature
"put(java.lang.String, int)" was found in type "java.util.Map". Perhaps you
wanted the overloaded version "java.lang.Object put(java.lang.Object $1,
java.lang.Object $2);" instead?


My guess is that all this bug needs is a small fix in the semantics check
of the precompiler so that it accepts the <Key, Value> of the Map
constructors.
---snip---

Any suggestions for a workaround much appreciated.
Re: Map, HashMap etc.
Reply #1 - Nov 27th, 2006, 8:38pm
 
the workaround is to use another environment such as eclipse and go the full java route.

unfortunately, it's not at all a small fix. preprocessors and compilers are nasty things.
Re: Map, HashMap etc.
Reply #2 - Nov 27th, 2006, 8:45pm
 
Ah, yes. I thought it was a part of the Java 1.4 and just a bug in the Processing editor. My bad, sorry about that.

Now I just have to make you move this thread over to Syntax so I can get some help to find out how to construct and use HashMaps properly in Processing and Java 1.4 then. Wink


(Just kidding. I'm of course already googling for a 1.4 example. Smiley )
Re: Map, HashMap etc.
Reply #3 - Nov 27th, 2006, 9:52pm
 
In case someone else wants to do HashMaps in Processing, this is how you do it.

Code:
Map m = new HashMap();
m.put("fish", new Integer(3));
println(2 + parseInt(m.get("fish").toString()));

Re: Map, HashMap etc.
Reply #4 - Nov 29th, 2006, 12:21pm
 
Just to reinforce what mkn is saying, the Java HashMap stores and retrieves only objects, so in order to store any primitives, you must put your value into a wrapper, such as Integer, String, or some custom-made wrapper.

(Just one more reason to love Ruby, IMO)
Re: Map, HashMap etc.
Reply #5 - Nov 29th, 2006, 12:49pm
 
FYI, this is no longer a mess with Java 1.5+.

By the way, isn't Ruby in a totally different universe than Processing as its mainly used for server side scripting and web applications?
Re: Map, HashMap etc.
Reply #6 - Nov 29th, 2006, 3:10pm
 
matthew.thompson wrote on Nov 29th, 2006, 12:21pm:
Just to reinforce what mkn is saying, the Java HashMap stores and retrieves only objects, so in order to store any primitives, you must put your value into a wrapper, such as Integer, String, or some custom-made wrapper.

(Just one more reason to love Ruby, IMO)

primitive types are kept separate because objects have significantly more overhead than primitive types. but this is problematic when storing primitive types because java's hashmap et al require objects so that the values can be passed by reference.

if everything in java were objects (presumably how ruby treats things) then it would be too slow for interactive graphics. i don't know if ruby has done something clever, but my guess is that ruby is too slow for the type of work for which processing is designed. or at least the pixel flipping sort of things, as its speed is probably more on par with python or ecma/action/javascript.
Re: Map, HashMap etc.
Reply #7 - Nov 30th, 2006, 3:23am
 
fry wrote on Nov 29th, 2006, 3:10pm:
primitive types are kept separate because objects have significantly more overhead than primitive types. but this is problematic when storing primitive types because java's hashmap et al require objects so that the values can be passed by reference.

if everything in java were objects (presumably how ruby treats things) then it would be too slow for interactive graphics. i don't know if ruby has done something clever, but my guess is that ruby is too slow for the type of work for which processing is designed. or at least the pixel flipping sort of things, as its speed is probably more on par with python or ecma/action/javascript.


You raise the correct point that primitives are more efficient and less computationally expensive than pure OO, but the topic itself is an interesting avenue that I think will become more important as time goes on.

[rant]
Despite the inherent computational benefits of using primitives, I am inclined to believe that as Moore's law continues to hold for modern processors, the value in most programming will stem from development time rather than initial performance benefits. Ruby does well in being expressive, elegant, and very logical, which lends itself to manifesting working models of whatever you happen to envision within a program.

With Ruby 2.0 to increase its speed tremendously with a new VM, and with the ability to streamline functional components with C code, I predict Ruby, and it's kind (Python, Perl, Lua, etc.) will gain more footing in this sort of graphically intensive application. Projects like NodeBox  http://www.nodebox.net/ are encouraging examples of this sort of thing.

If nothing else, maybe we will all reach a compromise when JRuby development matures, and it gets fast enough to use for intensive applications.
[/rant]
Re: Map, HashMap etc.
Reply #8 - Nov 30th, 2006, 9:55am
 
In an ideal world, all computers would be fast enough to do whatever you wanted to do. In reality, however, they are almost never fast enough because programmers and users keep wanting more.

NodeBox and most other examples of interpreter-based graphics tools work fine because they produce files, rather than realtime visuals. In Java, simply initializing an object costs  you precious time in a real-time situation. In non-realtime applications this cost is more acceptable because it only occurs once. Hence, non-realtime systems can easily exploit approaches that are slightly "wasteful" as long as they ease development or produce better results.

Some references for optimal use of objects in realtime applications:
http://www.microjava.com/articles/techtalk/recylcle2
http://java.sys-con.com/read/45500.htm
Re: Map, HashMap etc.
Reply #9 - Nov 30th, 2006, 9:20pm
 
wake me up when computers are fast enough to do whatever i want to do Smiley
http://processing.org/faq.html#whyjava
Re: Map, HashMap etc.
Reply #10 - Nov 30th, 2006, 10:33pm
 
fry wrote on Nov 30th, 2006, 9:20pm:
wake me up when computers are fast enough to do whatever i want to do Smiley
http://processing.org/faq.html#whyjava


We are closing up, but you keep wanting more all the time. Smiley

Page Index Toggle Pages: 1