Java集合类常用方法总结 —— List相关

一、ArrayList

基本方法
  • Returns the number of elements in this list.

    public int size(); 
    
  • Returns true if this list contains no elements.

    public boolean isEmpty(); 
    
  • Returns an array containing all of the elements in this list in proper sequence (from first to last element).

    public Object[] toArray();
    
  • Returns a view of the portion of this list between the specified {@code fromIndex}, inclusive, and {@code toIndex}, exclusive. (If {@code fromIndex} and {@code toIndex} are equal, the returned list is empty.)

    public List<E> subList(int fromIndex, int toIndex); 
    
  • Returns an iterator over the elements in this list in proper sequence.

    public Iterator<E> iterator();
    
  • Appends the specified element to the end of this list.

    public boolean add(E e);
    
  • Inserts the specified element at the specified position in this list.

    public void add(int index, E element);
    
  • Appends all of the elements in the specified collection to the end of this list.

    public boolean addAll(Collection<? extends E> c);
    
  • Inserts all of the elements in the specified collection into this list, starting at the specified position. Shifts the element currently at that position (if any) and any subsequent elements to the right (increases their indices).

    public boolean addAll(int index, Collection<? extends E> c);
    
  • Removes all of the elements from this list.

    public void clear();
    
  • Removes the element at the specified position in this list and return the element that was removed from the list.

    public E remove(int index); 
    
  • Removes the first occurrence of the specified element from this list,if it is present. If the list does not contain the element, it is unchanged.

    public boolean remove(Object o);
    
  • Removes from this list all of its elements that are contained in the specified collection.

    public boolean removeAll(Collection<?> c);
    
  • Retains only the elements in this list that are contained in the specified collection. In other words, removes from this list all of its elements that are not contained in the specified collection.

    public boolean retainAll(Collection<?> c);
    
  • Replaces the element at the specified position in this list with the specified element and return the element previously at the specified position.

    public E set(int index, E element);
    
  • Returns the element at the specified position in this list.

    public E get(int index);
    
  • Returns true if this list contains the specified element.

    public boolean contains(Object o); 
    
  • Returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element.

    public int indexOf(Object o); 
    
  • Returns the index of the last occurrence of the specified element in this list, or -1 if this list does not contain the element.

    public int lastIndexOf(Object o);
    

二、LinkedList

基本方法
  • Returns the number of elements in this list.

    public int size(); 
    
  • Returns true if this list contains no elements.

    public boolean isEmpty(); 
    
  • Returns an array containing all of the elements in this list in proper sequence (from first to last element).

    public Object[] toArray();
    
  • Returns a view of the portion of this list between the specified {@code fromIndex}, inclusive, and {@code toIndex}, exclusive. (If {@code fromIndex} and {@code toIndex} are equal, the returned list is empty.)

    public List<E> subList(int fromIndex, int toIndex); 
    
  • Returns an iterator over the elements in this list in proper sequence.

    public Iterator<E> iterator();
    
  • Inserts the specified element at the beginning of this list.

    public void addFirst(E e);
    public boolean offerFirst(E e);
    public void push(E e);
    
  • Appends the specified element to the end of this list.

    public void addLast(E e);
    public boolean offer(E e);
    public boolean offerLast(E e);
    
  • Appends the specified element to the end of this list.

    public boolean add(E e);
    
  • Inserts the specified element at the specified position in this list.

    public void add(int index, E element);
    
  • Appends all of the elements in the specified collection to the end of this list.

    public boolean addAll(Collection<? extends E> c);
    
    
  • Inserts all of the elements in the specified collection into this list, starting at the specified position.

    public boolean addAll(int index, Collection<? extends E> c);
    
  • Removes and returns the first element from this list.

    public E removeFirst();
    public E poll();
    public E remove();
    public E pollFirst();
    public E pop();
    
  • Removes and returns the last element from this list.

    public E removeLast();
    public E pollLast();
    
  • Removes the first occurrence of the specified element from this list.

    public boolean remove(Object o);
    
  • Removes the element at the specified position in this list.

    public E remove(int index);
    
  • Removes all of the elements from this list.

    public void clear();
    
  • Removes the first occurrence of the specified element in this list (when traversing the list from head to tail).

    public boolean removeFirstOccurrence(Object o);
    
  • Removes the last occurrence of the specified element in this list (when traversing the list from head to tail).

    public boolean removeLastOccurrence(Object o);
    
  • Replaces the element at the specified position in this list with the specified element.

    public E set(int index, E element);
    
  • Returns, but does not remove, the first element in this list

    public E getFirst();
    public E peek();
    public E element();
    public E peekFirst()
    
  • Returns the last element in this list.

    public E getLast();
    public E peekLast();
    
  • Returns true if this list contains the specified element.

    public boolean contains(Object o);
    
  • Returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element.

    public int indexOf(Object o);
    
  • Returns the index of the last occurrence of the specified element in this list, or -1 if this list does not contain the element.

    public int lastIndexOf(Object o);
    
  • Returns the element at the specified position in this list.

    public E get(int index);
    

三、Collections

  • Sorts the specified list into ascending order.

    Collections.sort(tempList);
    
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值