代码实例:
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import org.junit.Test;
public class TestCollection {
@Test
public void testCollection1() {
Collection col = new ArrayList();
//1.size():返回集合中元素的个数
System.out.println(col.size());//0
//2.add(Object obj):向集合中添加元素
col.add(123);
col.add(new String("AAA"));
col.add(new Date());
System.out.println(col.size());//3
Collection col1 = Arrays.asList(1,2,3,"BB");
/**
* public static <T> List<T> asList(T... a) {
* return new ArrayList<T>(a);
* }
*/
//3.addAll(Collection col):将形参col中包含的所有元素添加到当前集合中
col.addAll(col1);
System.out.println(col.size());//7
//查看集合元素
System.out.println(col);
//4.isEmpty():判断集合是否为空
System.out.println("col中是否为空:" + col.isEmpty());//false
//5.clear():清空集合元素
col.clear();
System.out.println("col中是否为空:" + col.isEmpty());//true
}
@Test
public void testCollection2() {
Collection col = new ArrayList();
col.add(123);
col.add(new String("AAA"));
col.add(new Date());
col.add(new TestObject1("MM"));
col.add(new TestObject2("GG"));
//6.contains(Object obj):判断集合中是包含指定的元素。如果包含返回true,否则,返回false。
//判断依据:根据元素所在类的equals()方法进行判断
//明确:如果存入集合中的元素是自定义的对象。要求:自定义类要重写equals()方法。
boolean b1 = col.contains(new String("AAA"));//String类本身就已重写了Object类中的equals()方法
System.out.println(b1);//true
boolean b2 = col.contains(new TestObject1("MM"));//TestObject1类重写了equals()方法
System.out.println(b2);//true
boolean b3 = col.contains(new TestObject2("GG"));//TestObject2类未重写equals()方法
System.out.println(b3);
//7.containsAll(Collection col):判断当前集合中是否包含col中的所以的元素
Collection col2 = new ArrayList();
col2.add(123);
col2.add(new TestObject1("MM"));
boolean b4 = col.containsAll(col2);
System.out.println("7:" + b4);
//8.retainAll(Collection col):求当前集合与col的共有元素,返回给当前集合。有修改过col则返回true。
boolean b5 = col.retainAll(col2);
System.out.println(b5 + "+" + col);//[123, TestObject1 [name=MM]]
//9.remove(Object obj):删除集合中的obj元素。若删除成功返回true,否则返回false。
boolean b6 = col.remove(new TestObject2("GG"));
System.out.println("9:" + b6);
}
@Test
public void testCollection3() {
Collection col = new ArrayList();
col.add(123);
col.add(new String("AAA"));
col.add(new Date());
col.add(new TestObject1("MM"));
col.add(new TestObject2("GG"));
Collection col1 = new ArrayList();
col1.add(123);
col1.add(456);
col1.add(new String("AAA"));
//10.removeAll(Collection col):从当前集合中删除包含在col中的元素
col.removeAll(col1);
System.out.println(col);
//11.equals(Object obj):判断集合中的所有元素是否相同
Collection col2 = new ArrayList();
col2.add(123);
col2.add(456);
col2.add(new String("AAA"));
System.out.println("11:" + col1.equals(col2));//true
//12.hashCode()
System.out.println("12:" + col1.hashCode());
//13.toArray():将集合转化为数组
Object[] obj = col.toArray();
for (int i = 0; i < obj.length; i++) {
System.out.println(obj[i]);
}
//14.iterator():返回一个Iterator接口的实现类的对象
Iterator iterator = col.iterator();
while(iterator.hasNext()) {
System.out.println(iterator.next());
}
}
}
//重写equals()方法
class TestObject1 {
private String name;
public TestObject1(String name) {
super();
this.name = name;
}
@Override
public boolean equals(java.lang.Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
TestObject1 other = (TestObject1) obj;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
@Override
public String toString() {
return "TestObject1 [name=" + name + "]";
}
}
//未重写equals()方法
class TestObject2 {
private String name;
public TestObject2(String name) {
super();
this.name = name;
}
@Override
public String toString() {
return "TestObject2 [name=" + name + "]";
}
}