Home > Java > javaTutorial > body text

List code analysis of Java collections

黄舟
Release: 2017-03-13 17:28:51
Original
1576 people have browsed it

ListInherits from Collection’s interface. List is also a type of collection. List is an ordered queue. Each element in the List will have an index. The index of the first element is 0, and the index values ​​of subsequent elements are +1 in sequence. List Duplicate elements are allowed.

List framework:


List interface source code:


public interface List<E> extends Collection<E> {
    int size();//大小
    boolean isEmpty();//判断是否为空
    boolean contains(Object o);//判断是否包含某个对象
    Iterator<E> iterator();//返回迭代对象
    Object[] toArray(); //返回对象数组
    <T> T[] toArray(T[] a);//对象数组
    boolean add(E e);//添加某个对象
    boolean remove(Object o);//删除某个对象
    boolean containsAll(Collection<?> c); // 是否包含某个Collection的所有对象
    boolean addAll(Collection<? extends E> c);//将Collection对象追加到List中
    boolean addAll(int index, Collection<? extends E> c);//在某个位置将Collection对象追加到List中
    boolean removeAll(Collection<?> c);//去掉Collection中所包含的对象
    boolean retainAll(Collection<?> c);//去掉不包含在Collection中所包含的对象
    default void replaceAll(UnaryOperator<E> operator) {
        Objects.requireNonNull(operator);
        final ListIterator<E> li = this.listIterator();
        while (li.hasNext()) {
            li.set(operator.apply(li.next()));
        }
    }
    @SuppressWarnings({"unchecked", "rawtypes"})
    default void sort(Comparator<? super E> c) {
        Object[] a = this.toArray();
        Arrays.sort(a, (Comparator) c);
        ListIterator<E> i = this.listIterator();
        for (Object e : a) {
            i.next();
            i.set((E) e);
        }
    }
    void clear();//删除所有对象
    boolean equals(Object o);//判断两个list是否相同
    int hashCode();//返回List的hashCode
    E get(int index);//返回某个位置的对象
    E set(int index, E element);//替换某个位置的对象
    void add(int index, E element);//在某个位置添加对象
    E remove(int index);//删除某个位置的对象
    int indexOf(Object o);//返回某个对象在List中的位置
    int lastIndexOf(Object o);//List中最后一个对象的坐标
    ListIterator<E> listIterator();//返回整个List的迭代
    ListIterator<E> listIterator(int index); //从某个位置开始返回List的迭代
    List<E> subList(int fromIndex, int toIndex);//截取部分List
    @Override
    default Spliterator<E> spliterator() {
        return Spliterators.spliterator(this, Spliterator.ORDERED);
    }
}
Copy after login


The above is the detailed content of List code analysis of Java collections. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template