-
特点:代表一组任意类型的对象,无序、无下标、不可重复
-
方法:
- boolean add(Object obj) //添加一个对象
- boolean addAll(Collection c) //将一个集合中的所有对象添加到此集合中
- void clear() //清空此集合中的所有对象
- boolean contains(Object o) //检查此集合中是否包含o对象
- boolean equals(Object o) //比较此集合是否与指定对象相等
- boolean isEmpty() //判断此集合是否为空
- boolean remove(Object o) //在此集合中移除o对象
- int size() //返回此集合中的元素个数
- Object[] toArray() //将此集合转换成数组
//创建集合 Collection collection = new ArrayList(); //添加元素 collection.add("apple"); collection.add("orange"); collection.add("pear"); System.out.println("元素个数:" + collection.size()); //元素个数:3 System.out.println(collection); //[apple,orange,pear] //遍历元素 //1.增强for循环 for(Object obj : collection){ System.out.println(obj); //apple orange pear } //2.使用迭代器(专门用来遍历集合的一种方式) //hasNext(); 有没有下一个元素 //next(); 获取下一个元素 //remove(); 删除当前元素(迭代过程中不能使用collection的删除方法) Iterator it = collection.iterator(); while(it.hasNext()){ String s = (String)it.next(); Systrm.out.println(s); } //判断 System.out.println(collection.contains("apple")); //true System.out.println(collection.isEmpty()); //false //删除元素 collection.remove("pear"); System.out.println("删除之后:" + collection.size()); //删除之后:2 collection.clear(); System.out.println("删除之后:" + collection.size()); //删除之后:0
//学生类 public class Student { private String name; private int age; public Student(String name, int age) { super(); this.name = name; this.age = age; } public Student() { } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String toString() { return "Student [name=" + name + ", age=" + age + "]"; } @Override public boolean equals(Object obj){ //判断是不是同一个对象 if(this == obj){ return true; } //判断是否是空 if(obj == null){ return false; } //判断是否是Student类型 if(obj instanceof Student){ Student s = (Student)obj; //比较属性 if(this.name.equals(s.getName()) && this.age == s.getAge()){ return true; } } //不满足条件返回false return false; } } //Collection使用:保存学生信息 public class Test{ public static void main(String[] args){ //创建集合 Collection collection = new ArrayList(); Student s1 = new Student("张三",20); Student s2 = new Student("李四",21); Student s3 = new Student("王五",22); //添加数据 collection.add(s1); collection.add(s2); collection.add(s3); System.out.println("元素个数:" + collection.size()); //元素个数:3 //遍历 //1.增强for循环 for(Object obj : collection){ Student stu = (Student)obj; System.out.println(stu.toString()); } //2.迭代器 Iterator it = collection.iterator(); while(it.hasNext()){ Student s = (Student)it.next(); System.out.println(s.toString()); } //判断 System.out.println(collection.contains(s1)); //true System.out.println(collection.contains(new Student("李四",21))); //重写equals方法后返回true System.out.println(collection.isEmpty()); //false //删除数据 collection.remove(s1); System.out.println("删除之后:" + collection.size()); //删除之后:2 collection.remove(new Student("李四",21)); //重写equals方法才能实现删除 System.out.println("删除之后:" + collection.size()); //删除之后:1 } }
Collection接口
最新推荐文章于 2024-10-01 18:51:18 发布