Arrays.asList与Arrays.stream

(1)Arrays.asList

  将数组转化为list

Arrays.asList("a","ab","abc").stream()
        .filter(s->s.contains("ab"))
        .map(s->s.toUpperCase())
        .forEach(System.out::println);

 注:

  一旦初始化元素后,集合的size就是不可变的

  所以下面的代码会报错 java.lang.UnsupportedOperationException

List list = Arrays.asList("a","ab","abc");
list.clear();
list.remove("a");
list.add("g");

Arrays.asList源码

@SafeVarargs
@SuppressWarnings("varargs")
public static <T> List<T> asList(T... a) {
   return new ArrayList<>(a);
}

ArrayList

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);
        }
    }
View Code

这个内部类没有add,clear,remove方法

继续找AbstractList,报错来自这

public void add(int index, E element) {
    throw new UnsupportedOperationException();
}

public E remove(int index) {
    throw new UnsupportedOperationException();
}

可以在外面这样包一层ArrayList,这样就可以对数组操作了

List<String> list = new ArrayList<>(Arrays.asList("a", "b", "c"));
list.remove("a");
list.add("g");
list.forEach(System.out::println);

(2)Arrays.stream

将数组转换成流

Arrays.Stream 或 Stream.of 都可以将 Array 转换为 Stream

Arrays.stream(new String[]{"a","ab","abc"})
        .map(s->Arrays.asList(s))
        .flatMap(l->l.stream())
        .forEach(System.out::println);

Stream.of的源码

public static<T> Stream<T> of(T... values) {
    return Arrays.stream(values);
}

注:

  调用了Arrays.Stream

Arrays.Stream的源码

public static <T> Stream<T> stream(T[] array) {
     return stream(array, 0, array.length);
}

原始数组

Arrays.Stream

int [] s= new int[]{1, 2, 3};
IntStream s1 = Arrays.stream(s);
s1.forEach(System.out::println);

Stream.of

Stream<int[]> s2 = Stream.of(s);
IntStream s3 = s2.flatMapToInt(Arrays::stream);
s3.forEach(System.out::println);

对于对象数组,都会调用Arrays.stream

对于原始数组,Arrays.stream更方便,它可以直接返回固定大小的IntStream

posted @ 2020-04-02 14:53  慕尘  阅读(17027)  评论(0编辑  收藏  举报