• Post Reply Bookmark Topic Watch Topic
  • New Topic

java.lang.UnsupportedOperationException: Result lists are read-only.  RSS feed

 
ben oliver
Ranch Hand
Posts: 375
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a class like



In my DAO class, I use JPA to get a list of <Identity> and did a "Collections.sort(idetities)". At this point I got

java.lang.UnsupportedOperationException: Result lists are read-only.

error. Why Colections.sort(..) gave this error ? I am not modifying anything.

 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Collections.sort(...) will sort the list that you give it. So, it will modify the list. However, the list that you are trying to sort is unmodifiable. When Collections.sort(...) calls one of the methods of the list to add or remove an element, it will throw an exception.

One solution is to create a new, modifiable list from the original list, and then sort that list.

But, since you're presumably getting the list from the database using a JPA query, it would be a better idea to change the database query to include an "order by" clause, to let the database do the sorting. You wouldn't need to do the sorting in your Java code then.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!