Saturday, April 5, 2008

My Java7 Wishlist regarding Collections

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 like

Collection<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 write

m[“four”] = 4;
int i = m[“two”] ;

instead of

m.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

--

33 comments :

[hide] Robby O'Connor said...

I just noticed you linked my CICE post :)

on April 7, 2008 at 6:37 AM
[hide]

How to Reply to this comment

To reply to this comment please ensure that one of the following lines:
  • @8238617515861878756.0
  • @Robby O'Connor
is the first line of your comment.
Click here to enter your reply
[hide] Shams said...

yup it was a nice post and it got me introduced to CICE. thanx.

on April 7, 2008 at 8:27 PM
[hide]

How to Reply to this comment

To reply to this comment please ensure that one of the following lines:
  • @2163585847799631509.0
  • @Shams
is the first line of your comment.
Click here to enter your reply
[hide] Alex Miller said...

This idea has been written up several times. One great example is from Stephen Colebourne.

on April 8, 2008 at 3:17 AM
[hide]

How to Reply to this comment

To reply to this comment please ensure that one of the following lines:
  • @4967641495912792659.0
  • @Alex Miller
is the first line of your comment.
Click here to enter your reply
[hide] Shams said...

yup my first proposal seems very similar :). Wasn't aware of it till now.

on April 8, 2008 at 8:51 AM
[hide]

How to Reply to this comment

To reply to this comment please ensure that one of the following lines:
  • @6858116142040861945.0
  • @Shams
is the first line of your comment.
Click here to enter your reply
[hide] Casper Bang said...

C# 3.5 has something very similar implemented but goes further than just collection initialization, in that you can initialize POJO's fields a similar way (rather than rely on an API to be designed for method-chaining). Would love to see both in Java.

on April 8, 2008 at 5:15 AM
[hide]

How to Reply to this comment

To reply to this comment please ensure that one of the following lines:
  • @3352950280366974853.0
  • @Casper Bang
is the first line of your comment.
Click here to enter your reply
[hide] Shams said...

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

on April 8, 2008 at 8:51 AM
[hide]

How to Reply to this comment

To reply to this comment please ensure that one of the following lines:
  • @6858116142040861945.1
  • @Shams
is the first line of your comment.
Click here to enter your reply
[hide] Anonymous said...

You can already legally do this:

Collection<String> c= java.util.Arrays.asList("asdf", "as");

on April 8, 2008 at 8:27 AM
[hide]

How to Reply to this comment

To reply to this comment please ensure that one of the following lines:
  • @6274808523246996046.0
  • @Anonymous
is the first line of your comment.
Click here to enter your reply
[hide] Anonymous said...

http://spiffyframework.sourceforge.net/

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 ;-)

on April 8, 2008 at 3:07 PM
[hide]

How to Reply to this comment

To reply to this comment please ensure that one of the following lines:
  • @5935033504482486064.0
  • @Anonymous
is the first line of your comment.
Click here to enter your reply
[hide] Shihab said...

For collection initialization there is a "double braces" syntax:

Collection<String> c = new ArrayList<String>(){{
add(“one”);
add(“two”);
add(“three”);
}};

But I liked Shams's one ;)

on April 8, 2008 at 5:15 PM
[hide]

How to Reply to this comment

To reply to this comment please ensure that one of the following lines:
  • @8922826288045657524.0
  • @Shihab
is the first line of your comment.
Click here to enter your reply
[hide] Shams said...

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”);
}
};

on April 8, 2008 at 8:21 PM
[hide]

How to Reply to this comment

To reply to this comment please ensure that one of the following lines:
  • @3435762450499839445.0
  • @Shams
is the first line of your comment.
Click here to enter your reply
[hide] Aleksandr said...

Hm...
Take a look at https://kijaro.dev.java.net/
You can experiment there with no problems.

on April 9, 2008 at 9:14 AM
[hide]

How to Reply to this comment

To reply to this comment please ensure that one of the following lines:
  • @1723033991047122643.0
  • @Aleksandr
is the first line of your comment.
Click here to enter your reply
[hide] Shams said...

@ JAlexoid
thanx for the link, I'll definitely look into the project. looks like a great idea :)

on April 9, 2008 at 7:43 PM
[hide]

How to Reply to this comment

To reply to this comment please ensure that one of the following lines:
  • @2303476892792642048.0
  • @Shams
is the first line of your comment.
Click here to enter your reply
[hide] Anonymous said...

Interesting! Reminds me a bit of the (good old?) Perl days. Good luck! :-)

on April 30, 2008 at 6:07 PM
[hide]

How to Reply to this comment

To reply to this comment please ensure that one of the following lines:
  • @44863566346060597.0
  • @Anonymous
is the first line of your comment.
Click here to enter your reply
[hide] 逆円助 said...

さあ、今夏も新たな出会いを経験してみませんか?当サイトは円助交際の逆、つまり女性が男性を円助する『逆円助交際』を提供します。逆円交際を未経験の方でも気軽に遊べる大人のマッチングシステムです。年齢上限・容姿・経験一切問いません。男性の方は無料で登録して頂けます。貴方も新たな出会いを経験してみませんか

on July 4, 2009 at 6:36 PM
[hide]

How to Reply to this comment

To reply to this comment please ensure that one of the following lines:
  • @9004101930843810565.0
  • @逆円助
is the first line of your comment.
Click here to enter your reply
[hide] 精神年齢 said...

みんなの精神年齢を測定できる、メンタル年齢チェッカーで秘められた年齢がズバリわかっちゃう!かわいいあの子も実は精神年齢オバサンということも…合コンや話のネタに一度チャレンジしてみよう

on July 6, 2009 at 7:38 PM
[hide]

How to Reply to this comment

To reply to this comment please ensure that one of the following lines:
  • @7763051936560907791.0
  • @精神年齢
is the first line of your comment.
Click here to enter your reply
[hide] メル友募集 said...

最近仕事ばかりで毎日退屈してます。そろそろ恋人欲しいです☆もう夏だし海とか行きたいな♪ k.c.0720@docomo.ne.jp 連絡待ってるよ☆

on July 17, 2009 at 9:09 PM
[hide]

How to Reply to this comment

To reply to this comment please ensure that one of the following lines:
  • @8032581997486013611.0
  • @メル友募集
is the first line of your comment.
Click here to enter your reply
[hide] 家出 said...

最近TVや雑誌で紹介されている家出掲示板では、全国各地のネットカフェ等を泊り歩いている家出娘のメッセージが多数書き込みされています。彼女たちはお金がないので掲示板で知り合った男性の家にでもすぐに泊まりに行くようです。あなたも書き込みに返事を返してみませんか

on July 19, 2009 at 5:29 PM
[hide]

How to Reply to this comment

To reply to this comment please ensure that one of the following lines:
  • @1686018869912707877.0
  • @家出
is the first line of your comment.
Click here to enter your reply
[hide] 動物占い said...

あなたの性格を、動物に例えて占っちゃいます。もしかしたらこんな動物かも!?動物占いをうまく使って、楽しい人間関係を築いてください

on July 20, 2009 at 6:22 PM
[hide]

How to Reply to this comment

To reply to this comment please ensure that one of the following lines:
  • @8975730517935541635.0
  • @動物占い
is the first line of your comment.
Click here to enter your reply
[hide] 家出 said...

家出中の女性や泊まる所が無い女性達がネットカフェなどで、飲み放題のドリンクで空腹を満たす生活を送っています。当サイトはそんな女性達をサポートしたいという人たちと困っている女性たちの為のサイトです

on July 22, 2009 at 7:52 PM
[hide]

How to Reply to this comment

To reply to this comment please ensure that one of the following lines:
  • @5578343658297131811.0
  • @家出
is the first line of your comment.
Click here to enter your reply
[hide] セレブラブ said...

セレブ女性との割り切りお付き合いで大金を稼いでみませんか?女性に癒しと快楽、男性に謝礼とお互い満たしあえる当サイト、セレブラブはあなたの登録をお待ちしております。

on July 23, 2009 at 7:48 PM
[hide]

How to Reply to this comment

To reply to this comment please ensure that one of the following lines:
  • @323014622760988208.0
  • @セレブラブ
is the first line of your comment.
Click here to enter your reply
[hide] 夏フェス!! said...

夏フェス一緒に行ってくれる人募集!!夏の思い出一緒につくろぉ☆ megumi-0830@docomo.ne.jp 連絡してね♪

on July 24, 2009 at 7:39 PM
[hide]

How to Reply to this comment

To reply to this comment please ensure that one of the following lines:
  • @8905474040713101112.0
  • @夏フェス!!
is the first line of your comment.
Click here to enter your reply
[hide] 無料ゲーム said...

あなたのゲーマー度を無料ゲーム感覚で測定します。15個の質問に答えるだけの簡単測定で一度遊んでみませんか?ゲームが得意な人もそうでない人もぜひどうぞ。

on July 27, 2009 at 6:55 PM
[hide]

How to Reply to this comment

To reply to this comment please ensure that one of the following lines:
  • @8953930041586163870.0
  • @無料ゲーム
is the first line of your comment.
Click here to enter your reply
[hide] 素人 said...

Hな女性たちは素人ホストを自宅やホテルに呼び、ひとときの癒しを求めていらっしゃいます。当サイトでは男性ホスト様の人員が不足しており、一日3~4人の女性の相手をするホストもおられます。興味を持たれた方は当サイトにぜひお越しください

on July 28, 2009 at 6:34 PM
[hide]

How to Reply to this comment

To reply to this comment please ensure that one of the following lines:
  • @2370853452671075627.0
  • @素人
is the first line of your comment.
Click here to enter your reply
[hide] 出会い系 said...

実は出会い系には…関係者用入り口があるのを知っていますか?広告主やスポンサー用に用意されたIDではサクラや業者が立ち入ることが出来ないようになっているのです。当サイトでは極秘に入手した関係者用URLが公開されています

on July 29, 2009 at 10:00 PM
[hide]

How to Reply to this comment

To reply to this comment please ensure that one of the following lines:
  • @4192283900122887032.0
  • @出会い系
is the first line of your comment.
Click here to enter your reply
[hide] 逆援助 said...

男性はお金、女性は快楽を得る逆援助に興味はありませんか?お金を払っても性的欲求を満たしたいセレブ達との割り切り1日のお付き合いで当サイトでは大金を得ることができます。無料登録なのでアルバイト感覚でOK、詳しくはTOPページでどうぞ。

on July 30, 2009 at 6:24 PM
[hide]

How to Reply to this comment

To reply to this comment please ensure that one of the following lines:
  • @604974674648125358.0
  • @逆援助
is the first line of your comment.
Click here to enter your reply
[hide] 友達募集 said...

ホムペ完成記念!私の事みんなに知ってもらいたくて頑張りましたぁ。色々とご感想をお待ちしているので思った事を意見してください。メアドはほむぺにのせてありますぅ!★ fan.jna@docomo.ne.jp

on July 31, 2009 at 7:08 PM
[hide]

How to Reply to this comment

To reply to this comment please ensure that one of the following lines:
  • @4594558842220368220.0
  • @友達募集
is the first line of your comment.
Click here to enter your reply
[hide] 家出 said...

夏休みで気軽に家出する女子○生が急増しています。しかし家出したはいいものの泊る所やお金が無い彼女たちは、掲示板などで泊めてくれる男性を探す子も多いようです。当掲示板にも夏休みに入ってから通常の3倍以上のメッセージが寄せられています

on August 2, 2009 at 6:06 PM
[hide]

How to Reply to this comment

To reply to this comment please ensure that one of the following lines:
  • @5273085453973230983.0
  • @家出
is the first line of your comment.
Click here to enter your reply
[hide] 人妻 said...

今最もアツイバイトは人妻とのセフレ契約です。当サイトではお金を払ってでもセフレがほしい人妻が集まり、男性会員様との逆援生活を待っています。当サイトで欲求不満の女性との出会いをしてみませんか

on August 5, 2009 at 6:29 PM
[hide]

How to Reply to this comment

To reply to this comment please ensure that one of the following lines:
  • @2672225622909605265.0
  • @人妻
is the first line of your comment.
Click here to enter your reply
[hide] 素人 said...

素人ホストでは、男性のテクニック次第で女性会員様から高額な謝礼がもらえます。欲求不満な人妻や、男性と出会いが無い女性達が当サイトで男性を求めていらっしゃいます。興味のある方はTOPページからどうぞ

on August 6, 2009 at 7:44 PM
[hide]

How to Reply to this comment

To reply to this comment please ensure that one of the following lines:
  • @8015264072131838039.0
  • @素人
is the first line of your comment.
Click here to enter your reply
[hide] 友達募集中 said...

少し魅惑な自分をネットだから公開してみました。普段言えない事など、思い切って告白しているプロフなので興味ある方はぜひ除いてみてください連絡待ってまぁす。 hinyaaaaa@docomo.ne.jp

on August 7, 2009 at 7:14 PM
[hide]

How to Reply to this comment

To reply to this comment please ensure that one of the following lines:
  • @4519455567351101234.0
  • @友達募集中
is the first line of your comment.
Click here to enter your reply
[hide] Anonymous said...

Who knows where to download XRumer 5.0 Palladium?
Help, please. All recommend this program to effectively advertise on the Internet, this is the best program!

on November 22, 2009 at 12:16 AM
[hide]

How to Reply to this comment

To reply to this comment please ensure that one of the following lines:
  • @6700585375173838163.0
  • @Anonymous
is the first line of your comment.
Click here to enter your reply
[hide] Anonymous said...

...please where can I buy a unicorn?

on November 22, 2009 at 11:16 AM
[hide]

How to Reply to this comment

To reply to this comment please ensure that one of the following lines:
  • @8682263719173844066.0
  • @Anonymous
is the first line of your comment.
Click here to enter your reply
[hide] Anonymous said...

...please where can I buy a unicorn?

on November 23, 2009 at 8:23 AM
[hide]

How to Reply to this comment

To reply to this comment please ensure that one of the following lines:
  • @2074874126812366383.0
  • @Anonymous
is the first line of your comment.
Click here to enter your reply
[hide] Anonymous said...

[url=http://italtubi.com/tag/levitra-10-mg/ ]compra levitra onlin [/url] qiraramente .. .. Si puГІ dire questo eccezione: i) compra levitra wgucbomhyd [url=http://www.mister-wong.es/user/COMPRARCIALIS/comprar-viagra/]viagra cialis[/url]

on December 23, 2009 at 2:38 AM
[hide]

How to Reply to this comment

To reply to this comment please ensure that one of the following lines:
  • @3288682135072037287.0
  • @Anonymous
is the first line of your comment.
Click here to enter your reply