Some issues @ JAVA COLLECTIONS FRAMEWORK

While dwelling with the Java Collection Framework, some of us facing these interesting issues:

  1. Arrays.asList() behaviour

This issue has been submitted as a bug in bugs.sun.com (bug_id=6353471) but the case is then closed and will not be fixed because it is considered “not a defect”.

The bug report description said:

java.util.Arrays.asList() does not support primitive arrays

If the programmer passes an array of primative data type, for example:

int[] intArray = { 1,2,3 };

to the asList() method, it will accept that primative array and place it as the internal Object array’s first element.

So a call to get(0) does not get you the first element that were in the primitive array (the value 1) but the array object itself

Of course a call to get(1) with an index of 1 will result in an ArrayIndexOutOfBoundsException…

And of course the toString() method (inherited from class Object) returns the hashcode of the array object, not the element of the array.

This bug report is also found against 1.3 which is “Closed, and will not be fixed”… (http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4348595)

In that report, it is noted that if a primitive array was passed, the compiler would generate an error stating that it’s looking for an Object [] array and that a primitive cannot be passed… but with J2SE 5.0, it apparently can… not sure what the mechanisms that allows this.

  1. Exception issues from the method List.subList() and List.removeAll().

It is similar with an article by Timothy Igor McGillicuty at

http://radio.weblogs.com/0108008/categories/technology/

Suppose we had an initialized array and,

want to convert it to a List,

creating a sublist from that list,

then remove all elements from that list like what is exits on sublist

At first we had this code:

1 String[] str = {“Bob”, “Bill”, “Jean”};

2 List list = Arrays.asList(arr);

3 List sublist = list.subList(1,2);

4 list.removeAll(sublist);

Line 4 got us the java.util.ConcurrentModificationException

The documentation says that a subList is dependent on the List that it is sublisting from. So, while a sublist is open, it is manipulating the parent list and seems to have a lock on it.

Instead, you can create a brand new ArrayList and fill it with the subList, thus freeing up the sublist so that a future call can modify the original List.

This is the modified line 3:

3 List sublist = new ArrayList(list.subList(1,2));

So why does

3 List sublist = list.subList(1,2);

get you a different animal than

3 List sublist = new ArrayList(list.subList(1,2));?

Answer : Because in the first snippet, we haven’t instantiated a new object, we just created a reference to an existing object.

In the second snippet, we created a brand new object. That’s what the word “new” gets us.

Right after we fixed that,

Line 4 got us the java.util.UnsupportedOperationException

The cause is similar with our previous problem. Note that the return from the method asList() doesn’t return a new object but it says that:

Returns:

a list view of the specified array

So we have to instantiate a new object rather than just refering.

This is the modified line 2:

2 List list = new ArrayList(Arrays.asList(arr));

Advertisements