Why does SortedList.add () throw an 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)-> lr ); 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) 
+5
source share
1 answer

A SortedList is a sorted view of its main list. If you are allowed to add items to a sorted list, this will break this relationship. Instead, you need to add the item to the base list:

 il.add(12); 
+11
source

Source: https://habr.com/ru/post/1210495/


All Articles