Java--Collection接口中的使用

Collection接口中的使用

接下来用一系列的方法进行测试,用注释来说明各方法的用途:

(1)代码:

   @Test
    public void test1(){
        Collection coll=new ArrayList();

        //add(Object obj):将元素添加到集合coll中
        coll.add("AA");
        coll.add("BB");
        coll.add(123);//自动装箱
        coll.add(new Date());

        //size():获取添加的元素个数
        System.out.println(coll.size());

        //addAll():将一个集合中的元素添加到一个集合中
        Collection coll1=new ArrayList();
        coll1.add(456);
        coll1.add("CC");
        coll.addAll(coll1);

        System.out.println(coll);

        //clear():清空集合元素
        coll.clear();

        //isEmpty:判断集合是否为空
        coll.isEmpty();
        System.out.println(coll.isEmpty());

    }

使用到的方法:

//add(Object obj):将元素添加到集合coll中

//size():获取添加的元素个数

//clear():清空集合元素

//isEmpty:判断集合是否为空


(2)代码:

    @Test
    public void text2(){
        Collection coll=new ArrayList();
        coll.add(123);
        coll.add(new String("Tom"));
        coll.add(false);
        coll.add(new Person("Tom",20));

        //contains(Object obj):判断当前是否存在obj
        //当我们在判断时会调用obj对象所在类的equals()方法。
        coll.contains(123);
        System.out.println(coll.contains(new String("Tom")));//true
        System.out.println(coll.contains(new Person("Tom",20)));//false

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

    }

这里要注意:就是当我们用contains去比较new String(obj)的时候,返回时true,因为原本的包装类里面已经写好了equals方法。如果我们比较自定义类时,如果不重写equals方法会返回false,所以要记得重写equals方法。

使用到的方法:

//contains(Object obj):判断当前是否存在obj
//当我们在判断时会调用obj对象所在类的equals()方法。

//containsAll(Collection coll):判断形参coll1中的所有元素是否都存在于当前集合中


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

        coll.remove(123);

        //4,removeAll(Collection coll1):从当前集合中移除coll1中所有的元素(将一个集合中起初和一个集合共有的元素)
        Collection coll1=Arrays.asList(123);
        coll.removeAll(coll1);
        System.out.println(coll);

    }

(3)代码:

  @Test
    public void test4(){

        Collection coll=new ArrayList();
        coll.add(123);
        coll.add(456);
        coll.add(new String("Tom"));
        coll.add(false);
        coll.add(new Person("Tom",20));

        //5,equals(Object obj):
        Collection coll2=new ArrayList();
        coll2.add(123);
        coll2.add(456);
        coll2.add(new String("Tom"));
        coll2.add(false);
        coll2.add(new Person("Tom",20));
        System.out.println(coll.equals(coll2));

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

使用到的方法:

//equals(Object obj):因为ArrayList是有序的,如果123和456换个位置,那么将会返回false。

//6,retainAll(Collection coll1):交集:获取当前集合和coll1集合的交际;并返回给当前集合。


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

        //hashCode():返回当前对象的哈希值
        System.out.println(coll.hashCode());

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

        //数组---》集合:调用Arrays类的静态方法asList()
        List<String> list=Arrays.asList(new String[]{"AA","BB","CC"});
        System.out.println(list);

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

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

使用到的方法:

//hashCode():返回当前对象的哈希值

//toArray():集合—》数组

//数组—》集合:调用Arrays类的静态方法asList()

发布了49 篇原创文章 · 获赞 0 · 访问量 1400

猜你喜欢

转载自blog.csdn.net/qq_43616001/article/details/104058176