Collection常用方法

//Collection接口中方法的使用:
//    要求:向Collection接口的实现类的对象中添加数据obj时,要求obj所在类要重写equal().
public class CollectionTest1 {
    @Test
    public void test1(){

        Collection coll=new ArrayList();
        //6.contains(Object obj):判断当前集合中是否包含obj
        //7.我们在判断时会调用obj对象所在类的equal()
        coll.add("AA");
        coll.add("BB");
        coll.add("AA");
        coll.add(123);//自动装箱
        coll.add(new String("Tom"));


        boolean contains = coll.contains(123);
        System.out.println(contains);//true

        System.out.println(coll.contains(new String("Tom")));//true
//      8.containsAll(Collectiom coll1):判断形参coll1中的所有元素是否都存在与当前集合中
        Collection coll1= Arrays.asList(123,"AA");
        System.out.println(coll.containsAll(coll1));//true

    }
    @Test
    public void test2(){
//        9.remove(Object obj):从当前集合中移除Obj元素
        Collection coll=new ArrayList();
        coll.add("AA");
        coll.add("BB");
        coll.add("AA");
        coll.add(123);//自动装箱
        coll.add(new String("Tom"));

        coll.remove(123);
        System.out.println(coll);//[AA, BB, AA, Tom]

//        10.removeAll(Collection coll1):从当前集合中移除coll1中所有元素
        Collection coll1=Arrays.asList("AA",123);
        coll.removeAll(coll1);
        System.out.println(coll);
    }
    @Test
    public void test3(){
        Collection coll=new ArrayList();
        coll.add("AA");
        coll.add("BB");
        coll.add("AA");
        coll.add(123);//自动装箱
        coll.add(new String("Tom"));

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

        */



//       equals(Object obj):判断当前集合和形参集合元素是否相同
        Collection coll2=new ArrayList();
        coll2.add("AA");
        coll2.add("BB");
        coll2.add("AA");
        coll2.add(123);//自动装箱
        coll2.add(new String("Tom"));

        System.out.println(coll.equals(coll2));//true
        }

        @Test
    public void test4(){
            Collection coll=new ArrayList();
            coll.add("AA");
            coll.add("BB");
            coll.add("AA");
            coll.add(123);//自动装箱
            coll.add(new String("Tom"));

//            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]);

//           14.数组转换为集合:调用Arrays类的静态方法asList()
                List<String> strings = Arrays.asList(new String[]{"aa", "bb", "cc"});
                System.out.println(strings);

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

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

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

            }
        }

}```

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值