1. 程式人生 > >Collection、 List 接口 LinkedList 類

Collection、 List 接口 LinkedList 類

val 類型 sort copy cti pan 必須 adf osi

技術分享

上圖是本篇博客所要寫的類的繼承圖

java.util.Collection

添加

boolean add(E e) // append the element

boolean addAll(Collection c) // append all element in c, must be the same type

清空

void clear() // delete all the element in Collection, but the collection still work

查找

boolean contains(Object o) //Returns true if this collection contains the specified element.

技術分享
        Collection<String> coll = new ArrayList<String>();
        Collection<String> c2 = new ArrayList<>();
        
        coll.add("h");
        coll.add("e");
        System.out.println(coll);  // [h, e]
        
        c2.add("h");
        c2.add("a");
        coll.addAll(c2);   
// [h, e, l, l] 把c2中元素全部加入coll中,類型必須相同 c2.clear(); // [] 只是清空元素,容器還在 boolean b = coll.contains("h"); // true 是否包含指定元素
View Code

叠代器

Iterator<E> iterator() // return a iterator object

Iterator<String> it = coll.iterator();  
while(it.hasNext())
System.out.print(it.next());    

刪除

boolean remove(Object o) // return false if not found the element , or true

boolean removeIf(Predicate<? extends E> filter) // functional interface

技術分享
Collection<Integer> coll = new ArrayList<>();
    
        coll.add(1);
        coll.add(2);
        coll.add(3);
        coll.add(4);
        coll.add(5);
    
 // 匿名內部類
        coll.removeIf(new Predicate<Integer>(){ 

            @Override
            public boolean test(Integer t) {
                return t > 3;
            }
            
        });
        System.out.println(coll);   //  [1, 2, 3]
    
// lambda表達式
coll.removeIf(t->{
            return t > 3;
        });
        
View Code

大小

int size()

遍歷

default void forEach(Consumer<? extends T> action) // Consumer is a functional interface

技術分享
            // anonymous inner class
        coll.forEach(new Consumer<Integer>(){

            @Override
            public void accept(Integer t) {
                System.out.print(t);  // 12345
            }
            
        });
        
        // lambda expression
        coll.forEach(t->{
            System.out.print(t);  // 12345
        });
View Code

其他

Object[] toArray() // Returns an array containing all of the elements in this collection.

List接口,父類方法就不贅述了,只寫下子類方法

java.util.List;

添加

void add(int index, E e) // add the specified value at the specified position in list

獲取

E get(int index) // Returns the element at the specified position in this list

查找

int indexOf(Object o) // same as lastIndexOf()

  Returns the index of the first occurrence of the specified element , or -1 if this list does not contain the element.

刪除

E remove(index) //Removes the element at the specified position.

修改

void replaceAll(UnaryOperator<E> operator) // repalce each element with the method return value in functional interface

技術分享
            // anonymous inner class
        list.replaceAll(new UnaryOperator<Integer>() {
            
            @Override
            public Integer apply(Integer t) {        
                return t+10;     //[11, 12, 13, 14, 15]
            }
        });
        
        // lambda expression
        list.replaceAll(t->{
            return t + 10;
        });
View Code

E set(int index, E e) // Replaces the element at the specified position

排序

void sort(Comparator<? super E> c) // Sorts this list according to the order induced by the specified Comparator.

技術分享
list.sort(new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
        
                return o2 - o1;   // [5, 4, 3, 2, 1]
            }        
        });
        
list.sort((o1, o2)->{
            return o1 - o2;    //  [1, 2, 3, 4, 5]
        });
View Code

ArrayList 底層是數組實現,查找快,在任意位置增刪較慢,盡量不使用索引增刪

復制 Object clone() // Returns a shallow copy of this ArrayList instance.

LinkedList 底層是雙向鏈表,因此可以實現棧,隊列,鏈表等數據結構

增加

void addFirst(E e) // equals to boolean offerFirst(E e)

void addLast(E e) // equals to boolean offerLast(E e) offer(E e) push(E e)

獲取值

E getFirst() // E element() E peek() E peekFirst() is equals to this method

E getLast() // E peekLast()

刪除

E removeFisrt()    // E pop() pollFirst() E poll() Retrieves and removes the first element of this list, or returns null if this list is empty.

E removeLast() // E pollLast() Retrieves and removes the last element of this list, or returns null if this list is empty.

Collection、 List 接口 LinkedList 類