Collection接口中声明的方法和测试

package com.collectiontest.Demo01;

import org.junit.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;

/*
    Collection接口中声明的方法和测试
    向Collection接口的实现类的对象中添加数据时,要求obj所在类要重写equals()

 */
public class CollectionTest {

    @Test
    public void test1() {
        Collection coll = new ArrayList();
        coll.add(123);
        coll.add(456);
        coll.add(new Person("Jerry", 20));
        coll.add(new String("Tom"));
        coll.add(false);
//        Person p = new Person("Jerry",20);
//        coll.add(p);


        //1.contains(Object obj):判断当前集合中是否包含obj
        //我们会在判断时会调用obj对象所在类的equals()
        boolean x = coll.contains(123);
        System.out.println(x);//true
        System.out.println(coll.contains(new String("Tom")));
//        System.out.println(coll.contains(p));//true
        System.out.println("===========================");
        System.out.println(coll.contains(new Person("Jerry", 20)));


        //2.containAll(Collection coll1):判断形参coll1中所有元素是否都存在于当前集合中
        Collection coll1 = Arrays.asList(123, 456);
        System.out.println(coll.containsAll(coll1));

    }

    @Test
    public void test2() {
        //3.remove(Object obj):从当前集合中移除obj元素
        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(123);
        System.out.println(coll);
        System.out.println("--------------");
        coll.remove(new Person("Jerry", 20));
        System.out.println(coll);


        //4.removeAll(Collection coll1):从当前集合中移除coll1中所有的元素
        Collection coll1 = Arrays.asList(123, 456, new Person("jerry", 20));
        coll.removeAll(coll1);
        System.out.println(coll);

    }

    @Test
    public void test3() {
        //5.remove(Object obj):从当前集合中移除obj元素
        Collection coll = new ArrayList();
        coll.add(123);
        coll.add(456);
        coll.add(new Person("Jerry", 20));
        coll.add(new String("Tom"));
        coll.add(false);

//        //6.retainAll(Collection coll1):交集:获取当前集合和coll1集合的交集,并返回值
//        Collection coll1 = Arrays.asList(123, 456, 789);
//        coll.retainAll(coll1);
//        System.out.println(coll);


        //7.equals(Object obj):
            Collection coll1 = new ArrayList();
            coll1.add(123);
            coll1.add(456);
            coll1.add(new Person("Jerry", 20));
            coll1.add(new String("Tom"));
            coll1.add(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.hasCode():返回当前对象的哈希值
            System.out.println(coll.hashCode());

            //8.集合--->数组:toArray()
            Object[] arr = coll.toArray();
            for (int i = 0; i < arr.length; i++) {
                System.out.println(arr[i]);
            }

            //拓展:数组--->集合
            List<String> list = Arrays.asList(new String[]{"AA", "BB", "CC"});
            System.out.println(list);

            //!!!!!!List arr1 = Arrays.asList(new int[]{123, 456});
            List arr1 = Arrays.asList(new int[]{123, 456});
            //List arr1 = Arrays.asList{123, 456};
            System.out.println(arr1);//123,456
            System.out.println(arr1.size());


            List arr2 = Arrays.asList(new Integer[]{123, 456});
            System.out.println(arr2);
            System.out.println(arr2.size());

            //9.iterator():返回Iterator接口的实例,用于遍历集合元素。放在IteratorTest.java中检测

        }
    }

运行结果
Collection接口中声明的方法和测试_第1张图片

package com.collectiontest.Demo01;

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;
//    }
}

你可能感兴趣的:(java)