jastrachan | 19 Feb 14:13 2005
Picon

Re: Questions on List and Map

I'll answer the second first :)

Looking up an item in a map via

     map[key]

equates to

     map.get(key)

which does not throw an exception. (Its valid to look up something and 
get nothing back). If you really wanna know if the item is in the 
collection, use m.containsKey()

you could imagine maps and list as being similar but lists have integer 
keys rather than maps which have any kind of key; for this reason we 
treat list lookups for non-existent keys as like map lookups for 
non-existent keys, and so we return null.

map = [:]
list  = []

assert list[5] == null
assert map[5] == null

However list.get() does throw an exception for an out of range index

On 19 Feb 2005, at 12:33, Jean-Louis Berliet wrote:
> I have 2 questions on using List and Map with groovy :
>
> 1) List and exception :
>
> mylist = ["a", "b", "c"]
> println mylist[5]
> go
> --> null
>
> Perhaps groovy has to throw an exception in this case ?
>
> 2) Map and exception :
>
> mymap = ["a":"aaa", "b":"bbb"]
> println mymap["c"]
> go
> --> null
>
> Perhaps groovy has to throw an exception in this case ?
>
> Thanks.
>
> Jean-Louis.
>
>
>
>

James
-------
http://radio.weblogs.com/0112098/


Gmane