Collection接口常用方法
Collection接口是List、Set和Queue接口的父接口,该接口里定义的方法既可用于操作 Set 集合,也可用于操作 List 和 Queue 集合。
JDK不提供此接口的任何直接实现,而是提供更具体的子接口(如:Set和List)实现。
在 Java5 之前,Java 集合会丢失容器中所有对象的数据类型,把所有对象都当成 Object 类型处理;从 JDK 5.0 增加了 泛型以后,Java 集合可以记住容器中对象的数据类型。
add & size
boolean add(E e)
int size()
ArrayList list = new ArrayList();
list.add(123);
list.add(456);
list.add("hello");
list.add(new Employee("James", 36, 4000));
System.out.println(list.size());
addAll 添加一个collection
boolean addAll(Collection<? extends E> c)
ArrayList list = new ArrayList();
list.add(123);
list.add(456);
list.add("hello");
list.add(new Employee("James", 36, 4000));
ArrayList list2 = new ArrayList();
list2.addAll(list);
System.out.println(list2);
clear & isEmpty
void clear()
boolean isEmpty()
ArrayList list = new ArrayList();
list.add(123);
list.add(456);
list.add("hello");
list.add(new Employee("James", 36, 4000));
list.clear();
System.out.println(list.isEmpty()); // true
contains & containsAll
boolean |
contains(Object o) |
Returns true if this collection contains the specified element. |
---|---|---|
boolean |
containsAll(Collection<?> c) |
Returns true if this collection contains all of the elements in the specified collection. |
注意,在判断时,会调用集合元素对象的Equals方法。所以,在集合中添加自定义类的实例时,要求自定义类重写equals方法。
ArrayList list = new ArrayList();
list.add(123);
list.add(456);
list.add("hello");
list.add(new Employee("James", 36, 4000));
System.out.println(list.contains(123));
System.out.println(list.contains("hello"));
System.out.println(list.contains(new Employee("James", 36, 4000)));
ArrayList list2 = new ArrayList();
list2.add(123);
list2.add(456);
System.out.println(list.containsAll(list2));
remove & removeAll & removeIf
E remove(int index) 按索引值删除元素并返回该元素
boolean remove(Object o)
boolean removeAll(Collection<?> c)
default bool removeIf(Predicate<? super E> filter)
关于按索引值删除元素的用法,不是由Collection接口定义的,而是它的子接口List定义的。
ArrayList list = new ArrayList();
list.add(123);
list.add(456);
list.add("hello");
list.add(new Employee("James", 36, 4000));
ArrayList list2 = new ArrayList();
list2.add(123);
list2.add(456);
list2.add(new Employee("James", 36, 4000));
//list.remove(123); // 错误,会被当成按索引值删除元素
list.remove(new Integer(123));
System.out.println(list);
list.removeAll(list2);
System.out.println(list);
ArrayList<Integer> list = new ArrayList<>();
List<Integer> integers = Arrays.asList(32, 23, -32, -5, 32);
list.addAll(integers);
list.removeIf(num->num < 0);
System.out.println(list); //[32, 23, 32]
retainAll 交集
boolean |
retainAll(Collection<?> c) |
Retains only the elements in this collection that are contained in the specified collection (optional operation) |
ArrayList<Integer> list = new ArrayList<>();
list.add(1);
list.add(11);
list.add(12);
list.add(41);
ArrayList<Integer> list2 = new ArrayList<>();
list2.add(31);
list2.add(11);
list2.add(12);
list2.add(46);
list.retainAll(list2);
System.out.println(list); // [11, 12]
toArray
将集合转换为数组
Object[] |
toArray() |
Returns an array containing all of the elements in this collection. |
---|---|---|
<T> T[] |
toArray(T[] a) |
Returns an array containing all of the elements in this collection; the runtime type of the returned array is that of the specified array. |
将数组转换为集合
static <T> List<T> asList(T... a) Returns a fixed-size list backed by the specified array.
// 数组转集合
List<Integer> list = Arrays.asList(32, 12, 5); // 返回只读list
//list.add(1); // UnsupportedOperationException
/* 集合转数组 */
// 这种方法会丢失类型信息,所以实际应用很少。
Object[] objects = list.toArray();
//第二种方式是给toArray(T[])传入一个类型相同的Array
Integer[] integers = list.toArray(new Integer[list.size()]);