- 向Collection接口的实现类的对象中添加数据obj时,要求obj所在类要重写equals().
Collection没有提供直接的实现类,而是提供了List、Set实现类
1.结合Collection中存储的如果是自定义类的对象,需要自定义类重写哪个方法?为什么?
equals()方法
List:equals()方法
Set:(HashSet、LinkedHashSet为例) : equals()、hashCode()
(TreeSet为例) : Comparable:comparaeTo(Object obj)
Comparator:compare(Object o1,Object o2)f
package com.atguigu.java1;
import org.junit.Test;
import java.util.*;
/**
* Collection接口中声明的方法的测试
*
* 向Collection接口的实现类的对象中添加数据obj时,要求obj所在类要重写equals().
*
* @author Dxkstart
* @create 2021-05-19 14:05
*/
public class CollectionTest1 {
@Test
public void test(){
Collection coll = new ArrayList();
//1.add(Object e):将元素e添加到集合coll中
coll.add("AA");
coll.add("BB");
coll.add(123);
coll.add(new Date());
//2.size():
System.out.println(coll.size());
System.out.println(coll);
//3.addAll(Collection coll1):将coll集合中的元素添加到当前的集合中
Collection coll1 = new ArrayList();
coll1.add("WW");
coll.addAll(coll1);
System.out.println(coll.toString());
//4.clear():清空集合中的元素
coll.clear();
//5.isEmpty():判断集合是否为空
System.out.println(coll.isEmpty());
}
@Test
public void test1(){
Collection coll = new ArrayList();
coll.add("AA");
coll.add(123);
coll.add(new String("Tom"));
coll.add(new Person("Jerry",20));
coll.add(false);
//6.contains(Object obj):判断当前集合中是否包含obj
//我们在判断时会调用obj对象所在类的equals()。
boolean contains = coll.contains("AA");
System.out.println(contains);
System.out.println(coll.contains(new String("Tom")));//true,用equals比较的
System.out.println(coll.contains(new Person("Jerry",20)));//false
//没有重写equals()
//7.containsAll(Collection coll1):判断形参coll1中的所有元素是否都存在于当前集合中。
Collection coll1 = Arrays.asList(123,456);
System.out.println(coll.containsAll(coll1));
}
@Test
public void test2(){
//8.remove(Object obj):从当前集合中移除obj元素
Collection coll = new ArrayList();
coll.add("AA");
coll.add(123);
coll.add(new String("Tom"));
coll.add(new Person("Jerry",20));
coll.add(false);
System.out.println(coll.size());
System.out.println(coll.remove(123));
System.out.println(coll.size());
coll.remove(new Person("Jerry",20));
System.out.println(coll.size());
//9.removeAll(Collection coll1):差集:从当前集合中移除coll1中所有的元素
Collection coll1 = new ArrayList();
coll1.add(false);
coll.removeAll(coll1);
System.out.println(coll.size());
}
@Test
public void test3(){
Collection coll = new ArrayList();
coll.add("AA");
coll.add(123);
coll.add(new String("Tom"));
coll.add(new Person("Jerry",20));
coll.add(false);
//10.retainAll(Collection coll1):交集:获取当前集合和coll1集合的交集,并返回给当前集合
Collection coll1 = Arrays.asList(123,456,789);
coll.retainAll(coll1);
System.out.println(coll);
//11.equals(Object obj):要想返回true,需要当前集合和形参集合的元素都相同。
System.out.println(coll.equals(coll1));
}
@Test
public void test4(){
Collection coll = new ArrayList();
coll.add("AA");
coll.add(123);
coll.add(new String("Tom"));
coll.add(new Person("Jerry",20));
coll.add(false);
//12.hashCode():返回当前对象的哈希值
System.out.println(coll.hashCode());
//13.toArray():集合 ----> 数组
Object[] arr = coll.toArray();
for(int i = 0;i<arr.length;i++){
System.out.println(arr[i]);
}
//拓展:asList():数组 ---->集合:调用Arrays类的静态方法asList()
List<Object> list = Arrays.asList(arr);
System.out.println(list);
List arr1 = Arrays.asList(new int[]{123, 456});
System.out.println(arr1);//这样会识别成一个数,不对
List arr2 = Arrays.asList(123, 456);
System.out.println(arr2);
//14.iterator():返回Iterator接口的实例,用于遍历集合元素。放在IteratorTest.java中测试
}
}
package com.atguigu.java1;
/**
* @author Dxkstart
* @create 2021-05-19 14:15
*/
public class Person {
private String name;
private int age;
public Person() {
}
public Person(String name, int age) {
this.name = name;
this.age = age;
}
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 "Person{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
@Override
public boolean equals(Object o) {
System.out.println("Person equals()....");
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Person person = (Person) o;
if (age != person.age) return false;
return name != null ? name.equals(person.name) : person.name == null;
}
// @Override
// public int hashCode() {
// int result = name != null ? name.hashCode() : 0;
// result = 31 * result + age;
// return result;
// }
}