• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

list.retainAll() throws java.lang.UnsupportedOperationException exception

 
Ranch Hand
Posts: 44
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am trying to get common numbers from two lists and getting UnsupportedOperationException. Below is my code. Please suggests your thoughts?
Requirement: I want all common numbers from two lists as output. I am able to get unique numbers by using set but that does not fulfill the requirement.





Output:


[1, 3, 5]
Exception in thread "main" java.lang.UnsupportedOperationException
at java.util.AbstractList.remove(Unknown Source)
at java.util.AbstractList$Itr.remove(Unknown Source)
at java.util.AbstractCollection.retainAll(Unknown Source)
at com.test.ArrayListTEest.main(ArrayListTEest.java:20)

 
Marshal
Posts: 79177
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you go through the List interface, you find that the retainAll method is marked as “optional”. That means that an implementing class is permitted not to implement that method, but to throw an Exception instead.

A lot of people think that is a daft way to write an interface, but that is how it was done.

If you look up Arrays#asList, it appears to return a “fixed‑size” List. That means you cannot use it for retainAll or addAll or similar. What you want is an ordinary ArrayList or similar, which does implement those methods. It doesn't seem to ahve a constructor taking an array as a parameter, so you may have to use a for‑each loop to add the elements of the arrays to your Lists.
 
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:It doesn't seem to ahve a constructor taking an array as a parameter, so you may have to use a for‑each loop to add the elements of the arrays to your Lists.


Or you can pass the List returned by .asList() to an ArryaList constructor ie:

 
Surinder Mehra
Ranch Hand
Posts: 44
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Campbell.... It sloved my problem. Here is the modified program with little trick you suggested. If Arrays.asList(array) returns the fixed sized list which does not implement these methods, why not add this newly created list to ordinary list using addAll(collection) method.





Output:

[1, 3, 5]
[1, 3, 3, 5, 5]
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can add that List to a pre-existing mutable List, yes. But that is not what you tried to do originally. What you tried to do was assign the List from the asList method and later modify that List. What you would have to do there is create an empty List and use two add/retain calls on it:-By the way: those addAll retainAll and removeAll methods are intended for set union set intersection and set difference. They may suffer slow performance on Lists because they will run in O(n²) complexity.
You can read about them in the Java Tutorials (also use the next link on that page). They are called bulk operations.
 
Surinder Mehra
Ranch Hand
Posts: 44
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Campbell and tony... I would like to know how these bulk operation algorithms are implemented. If you could provide link for same, that would be great help.
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You don't need a link, because you have the source code for those classes in your Java installation. Look for a file named "src.zip" and open it up.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
… and “you're welcome”
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic