向Collection接口的实现类的对象(coll)中添加数据obj时,要求obj所在的类要重写equals().
/**
* @author 冀帅
* @date 2020/8/7-15:31
*/
public class Person {
private String name;
private int 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 boolean equals(Object o) {
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;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public Person() {
}
}
import org.junit.Test;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
/**
* Collection接口中声明的方法的测试
* 向Collection接口的实现类的对象(coll)中添加数据obj时,要求obj所在的类要重写equals().
* @author 冀帅
* @date 2020/8/7-15:27
*/
public class CollectionTest1 {
@Test
public void test1(){
Collection coll = new ArrayList();
coll.add(123);//123对应的包装类 自动装箱
coll.add(456);
coll.add(new String("Tom"));
coll.add(false);//包装类的Boolean
//Person p =new Person();
//coll.add(p);
coll.add(new Person("jerry",20));
// coll.add(new Person("jerry",20));//自定义类对象
//contains(Object obj):判断当前集合中是否包含obj
System.out.println(coll.contains(123));//true
System.out.println(coll.contains(new String("Tom")));//true
// System.out.println(coll.contains(p));//true
System.out.println(coll.contains(new Person("jerry",20)));//false,重写equals就为true
// 2.cotainsAll(Collection coll1):判断形参coll1中的所以元素是否都存在于当前集合中
Collection coll1 = Arrays.asList(123,456);
System.out.println(coll.containsAll(coll1));//true
}
@Test
public void test2(){
//3.remove(Object obj):从当前集合中溢出obj元素。需要重写equals
Collection coll = new ArrayList();
coll.add(123);
coll.add(456);
coll.add(new Person("jerry",20));
coll.add(new String("Tom"));
coll.add(false);
coll.remove(1234);
System.out.println(coll);
coll.remove(new Person("jerry",20));
System.out.println(coll);
//4.removeAll(Collection coll1):从当前集合中移除coll1中所有的元素,也会调用equals方法,所以得重写
Collection coll1 = Arrays.asList(123,4567,new Person("jisua",20));
coll.removeAll(coll1);
System.out.println(coll);
}
@Test
public void test3(){
Collection coll = new ArrayList();
coll.add(123);
coll.add(456);
coll.add(new Person("jerry",20));
coll.add(new String("Tom"));
coll.add(false);
//5.retainAll(Collection coll1):交集:获取当前集合和coll1集合的交集,并返回给当前集合
// Collection coll1 = Arrays.asList(123,456,new Person("jerry",20),789);
// coll.retainAll(coll1);
// System.out.println(coll);//会对coll集合进行修改
//6.equals(Object obj):
// Collection coll1 = new ArrayList();
// coll1.add(123);
// coll1.add(456); 交换位置就位false,因为list是有序的。
// coll1.add(new Person("jerry",20));
// coll1.add(new String("Tom"));
// coll1.add(false);
Collection coll1 = Arrays.asList(123,456,new Person("jerry",20),new String("Tom"),false);
System.out.println(coll.equals(coll1));
}
@Test
public void test4(){
Collection coll = new ArrayList();
coll.add(123);
coll.add(456);
coll.add(new Person("jerry",20));
coll.add(new String("Tom"));
coll.add(false);
//7.hashCode():返回当期对象的哈希值
System.out.println(coll.hashCode());
//8.集合转换为数组:toArray();
Object[] arr = coll.toArray();
for (int i = 0; i <arr.length ; i++) {
System.out.println(arr[i]);
}
//拓展:数组转化为集合:调用Ayyays类的静态方法asList()
List<String> list = Arrays.asList(new String[]{"AA", "BB", "CC"});
System.out.println(list);//自动调用toString方法
List<int[]> arr1 = Arrays.asList(new int[]{123, 456});
System.out.println(arr1);//[[I@1b9e1916] 会把整体当成一个元素
System.out.println(arr1.size());//长度为1
List<Integer> arr2 = Arrays.asList(123, 456);//[123, 456]
System.out.println(arr2);
//9.iterator():返回Iterator接口的实例,用于遍历集合元素。放在IteratorTest.java中测试
}
}

京公网安备 11010502036488号