Following my colleague Mathieu’s excellent post “Dear Java Please Let Me Work” I’ve decided to write a small comparative between Java’s implementation of the different collection types compared to Python’s and Ruby’s. In this comparative study, I will take a slightly naive approach and put myself in the shoes of a newbie trying to learn all three languages.
First let’s look at the UML diagram of Java’s collection hierarchy to try and find out what we need to get started.

Ok. Where to start. Alright, I’ll try declaring a simple array to hold my planning poker fibonacci numbers in Java:
int[] fibonacci = { 0, 1, 3, 5, 8, 13, 21, 34 };
ArrayList<integer> fibonacci = new ArrayList<integer>();
fibonacci.add( new Integer(0) );
Vector<integer> fibonacci = new Vector<integer>();
fibonacci.add(new Integer(0));
Stack fibonacci=new Stack();
fibonacci.push(new Integer(10));
List fibonacci = new LinkedList();
fibonacci.add(new Integer(i));
Wow. Sure had a lot of choices. So many ways to do the same thing. That would be quite confusing for a newbie.
Alright let’s give Java’s dictionaries a chance.
Hashtable fibonacci = new Hashtable();
fibonacci.put( "zero", new Integer(0) );
HashMap fibonacci = new HashMap();
fibonacci.put( “One”, new Integer(1) );
enum Fibonacci{Zero, One, Two}
EnumMap<fibonnacci, integer=0> fibonacci = new EnumMap<fibonnacci, integer=0>(Fibonnacci.class);
sizeMap.put(Fibonacci.Zero,new Integer(0));
Map<string, integer=0> fibonacci = new WeakHashMap<string, integer=0>();
map.put(new String(“Zero”), new Integer(0));
Still so many choices. Still seems too complicated for my liking.
That made me wonder though, how many people actually use the WeakHashMap ? I mean seriously, who would go in a design meeting and say “Hey guys, I think we should use the weak hash map over the regular hashmap this time.” Doesn’t sound quite right does it ?
Alright, let’s see how this would look in Python.
fibonacci = [1,2,3,5,8,13,21,32]
Oh. Only one way to declare things ? Yeah there’s only one way to declare arrays, lists, stacks and queues in python. Simple as that. YOU decide how you use it. Makes sense right ?
How bout dictionaries ?
fibonacci = {"zero":0, "one":1}
Only one way too. How sweet.
Got some free time and want to try out tuples? Python actually supports tuples right out of the box. Many other language do not…
fibonacci = (("zero",0),("one",1))
Ok that went by too fast. How about some Ruby then ?
To declare an array, you can do it in either fashions :
fibonacci = Array.new([0, 1, 3, 5, 8, 13, 21, 34 ])
fibonacci = [0, 1, 3, 5, 8, 13, 21, 34 ]
Nice, simple and concise. I like that.
Now let’s move to hashes shall we ?
fibonacci =Hash["zero"=>0, "one"=>1]
fibonacci = {"zero"=>0, "one"=>1}
That was easy.
All this double teaming kinda makes me feel sorry for Java.
Nicholas Lemay