Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8329089

Empty immutable list throws the wrong exception type for remove(first | last) operations

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P4 P4
    • 23
    • None
    • core-libs
    • None

      According to the specifications:

      "Elements cannot be added, removed, or replaced. Calling any mutator method on the List will always cause {@code UnsupportedOperationException} to be thrown."

      However, calling `removeFirst()` or `removeLast()` on an empty immutable list will erroneously throw `java.util.NoSuchElementException` instead.

      Reproducer:

      assertThrows(UnsupportedOperationException.class, List.of()::removeFirst);

            pminborg Per-Ake Minborg
            pminborg Per-Ake Minborg
            Votes:
            1 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated:
              Resolved: