Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why SortedList.add() throws UnsupportedOperationException?

Very simple code:

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.collections.transformation.SortedList;

public final class SortedListTest {

   public static void main( String[] args ) {
      final ObservableList<Integer> il  = FXCollections.observableArrayList();
      final SortedList<Integer>     sil = new SortedList<>( il );
      sil.comparatorProperty().set((l,r)-> l-r );
      sil.add( 12 );
   }
}

Execution:

Exception in thread "main" java.lang.UnsupportedOperationException
    at java.util.AbstractList.add(AbstractList.java:148)
    at java.util.AbstractList.add(AbstractList.java:108)
    at SortedListTest.main(SortedListTest.java:13)
like image 738
Aubin Avatar asked Jan 06 '15 18:01

Aubin


1 Answers

A SortedList is a sorted view of its underlying list. If you were allowed to add elements to the sorted list it would break that relationship. You need to add the element to the underlying list instead:

il.add(12);
like image 114
James_D Avatar answered Nov 20 '22 18:11

James_D