Update: I've implemented the features mentioned below. Read more about it at another post: Implementation of My Java7 Wishlist
There are tons of feature requests for java7. Most of the them center around closures, super-packages, extension methods, tuples, etc which are all new concepts for the language. I have been wondering why no one has bothered with improving support for some of the existing concepts/classes – in particular the syntax regarding Collection classes.
Collections have been a hot topic, although indirectly, with the introduction of generics in java 5. These classes are used more and more often in daily development – at least by me J and all my colleagues. So I am considering proposing giving the Collections citizens of java additional privileges like Arrays have receive right from the start. My wish list features mainly around syntactic enhancements to improve readability and make the code more concise (inspired by Rob’s CICE).
My first proposal revolves around making the collection instantiation syntax.
For a Collection class we might consider something similar to arrays:
Instead of:Collection<String> c = new ArrayList
c.add(“one”);
c.add(“two”);
c.add(“three”);
Use:Collection<String> c = new ArrayList {“one”, “two”, “three” };
For a Map instance we might consider the javascript like syntax:
Instead of:Map<String, Integer> m = new HashMap
m.put(“one”, 1);
m.put(“two”, 2);
m.put(“three”, 3);
Use:Map<String, Integer> m = new HashMap { “one”:1, “two”:2, “three”:3 };
However it becomes tricky when we wish to invoke a particular conctructor of the actual implementing class. I am yet to come up with a pretty format for that one but something likeCollection<String> c = new ( ArrayList(3) ) { “one”, “two”, “three” };
Map<String, Integer> m = new ( HashMap(5, 0.8) ) { “one”:1, “two”:2, “three”:3 };
should work fine.
My second proposal is to allow syntax to treat Maps like Arrays. After all Maps are Associative Arrays J.
So one should be able to writem[“four”] = 4;
int i = m[“two”] ;
instead ofm.put(“four”, 4);
int i = m.get(“two”);
These help make the code compact and more readable.
I would be glad if you shared with me what you think of this proposal or if you have any similar ideas.
Update: I've implemented the features mentioned above. Read more about it at another post: Implementation of My Java7 Wishlist
Saturday, April 5, 2008
My Java7 Wishlist regarding Collections
Posted by Shams at 18:56
Labels: Collection , java7 wishlist , List , Map , Set
Subscribe to:
Post Comments
(
Atom
)
33 comments :
Post a Comment
Collection<String> c= java.util.Arrays.asList("asdf", "as");
yup my first proposal seems very similar :). Wasn't aware of it till now.
@Casper
in case of pojos, i'm more in favor of explicit constructors to initialize member variables
@Ishaaq
yup that method already exists, but ties my implementation to an ArrayList and I lose the freedom to choose my Collection implementation
is one of my pet projects. In here I try adding shortcut methods and builders etc.. to leverage the interaction with common classes as much as possible :-)
But yes, collections is one of the few packages in the java API that is actually used in 99% of all apps, hence deserving more attention ;-)
Collection<String> c = new ArrayList<String>(){{
add(“one”);
add(“two”);
add(“three”);
}};
But I liked Shams's one ;)
Yup it exists but again it's verbose. I prefer to write it as follows so its easy to recognize the initializer block:
Collection<String> c = new ArrayList<String>(){
{
add(“one”);
add(“two”);
add(“three”);
}
};
Take a look at https://kijaro.dev.java.net/
You can experiment there with no problems.
thanx for the link, I'll definitely look into the project. looks like a great idea :)
Help, please. All recommend this program to effectively advertise on the Internet, this is the best program!
Post a Comment