Collection所有方法汇总
package com.atguigu.javase.lesson7;
import org.junit.Test;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
/**
* 1.添加元素
* 2.获取元素 & 查找指定元素
* 3.移除元素
* 4.工具方法
*/
public class CollectionTest {
/**
* 添加一个元素到集合中
*/
@Test
public void testAdd(){
Collection collection = new ArrayList();
System.out.println(collection.size());
collection.add("abc");
collection.add(new Person("Tom",21));
collection.add(new Person("Jerry",33));
collection.add(new Person("Mike",23));
System.out.println(collection.size());
}
/**
* 添加一组元素到集合中
*/
@Test
public void testAddAll(){
Collection collection2 = new ArrayList();
collection2.add("athebei");
Collection collection = new ArrayList();
collection.add("abc");
collection.add(new Person("Tom",21));
collection.add(new Person("Jerry",33));
collection.add(new Person("Mike",23));
collection2.addAll(collection);
System.out.println(collection2.size());
}
/**
* 在Collection中无法获取指定的元素,但可以遍历所有的元素
* 1.使用增强的for循环
* 2.使用Iterator迭代器
* 2.1 获取迭代器对象:调用Collection的iterator()方法,获取Iterator接口的对象
* 2.2 调用Iterator接口的方法进行迭代
*/
@Test
public void testIterator(){
Collection collection = new ArrayList();
collection.add("ABC");
collection.add(new Person("Tom",23));
collection.add(new Person("Jerry",12));
collection.add(new Person("Mike",22));
for(Object obj : collection){
System.out.println(obj);
}
Iterator iterator = collection.iterator();
while(iterator.hasNext()){
Object obj = iterator.next();
System.out.println(obj);
}
}
/**
* clear():清空集合
*/
@Test
public void testClear(){
Collection collection = new ArrayList();
collection.add("ABC");
collection.add(new Person("Tom",23));
collection.add(new Person("Jerry",12));
collection.add(new Person("Mike",22));
collection.clear();
System.out.println(collection.size());
}
/**
* remove():移除指定元素
* 通过equals()方法在集合中查找指定的元素,若存在则移除
*/
@Test
public void testRemove(){
Collection collection = new ArrayList();
collection.add("ABC");
Person p = new Person("Tom",23);
collection.add(p);
collection.add(new Person("Jerry",12));
collection.add(new Person("Mike",22));
System.out.println(collection.size());
collection.remove(p);
System.out.println(collection.size());
collection.remove(new Person("Jerry",12));
System.out.println(collection.size());
}
/**
* removeAll(Collection collection):移除collection中有的元素
*/
@Test
public void testRemoveAll(){
Collection collection = new ArrayList();
collection.add("ABC");
Person p = new Person("Tom",23);
collection.add(p);
collection.add(new Person("Jerry",12));
collection.add(new Person("Mike",22));
System.out.println(collection.size());
Collection collection2 = new ArrayList();
collection2.add("ABC");
collection2.add(new Person("Mike",22));
collection.removeAll(collection2);
System.out.println(collection.size());
}
/**
* retainAll(Collection collection):保留collection中有的元素
*/
@Test
public void testRetatinAll(){
Collection collection = new ArrayList();
collection.add("ABC");
Person p = new Person("Tom",23);
collection.add(p);
collection.add(new Person("Jerry",12));
collection.add(new Person("Mike",22));
System.out.println(collection.size());
Collection collection2 = new ArrayList();
collection2.add("ABC");
collection2.add("EDF");
collection2.add(new Person("Mike",22));
collection.retainAll(collection2);
System.out.println(collection.size());
}
/**
* contains(Object o):利用equals()方法比较,查看集合中有没有指定的元素
*/
@Test
public void testContains(){
Collection collection = new ArrayList();
collection.add("ABC");
Person p = new Person("Tom",23);
collection.add(p);
collection.add(new Person("Jerry",12));
collection.add(new Person("Mike",22));
boolean flag = collection.contains(new Person("Jerry",12));
System.out.println(flag);
}
/**
* containsAll(Collecion<?> c):查看集合中有没有指定元素的集合
*/
@Test
public void testContainsAll(){
Collection collection = new ArrayList();
collection.add("ABC");
Person p = new Person("Tom",23);
collection.add(p);
collection.add(new Person("Jerry",12));
collection.add(new Person("Mike",22));
Collection collection2 = new ArrayList();
collection2.add("ABC");
collection2.add(new Person("Mike",22));
boolean flag = collection.containsAll(collection2);
System.out.println(flag);
}
/**
* isEmpty():检验集合是否为空集合
*/
@Test
public void testIsEmpty(){
Collection collection = new ArrayList();
collection.add("ABC");
Person p = new Person("Tom",23);
collection.add(p);
collection.add(new Person("Jerry",12));
collection.add(new Person("Mike",22));
System.out.println(collection.isEmpty());
collection.clear();
System.out.println(collection.isEmpty());
}
/**
* toArray():把集合元素转为Object对象的数组
*/
@Test
public void testToArray(){
Collection collection = new ArrayList();
collection.add("ABC");
Person p = new Person("Tom",23);
collection.add(p);
collection.add(new Person("Jerry",12));
collection.add(new Person("Mike",22));
Object[] objs = collection.toArray();
System.out.println(objs.length);
}
}