//单列集合
Collection collection=new ArrayList();
Student s1 = new Student("张三", 23);
Student s2 = new Student("李四", 23);
Student s3 = new Student("王五", 23);
//往集合添加元素add()
collection.add(s1);
collection.add(s2);
collection.add(s3);
//把两个集合的元素,归并到一个集合里面去,collection2至collection,collection2不变;
boolean b = collection.addAll(collection2);
//删除集合中的元素
Collection collection = new ArrayList();
collection.add("aaa");
collection.remove("aaa");//删除元素
//A集合removeAll (B集合) ,会移除掉两个集合中的交集元素(仅仅是A中的删除),如果有交集元素被除了,返回true,没有交集元素可删除返回false;
boolean b = collection.removeAll(collection2);
//清空集合中所有元素;
collection2.clear();
//判断集合中有没有该元素
boolean b = collection.contains(400);
//判断集合中有没有另一个集合中的所有元素
boolean b = collection.containsAll(collection2);
//判断集合是否为空
boolean empty = collection2.isEmpty();
//size()获取集合的长度
int size = collection.size();
//A集合对B集合取交集,获取到的交集元素存在A集合中,返回的布尔值表示的是A集合是否发生变化。
boolean b = collection.retainAll(collection2);
集合(Collection)中常用的一些方法
最新推荐文章于 2022-09-06 10:56:29 发布