集合框架
Collection下分为两大派系:List和Set
- List:有序,有下标,可重复
- Set:无序,无下标,不可重复
官方解释: 集合层次结构中的根界面 。 集合表示一组被称为其元素的对象。 一些集合允许重复元素,而其他集合不允许。 有些被命令和其他无序。 JDK不提供此接口的任何直接实现:它提供了更具体的子接口的实现,如Set
和List
。 该界面通常用于传递集合,并在需要最大的通用性的情况下对其进行操作。
图片来源: Java:集合,Collection接口框架图 - 那些年的事儿 - 博客园 (cnblogs.com)
Collection的特点是Set的特点
但要根据实现来定,如实例化ArrayList时元素可重复
基本使用
Collection的实现类AbstractCollection,重写了toString方法:用StringBuilder逐个拼接
- removerAll差集,留下两者中都没有的:[1,2]
- retainAll交集,留下两者中都有的:[3,4,4]
- clear()清空
public static void removeAndRetainAllTest(Collection collection,Collection collection1){
collection.add(1);
collection.add(2);
collection.add(3);
collection.add(4);
collection.add(4);
collection1.add(3);
collection1.add(4);
collection1.add(5);
collection.removeAll(collection1);
collection.retainAll(collection1);
collection.clear();
//-----------------------------------------------
/**
* removerAll差集,留下两者中都没有的:[1,2]
* retainAll交集,留下两者中都有的:[3,4,4]
*
* clear:[]
*/
System.out.println(collection);
}
官方推荐元素遍历时用Iterator接口
-
迭代器遍历时不允许用集合本身的remove方法
-
迭代器的原理是用"指针"指针元素,
-
集合自己删除时,不会改变指针位置
public static void iteratorTest(Collection collection){
Iterator iterator = collection.iterator();
/**
* 如果还有下一个元素,
* 就取出下一个元素
* 然后删除掉
*/
while (iterator.hasNext()){
Object next = iterator.next();
System.out.print("元素:"+next+"-size:");
iterator.remove();//删除当前指针位置上的元素
System.out.println(collection.size());
}
}
重写对象的equals方法:用属性来比较对象是否相等
Student实体类:
public String name;
public int age;
public char sex;
public Student(){}
public Student(String name,int age,char sex){
this.age = age;
this.name = name;
this.sex = sex;
}
重写toString()
-
Collection直接打印对象信息
-
不再默认直接打印对象地址
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
", sex=" + sex +
'}'+"\n";
}
重写equals()
- 输入equals或hashCode,找到对应的方法
- 回车:直接生成代码模板
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
if(o instanceof Student){
Student student = (Student) o;
return age == student.age && sex == student.sex && Objects.equals(name, student.name);
}
return false;
}
用new对象的方式删除元素
public static void removeObject(Collection collection){
Student student = new Student("长歌", 18, '女');
Student student1 = new Student("世民", 22, '男');
Student student2 = new Student("则天", 20, '女');
collection.add(student);
collection.add(student1);
collection.add(student2);
/**
* [Student{name='长歌', age=18, sex=女}
* , Student{name='世民', age=22, sex=男}
* , Student{name='则天', age=20, sex=女}
* ]
*/
System.out.println(collection);
//-------------------------------------------------
//new对象删除
collection.remove(new Student("长歌",18,'女'));
/**
* new对象删除后:[Student{name='世民', age=22, sex=男}
* , Student{name='则天', age=20, sex=女}
* ]
*/
System.out.println("new对象删除后:"+collection);
//--------------------------------------------------
/**
* Collection保存的是对象地址,
* 删除后对象还是能用的
*/
collection.removeAll(collection);
//Student{name='长歌', age=18, sex=女}
System.out.println(student);
}
Collection保存的是对象地址,删除后对象还是能用的
不为空和判断是否全部包含
public static void containsArrAndIsEmpty(Collection collection,Collection collection1){
collection.add(1);
collection.add(2);
collection.add(3);
collection1.add(2);
collection1.add(3);
/**
* 不为空
* 再判断是否全部包含
*/
if(!collection1.isEmpty()){
boolean b = collection.containsAll(collection1);
boolean a = collection1.containsAll(collection);
/**
* AbstractCollection实现了这个方法
*
* collection1为空时自动返回true
*/
//true-false
System.out.println(b+"-"+a);
}
}
main方法
public static void main(String[] args) {
/**
* Collection接口只能由子类实现
*/
Collection collection = new ArrayList();
Collection collection1 = new ArrayList();
//removeAndRetainAllTest(collection,collection1);
//iteratorTest(collection);
//removeObject(collection);
containsArrAndIsEmpty(collection,collection1);
}