on-internet
← Community

List.of()#iterator() exposes ListIterator interface but give UnsupportedOperationException when calling ListIterator methods

Stack Overflow ·

This code does not work correctly @Test(expected = UnsupportedOperationException.class) public void testListIteratorWrapperPreviousIndexThrows() { List<String> immutable = List.of("a", "b", "c"); Iterator<String> it = immutable.iterator(); ListIteratorWrapper w = new ListIteratorWrapper<>(it); w.next(); // works w.previousIndex(); // throws UOE } and this is why import java.util.*; public class Main { public static void main(String[] args) { var l = List.of(1,2,3); var itr = l.iterator(); // This instaceof is true! if(itr instanceof ListIterator li) { System.out.println("itr is a ListIterator!

Comments

Sign in or sign up to comment and upvote.

No comments yet.