标签: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 是否包含指定元素
迭代器
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; });
大小
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 });
其他
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; });
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] });
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