Creating a Map in “Ruby-style” in Java

With Ruby on Rails gaining popularity more and more people post comments about how awkward coding in Java (or .NET for that matter) is. Someone tried to explain why Java is this bad by explaining how PHP and Ruby can instantiate a Map of string and value pairs (called array in PHP). The syntax to define a Map in Ruby would be:

{
    "name" => "Tomas Salfischberger",
    "phone" => "123456789",
    "room" => "11b"
}

And for PHP:

array(
    "name" => "Tomas Salfischberger",
    "phone" => "123456789",
    "room" => "11b"
)

Now the problem with a lot of these comments is that most Java-critics can’t actually write proper Java code. There is, for example, the following method to instantiate the same Map in Java:

new HashMap() {{
    put("name", "Tomas Salfischberger");
    put("phone", "123456789");
    put("room", "11b");
}}

It is true that there is a bit more typing involved, but how many seconds does this actually cost you? And what if you look at the extra information we have provided? In Java we have actually expressed that we need a HashMap and not for example a TreeMap. That is not just more typing because Java programmers like typing, it’s actually valuable information.

Another one which might be useful (but more widely known I guess?) is this function to create a List using the Java 5 varargs feature:

Arrays.asList("value1", "value2", "something else")

These are both just simple tricks to make a programmer’s life easier, unfortunately a lot of people don’t know about them. Let’s hope the lost Ruby souls will some day find out about the simple tricks in other languages ;-)

Comments

  • Date-time November 5th, 2008 at 13:57 - Author Fun with initialization blocks - Celerity ICT - Link

    […] Tomas posted the article Creating a Map in “Ruby-style” in Java, I wanted to tell you a little more about the { } block notation, which is used in that article to […]

  • Date-time May 4th, 2010 at 05:07 - Author Alexander Ljungberg - Link

    Great tips. Still, more typing means worse readability, not better. Clear code is concise and to the point. I’ll take the Ruby/Python/JavaScript style any day.

  • Date-time December 6th, 2010 at 02:05 - Author Petras Ražanskas - Link

    @ Alexander - nonsense. Clear and well designed code is self-documenting and has NO implicit definitions or actions. The only thing you got correct is that the code must not be longer than it needs to be. After being a Python fan-boy for several years and actually writing several applications on it I realized that “industrial” Python code is anything BUT concise and to the point. The only thing it does extremely well is prototyping and throwing together something to test an idea.

  • Date-time July 10th, 2011 at 21:01 - Author JP - Link

    Cool trick - most people don’t have a good working knowledge of anonymous inner classes - they can be pretty helpful.
    The downside of course (not too big of one) is that you’re increasing the size of your code, since each time you do that a new anonymous class file gets compiled. so it may be nice on occasion, but doing it frequently could increase the size of your compiled code more than you’d want. Not only that, but in ruby/php/js this type of syntax is common - you’re gonna confuse alot of java developers using a syntax like this throughout your code base (java developers tend to dislike surprises :)

Leave a Reply