KBD

Keith Devens .com

Tuesday, December 2, 2008 Flag waving
Follow the path. – me

Archive: October 11, 2005

← October 10, 2005October 12, 2005 →

Daily link icon Tuesday, October 11, 2005

Arrays.asList()

Arrays.asList is a feature every Java developer should know about. It'll save you from writing code like:

List<Foo> foolist = new ArrayList<Foo>();
foolist.add(foo);
return foolist;

or maybe

if(map.containsKey(id)){
    map.get(id).add(foo);
}else{
    List<Foo> foolist = new ArrayList<Foo>();
    foolist.add(foo);
    map.put(id, foo);
}

and allow you to write code like:

return Arrays.asList(foo);

and

if(map.containsKey(id))
    map.get(id).add(foo);
else
    map.put(id, Arrays.asList(foo));

Update: I didn't notice that Arrays.asList returns a List that can't be added too. When you try to call add on the returned List, you'll get an UnsupportedOperationException in AbstractList.add. That seemed lame to me, but the List interface does say that add is an "optional operation". For the lists to be mutable, the above code snippets have to be changed to something like:

return new ArrayList<Foo>(Arrays.asList(foo));

and

if(map.containsKey(id))
    map.get(id).add(foo);
else
    map.put(id, new ArrayList<Foo>(Arrays.asList(foo)));

Update: Of course, the more pathalogical example of what Arrays.asList saves you from is:

List<Foo> foolist = new ArrayList<Foo>(fooarray.length);
for(int i=0,n=fooarray.length; i<n; i++){
    foolist.add(fooarray[i]);
}

or

List<Foo> foolist = new ArrayList<Foo>(fooarray.length);
for(Foo f : fooarray){
    foolist.add(f);
}

because that becomes just:

List<Foo> foolist = Arrays.asList(fooarray);
  1. Ned Batchelder: Object reference not set to an instance of an object "I know, I know, they aren't pointers, they're object references. Whatever. Bite me." Ned on bad .NET exception messages. Smiley

       (0) Tags: [Programming]
  2. Ned highly recommends Curse of the Were-Rabbit. Gotta see it.

       (0) Tags: [TV/Movies]
  3. MilkandCookies - Pro-Test by Skinny Puppy. Didn't link to this a while ago for some reason.

       (2)
  4. I just found out that C# 2.0 has closures. Oh happy day.

       (1) Tags: [Programming]
← October 10, 2005October 12, 2005 →
December 2008
SunMonTueWedThuFriSat
 123456
78910111213
14151617181920
21222324252627
28293031 



RSS feed RSS feed for Keith's Weblog
Atom feed Atom feed for Keith's Weblog
Weblog archive
Recent comments
  on 6 posts

Recent comments XML

new⇒Perl 6 1.0 in March?

Doh, my mistake. I'm aware of the​relation between Parrot and Rakudo​but I'...

Keith: Dec 2, 1:03am

Free image hosting sites

Well, TinyPic has this in its​FAQ:

> Images and videos is in​your accoun...

Keith: Dec 1, 1:13am

Join a NameValueCollection into a querystring in C#

Well with a lamba expression, this​is what I came up​with:

?!code:csharp...

Gustaf Lindqvist: Nov 30, 4:38pm

Why no generic OrderedDictionary?

Check​http://www.codeproject.com/KB/recip​es/GenericOrderedDictionary.aspx?d...

Gabrielk: Nov 27, 6:57am

WhatIsMyIP.com

http://www.thesysteminfo.com is​another good alternate to​whatismp.com... I...

Kripz: Nov 26, 8:51pm

Girls, please don't get breast implants

Actually I think it's sweet when a​man loves a woman whether she's big​or n...

218.186.12.228: Nov 26, 9:40am

Generated in about 0.058s.

(Used 7 db queries)

mobile phone