码迷,mamicode.com
首页 > 其他好文 > 详细

Collection、 List 接口 LinkedList 类

时间:2017-08-03 18:50:12      阅读:158      评论:0      收藏:0      [点我收藏+]

标签:tin   one   ace   sed   hal   first   collect   pre   lte   

技术分享

上图是本篇博客所要写的类的继承图

 

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 类

标签:tin   one   ace   sed   hal   first   collect   pre   lte   

原文地址:http://www.cnblogs.com/YKang/p/7281231.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!