集合批量处理的方法
Collection conn = new ArrayList();
conn.add("ad");
conn.add("faaf;");
conn.add("gsg5");
conn.add("sgh,");
System.out.println(conn);
Collection conn2 = new ArrayList();
conn2.add("java");
conn2.add("ios");
/*
* boolean addAll(Collection c)
* 将给定的集合中的所有元素添加到当前集合
* 当调用该方法后当前集合元素发生变化则返回true
*/
conn.addAll(conn2);
System.out.println(conn);
Collection conn3 = new ArrayList();
conn3.add("java");
conn3.add("ios");
/*
* boolean containsAll(Connection c)
* 查看当前集合中是否包含给定集合中的所有元素
* 全包含则返回true
* 这里判断含有还是根据元素的equals判断的结果
*/
boolean containsAll = conn.containsAll(conn3);
System.out.println("全包含"+containsAll);
删除给定数据
Collection c = new ArrayList();
c.add(new Cell(1,2));
c.add(new Cell(2,3));
c.add(new Cell(3,4));
c.add(new Cell(1,2));
Cell cell = new Cell(1,2);
System.out.println(c);
/*
* boolean remove(Object o)
* 从当前集合中删除给定数据
* 删除元素是根据元素的equals的结果判断的
* 从当前集合中删除与给定元素第一个equals比较为true的
*
*/
c.remove(cell);
System.out.println(c);
//[Cell [x=2,y=3], Cell [x=3,y=4], Cell [x=1,y=2]]
泛型
Collection<String> c = new ArrayList<String>();
c.add("one");
c.add("two");
c.add("three");
c.add("four");
for(String o : c){
System.out.println(o);
}
/*
* 迭代器同样支持泛型,而迭代器声明的泛型
* 应该与其遍历的集合的泛型类型一致
*/
Iterator<String> it = c.iterator();
while(it.hasNext()){
String str = it.next();
System.out.println(str);
}