美文网首页
List.removeIf()

List.removeIf()

作者: 胡飞飞_1995 | 来源:发表于2019-06-01 10:52 被阅读0次

    问题:

    当参照此写法时会抛出异常

    public static void main(String[] strs) {
            String str = ",123";
            String[] split = str.split(",");
            List<String> list = Arrays.asList(split);
            list.removeIf(StringUtils::isBlank);
        }
    
    Exception in thread "main" java.lang.UnsupportedOperationException
        at java.util.AbstractList.remove(AbstractList.java:161)
        at java.util.AbstractList$Itr.remove(AbstractList.java:374)
        at java.util.Collection.removeIf(Collection.java:415)
        at RemoveIf.main(RemoveIf.java:20)
    

    当调用Arrays.asList()方法时,返回值并非我们常用的java.util.ArrayList,而是java.util.Arrays.ArrayList,此类虽然也继承了AbstractList抽象类,但是并没有去实现add及remove方法,所以在调用这两个方法时便会调到抽象类,抛出异常

    public abstract class AbstractList<E> extends AbstractCollection<E> implements List<E> {
        public void add(int index, E element) {
            throw new UnsupportedOperationException();
        }
        public E remove(int index) {
            throw new UnsupportedOperationException();
        }
    }
    

    解决:

    解决办法也很简单,只需要重新new一个java.util.ArrayList把刚刚生成的java.util.Arrays.ArrayList放进去就可以了

    List<String> list1 = new ArrayList<>(list);
    

    java.util.Arrays.ArrayList 是Arrays类中的一个静态内部类,一个定长的List不允许修改,它的具体实现如下:

    private static class ArrayList<E> extends AbstractList<E>
            implements RandomAccess, java.io.Serializable
        {
            private static final long serialVersionUID = -2764017481108945198L;
            private final E[] a;
    
            ArrayList(E[] array) {
                a = Objects.requireNonNull(array);
            }
    
            @Override
            public int size() {
                return a.length;
            }
    
            @Override
            public Object[] toArray() {
                return a.clone();
            }
    
            @Override
            @SuppressWarnings("unchecked")
            public <T> T[] toArray(T[] a) {
                int size = size();
                if (a.length < size)
                    return Arrays.copyOf(this.a, size,
                                         (Class<? extends T[]>) a.getClass());
                System.arraycopy(this.a, 0, a, 0, size);
                if (a.length > size)
                    a[size] = null;
                return a;
            }
    
            @Override
            public E get(int index) {
                return a[index];
            }
    
            @Override
            public E set(int index, E element) {
                E oldValue = a[index];
                a[index] = element;
                return oldValue;
            }
    
            @Override
            public int indexOf(Object o) {
                E[] a = this.a;
                if (o == null) {
                    for (int i = 0; i < a.length; i++)
                        if (a[i] == null)
                            return i;
                } else {
                    for (int i = 0; i < a.length; i++)
                        if (o.equals(a[i]))
                            return i;
                }
                return -1;
            }
    
            @Override
            public boolean contains(Object o) {
                return indexOf(o) != -1;
            }
    
            @Override
            public Spliterator<E> spliterator() {
                return Spliterators.spliterator(a, Spliterator.ORDERED);
            }
    
            @Override
            public void forEach(Consumer<? super E> action) {
                Objects.requireNonNull(action);
                for (E e : a) {
                    action.accept(e);
                }
            }
    
            @Override
            public void replaceAll(UnaryOperator<E> operator) {
                Objects.requireNonNull(operator);
                E[] a = this.a;
                for (int i = 0; i < a.length; i++) {
                    a[i] = operator.apply(a[i]);
                }
            }
    
            @Override
            public void sort(Comparator<? super E> c) {
                Arrays.sort(a, c);
            }
        }
    

    相关文章

      网友评论

          本文标题:List.removeIf()

          本文链接:https://www.haomeiwen.com/subject/cqpotctx.html