/**
*演示Collection接口API介绍
*/
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
public class TestCollection {
public static void main(String[] args) {
//加了泛型
Collection<String> c = new ArrayList<String>();
c.add("tom");
c.add("lily");
c.add("kate");
//c.add("hello");
//没有加泛型
Collection c2 = new ArrayList();
c2.add("hello");
c2.add(3);
c2.add(new Date());
c2.addAll(c);//把其他集合一次性添加到本集合
System.out.println(c2);
boolean b = c2.contains(new String("hello"));//equals判断
System.out.println(b);
System.out.println(c2.containsAll(c));//equals判断
System.out.println(c2.isEmpty());
c2.remove(new String("hello"));//equals判断
System.out.println(c2);
//求交集
//c2.retainAll(c);
//System.out.println("求交集之后:"+c2);
//删除一组元素
c2.removeAll(c);//equals判断
System.out.println(c2);
System.out.println(c);
System.out.println("元素的个数:"+c2.size());
//集合転数组
Object[] arr = c2.toArray();
System.out.println("転为数组后取第0个元素:"+arr[0]);
//転成指定类型数组,前提是加了泛型限制
String[] ss = c.toArray(new String[0]);
System.out.println("転为数组后取第0个元素:"+ss[0]);
c2.clear();
System.out.println(c2);
System.out.println(c2.isEmpty());
}
}
*演示Collection接口API介绍
*/
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
public class TestCollection {
public static void main(String[] args) {
//加了泛型
Collection<String> c = new ArrayList<String>();
c.add("tom");
c.add("lily");
c.add("kate");
//c.add("hello");
//没有加泛型
Collection c2 = new ArrayList();
c2.add("hello");
c2.add(3);
c2.add(new Date());
c2.addAll(c);//把其他集合一次性添加到本集合
System.out.println(c2);
boolean b = c2.contains(new String("hello"));//equals判断
System.out.println(b);
System.out.println(c2.containsAll(c));//equals判断
System.out.println(c2.isEmpty());
c2.remove(new String("hello"));//equals判断
System.out.println(c2);
//求交集
//c2.retainAll(c);
//System.out.println("求交集之后:"+c2);
//删除一组元素
c2.removeAll(c);//equals判断
System.out.println(c2);
System.out.println(c);
System.out.println("元素的个数:"+c2.size());
//集合転数组
Object[] arr = c2.toArray();
System.out.println("転为数组后取第0个元素:"+arr[0]);
//転成指定类型数组,前提是加了泛型限制
String[] ss = c.toArray(new String[0]);
System.out.println("転为数组后取第0个元素:"+ss[0]);
c2.clear();
System.out.println(c2);
System.out.println(c2.isEmpty());
}
}