Arrays.asList returned List - Its modifying/write-through semantics (original) (raw)
Jaikiran Pai jai.forums2013 at gmail.com
Wed Aug 15 10:57:37 UTC 2018
- Previous message: Arrays.asList returned List - Its modifying/write-through semantics
- Next message: Arrays.asList returned List - Its modifying/write-through semantics
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Hi Ivan,
On 15/08/18 1:20 PM, Ivan Gerasimov wrote:
Hi Jaikiran!
The first part (the documentation clarification) was requested some time ago [1], so it may be eventually fixed. [1] https://bugs.openjdk.java.net/browse/JDK-7033681 Thank you for pointing to that one. I hadn't found it during my search before sending this mail.
With respect to the second part (throwing UOE from remove(Object)), I agree with you that it would be more consistent to always throw it, regardless of the presence of the searched item. However, it would introduce a change of behavior, so I think it's unlikely to happen. I understand. It may make sense to update the documentation for all the structurally-modifying methods of Collection/List (remove(Object), retainAll, removeAll, clear) in the same way it was done for List.removeIf() here [2]. [2] https://bugs.openjdk.java.net/browse/JDK-8023339
I looked at that commit and yes, a similar @throws explaining the UnsupportedOperationException will be helpful.
-Jai
With kind regards, Ivan
On 8/14/18 10:58 PM, Jaikiran Pai wrote: Consider the following code:
import java.util.Arrays; import java.util.List; public class ArraysAsListTest { public static void main(final String[] args) throws Exception { final List someList = Arrays.asList(new String[] {"a"}); System.out.println("Removed? " + someList.remove("a")); } } It uses Arrays.asList to create a java.util.List and then calls a list.remove(...) on it. This currently runs intothe following exception: Exception in thread "main" java.lang.UnsupportedOperationException: remove at java.base/java.util.Iterator.remove(Iterator.java:102) at java.base/java.util.AbstractCollection.remove(AbstractCollection.java:299) at ArraysAsListTest.main(ArraysAsListTest.java:8) The javadoc of Arrays.asList[1] states the following: "Returns a fixed-size list backed by the specified array. (Changes to the returned list "write through" to the array.)..." and has no other mention of how it's supposed to behave with "write" operations. Given that the javadoc states that the returned list is "write through", would that mean a "add/removal" is allowed? Probably not, because it does say it's a fixed size list.So the size altering, write operations (like add(), remove()) aren't allowed, but that isn't clear from the javadoc. So should the javadoc be updated to clarify the semantics of what changes are "write through" and what changes are not supported? Furthermore, the UnsupportedOperationException isn't always thrown from such a returned list. Consider this minor modification to the above code: final List someList = Arrays.asList(new String[] {"a"}); System.out.println("Removed? " + someList.remove("b")); I add "a" and remove "b". This now runs fine without exceptions and prints "Removed? false". Should the implementation just throw the UnsupportedOperationException irrespective of whether or not the element is contained insuch a returned list? If not, should that be made clear in the javadoc (maybe same for addoperations)? [1] https://docs.oracle.com/javase/10/docs/api/java/util/Arrays.html#asList(T...)
-Jaikiran
- Previous message: Arrays.asList returned List - Its modifying/write-through semantics
- Next message: Arrays.asList returned List - Its modifying/write-through semantics
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]