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

removeIf does not work for List created using Arrays.asList(arrayElements.);

XMLWordPrintable

      ADDITIONAL SYSTEM INFORMATION :
      OS: Windows 10
      Java: 1.8u72

      A DESCRIPTION OF THE PROBLEM :
      Removing an element using the list.removeIf(condition) when matches, it calls the remove method and that throws java.lang.UnSupportedOperationException.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      String[] stringArray = new String[]{"AA","BB","CC","DD"};
        List<String> stringList = Arrays.asList(columnArray);
      stringList .removeIf((String element) -> element.equals("BB"));
      stringList.foreach(System.out::println);

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      element BB should be removed from the list and when stringList.foreach(System.out::println); is called, the three remaining items from the list should get printed.
      ACTUAL -
      getting java.lang.UnSupportedOperationException for third statement. Even though the Arrays.asList() also does a new ArrayList().
      If you don't create using Arrays.asList() or you create your list as new ArrayList(Arrays.asList(columnArray)), it works fine.

      ---------- BEGIN SOURCE ----------
      String[] stringArray = new String[]{"AA","BB","CC","DD"};
              List<String> stringList = Arrays.asList(stringArray);
              stringList.forEach(System.out::println);
              stringList.removeIf((String string) -> string.equals("BB"));
              stringList.forEach(System.out::println);
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      I create a new list out of the list returned by Arrays.asList(). For example
      List<String> stringList = new ArrayList(Arrays.asList(stringArray));

      FREQUENCY : always


            psonal Pallavi Sonal (Inactive)
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: