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);
new⇒Perl 6 1.0 in March?
Doh, my mistake. I'm aware of therelation between Parrot and Rakudobut I'...
Keith: Dec 2, 1:03am