Collection是所有单列集合最顶层的接口,里面定义了所有单列集合的共性方法:
(单列集合:Vector、ArrayList、LinkedList、TreeSet、HashSet、LinkedHashSet)
1.public boolean add(E e); 向集合中添加元素:
Collection<String> coll = new ArrayList();
coll.add("张三");
coll.add("李四");
coll.add("王五");
coll.add("赵六");
coll.add("田七");
System.out.println(coll); // [张三, 李四, 王五, 赵六, 田七]
2.public boolean remove(E e); 删除集合中的指定元素:
boolean b1 = coll.remove("田七");
System.out.println(b1); // true
System.out.println(coll); // [张三, 李四, 王五, 赵六]
3.public int size(); 得到集合的大小:
int size = coll.size();
System.out.println(size); // 4
4.public boolean contains(E e); 判断对象是否在集合中:
boolean b2 = coll.contains("张三");
System.out.println(b2); // true
5.public boolean isEmpty(); 判断集合是否为空:
boolean b3 = coll.isEmpty();
System.out.println(b3); // false
6.public Object[ ] toArray(); 将集合转换成数组:
Object[] array = coll.toArray();
for (int i = 0; i < array.length; i++) {
System.out.print(array[i] + " "); // 张三 李四 王五 赵六
}