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!