Syntax support for collections?

by Dejan Bosanac

It is common to use final static fields to define constants in your Java applications. For example, you will often find the code similar to this in various Java APIs

public class Example {
public static int one = 1;
}


which is used in the following manner

System.out.println(Example.one);


Of course, Java supports more complex initialization of static properties through static initializer blocks. One of the most common usages for these blocks is initialization of static collections with known values.

Take a look at the following code for example:

public class Example {
public final static HashMap constants = new HashMap();
static {
constants.put("1", "First");
constants.put("2", "Second");
constants.put("3", "Third");
}
}


and the usage of such static collection:

System.out.println(Example.constants.get("1"));


Wouldn't you prefer to have more "native" support in Java for dealing with collections? For example, with the syntax that is found in most "dynamic" languages today, the previous example could be replaced with something like this:

public final static HashMap constants = new HashMap() {
"1" : "First",
"2" : "Second",
"3" : "Third"
}


Collections are one of the crucial aspects of programming languages and would it be normal to see them embedded in the language syntax? Would this "dynamic" syntax, used in Ruby, Groovy and other dynamic languages, make Java applications easier to write (and read)?

Do you have other syntax enchacements that you would like to see in the following editions of Java?

16 Comments

Jesse Barnum
2006-06-05 06:37:36
You could do that with Java as it exists in 1.5 using variable length parameters. You could even write a static utility method to do it with the existing HashMap class, ie.


public static final Map constants = MapUtils.createHashMap(
"1", "First",
"2", "Second",
"3", "Third"
);


The fact that this hasn't been implemented in the JDK is likely a sign that it's not terribly important. I see no reason to change the language syntax for this use case.

arnaud
2006-06-05 06:50:14
Also you can use an inline constructor {{...}} :


class Example {
public final static Map constants = new HashMap() {
{
put("1", "First");
put("2", "Second");
put("3", "Third");
}
};
}

eu
2006-06-05 09:24:29
Man, if you need something like that use Groovy. In Java for a small stuff like that you should really consider to use enums instead and big collections should not be hardcoded in the Java code, is is simply unwise from the code size point of view.
Steve
2006-06-05 09:30:43
I think Java 6 or Java SE 6 is going to support collections quite nicely. What do you think?
Laurent
2006-06-06 01:59:01
Hi.


Regarding the given example with the HashMap declared as a constant I have one objection : this HashMap is not constant at all as you can modify its content.


I prefer to use something like that when I need to have a constant collection :


public class Example {
public final static Map constants;
static {
Map tmpConstants = new HashMap();
tmpConstants.put("1", "First");
tmpConstants.put("2", "Second");
tmpConstants.put("3", "Third");
constants = Collections.unmodifiableMap(tmpConstants);
}
}


With the above initialization, nobody can modify the Map and so I consider this a as real constant.

Vaibhav
2006-06-06 03:43:00
Its better to define a block definition like


final {
// final consts
}


I guess the code should be more readable. Is their any issue with this declaration ?



Dejan Bosanac
2006-06-06 03:52:17
First, thank you all for great responses. I know that using hardcoded collections is not wise and I certainly wouldn't use them unless I really had to. In this project I needed to be 1.4 compliant and there was no easy way for me to configure these things in some kind of configuration file (it's an ongoing project so there is no much I can do at this moment). The thing that buged me was that an easy task such as creating hard coded collection should be quick to accomplish and somehow leveraged by the language (as we see in other languages).
I realize that "static collections" wasn't that best example, but I'm glad to see that it started an interesting discussion with a lot of great examples.
Ryan Sonnek
2006-06-06 14:56:50
Funny, I just blogged about this today! I really don't see the need to change Java. It's perfectly capable of handling your usecase, it's just a bit different than Ruby.


http://jroller.com/page/wireframe/?anchor=ruby_syntax_for_java_maps

Jeroen Wenting
2006-06-07 04:07:30
I wish people'd stop trying to add everything and the kitchen sink to Java and would get down to actually using it.


While your idea isn't that bad, the deluge of terrible "enhancements" and "improvements" posted almost every day makes me rather afraid for the future of the language (especially if Sun goes ahead with their crazy idea to throw away control over the JLS and JVMS to the open source wolves).

George Jempty
2006-06-07 06:46:24
Seems this suggestion is a moot point given Ryan Sonnek's JRoller post. Along these lines however it might be nice to be able to literally initialize a HashMap from JSON-compliant text. Then all sorts of interoperability with Javascript and Python would abound, without necessarily having to fall back on Jython or Rhino.

2006-06-10 06:37:43
Another useful shortcut could be made for Collections:
List example = new ArrayList() { "one", "two", "tree" };


as opposed to current:
List examle = Arrays.asList(new String[] {"one", "two", "three"});


I think while it's important to keep the syntax clean, it's equally important to keep the language expressive. If a particular syntax makes the intent of a commonly used expression clearer, I don't see why it shouldn't be considered for inclusion in the language.


In fact, if we think of syntax as a basic form of reuse, than the principle that reuse should be discovered (and not anticipated) applies here too. ;)


Cheers,
-Dmitri


2006-06-11 14:47:11
This is the realms of scripting languages. Groovy has just this and so does Ruby.
Kai Gro├čjohann
2006-06-13 02:54:05
If put were to return this, then one could say:
x = new HashMap().put("1", "First")
  .put("2", "Second")
  .put("3", "Third");

IMVHO it would be useful in other circumstances as well to have
setters and similar methods (like put above) return this.
And there is no Java syntax voodoo required at all!
DEBEDb
2006-06-20 23:03:14
I think something like Java Syntactic Extender is cool; now if only there was some
support from Java (for ease of debugging say - incorporating
support such functionality into JPDA) and from leading IDEs...


Some syntactic sugar that one may desired (and long
for C macros) may be rendered unnecessary with AOP
or annotations, but I've only had a cursory look at it...

Andrey
2006-06-21 21:23:10
a > b ? c : d : e
looks agly
sanath
2008-06-23 09:03:37
its nice but if u give some small program explaining of all hashmap methods , it would be better for novice users like me.............