Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does List.remove(int) throw java.lang.UnsupportedOperationException? [duplicate]

Tags:

java

I am trying to remove elements from a List and getting java.lang.UnsupportedOperationException.

    public <T extends Object> void findDuplicates(         String title, Multimap<T, ChunkId> map) {         for (T key : map.keySet()) {             Collection<ChunkId> ids = map.get(key);             List<ChunkId> idList = (Arrays.asList(ids.toArray(new ChunkId[0])));             removeUsedIds(idList);             Collections.sort(idList); //...         }     }      private void removeUsedIds(List<ChunkId> idList) {         // decrement counter to avoid indexing changed parts of list         for (int i = idList.size() - 1; i >= 0; i--) {             if (usedIdSet.contains(idList.get(i))) {                 idList.remove(i);   // **** Exception thrown here             }         }     } 

and I get

Exception in thread "main" java.lang.UnsupportedOperationException     at java.util.AbstractList.remove(Unknown Source)     at org.xmlcml.svg2xml.analyzer.PDFIndex.removeUsedIds(PDFIndex.java:104)     at org.xmlcml.svg2xml.analyzer.PDFIndex.findDuplicates(PDFIndex.java:87)     at org.xmlcml.svg2xml.analyzer.PDFIndex.findDuplicatesInIndexes(PDFIndex.java:129)     at org.xmlcml.svg2xml.analyzer.PDFAnalyzer.analyzePDF(PDFAnalyzer.java:188)     at org.xmlcml.svg2xml.analyzer.PDFAnalyzer.analyzePDFFile(PDFAnalyzer.java:115)     at org.xmlcml.svg2xml.analyzer.PDFAnalyzer.main(PDFAnalyzer.java:398) 

NOTE: This is flagged as a duplicate of remove() on List created by Arrays.asList() throws UnsupportedOperationException but it's significantly different. That poster knew what the problem was and wanted an explanation; I did not know what the problem was and couldn't find it on SO by posting the current question. Java 6 docs (http://docs.oracle.com/javase/6/docs/api/java/util/ArrayList.html) gives no hint of the problem (its signature throws IndexOutOfBoundsException). The previous question also used removeAll() while this question referred to remove(int) so lexical searching is less precise.

I therefore asked on SO and got rapid and useful answers. Because I phrased the title exactly (unlike the previous question) it should be easy for others to find this answer. In less than a day it already has nearly as many votes as the previous question in a year (and 100+ views), suggesting that it will be significantly more useful. Since this question is now linked to the previous one I think it adds to the general usefulness without polluting SO. (The current answers are short and don't unnecessarily repeat information.)

I have edited the question to remove extraneous code. If this question had been available when I encountered the problem it would have saved me an hour!

like image 546
peter.murray.rust Avatar asked Apr 17 '13 20:04

peter.murray.rust


People also ask

How do I fix Java Lang UnsupportedOperationException?

The UnsupportedOperationException can be resolved by using a mutable collection, such as ArrayList , which can be modified. An unmodifiable collection or data structure should not be attempted to be modified.

Are lists mutable in Java?

If you're wondering about java. util. ArrayList - it is mutable and it is not creating another List instance on add() or remove() . If you are looking for immutable list - check Guava implementation of ImmutableList or Collections.


1 Answers

You're using it on a List implementation returned by Arrays.asList(), which returns a fixed-length collection, therefore a remove is unsupported.

like image 121
Theodoros Chatzigiannakis Avatar answered Oct 07 '22 17:10

Theodoros Chatzigiannakis