• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

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

 
Ranch Hand
Posts: 44
1
  • Mark post as helpful
  • send pies
  • 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: 60136
188
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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: 3320
86
  • Mark post as helpful
  • send pies
  • 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
  • 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: 60136
188
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
Sheriff
Posts: 23710
50
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • 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: 60136
188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
… and “you're welcome”
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!