2.集合框架-Collection接口中方法的使用

Collection接口的常用方法

1. 集合框架总览

在这里插入图片描述

2.Collection接口常用方法

  • add(Object e):将元素e 添加到集合coll中
  • size():获取添加的元素的个数
  • clear():清空集合元素
  • isEmpty():判断当前集合是否为空(判断集合中是否有元素)
  • 1.contains(Object obj):判断当前集合中是否包含obj
  • 2.containsAll(Collection coll1):判断形参coll1中的所有元素是否都存在于当前集合中
  • 3.remove(Object obj):从当前集合中移除obj 元素
  • 4.removeAll(Collection coll1):从当前集合中移除coll1中所有的元素
  • 5.retainAll(Collection coll1):交集,获取当前集合和coll1集合的交集,并返回给当前集合
  • 6.equals(Object obj):要想返回true 需要当前集合和形参集合的元素都相同
  • 7.hashCode() :返回当前对象的哈希值
  • 8.集合—>数组 :toArray()
  • 拓展:数组—> 集合:调用Arrays类的静态方法asList()
  • 9.iterator():返回iterator接口的实例,用于遍历集合元素(iterator的使用细节请见本篇第一章)

方法使用细节(方法较多已标好序号)

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

        //add(Object e):将元素e 添加到集合coll中
        coll.add("aa,bb");
        coll.add("bb");
        coll.add(123);//自动装箱
        coll.add(new Date()) ;

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

        //addAll(Collection coll2):将coll2中的元素添加到当前的集合中
        Collection coll2 = new ArrayList();
        coll2.add(234);
        coll.add("cc");
        coll.addAll(coll2);

        System.out.println(coll.size());//6
        System.out.println(coll);

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

        //isEmpty():判断当前集合是否为空(判断集合中是否有元素)
        System.out.println(coll.isEmpty());
    }


}
序号1-8的方法

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

        //1.contains(Object obj):判断当前集合中是否包含obj
        boolean contains = coll.contains(123);//true
        System.out.println(contains);

        boolean contains1 = coll.contains(new String("Tom"));//true String重写了Equals() 比较字符串
        System.out.println(contains1);

        boolean contains2 = coll.contains(new Person("Jerry", 20));//false Person没有重写equals() 比较的是地址
        System.out.println(contains2);
        System.out.println("---------------------");
        //2. containsAll(Collection coll1):判断形参coll1中的所有元素是否都存在于当前集合中
        Collection coll1 = Arrays.asList(123,456);
        System.out.println(coll1.containsAll(coll1));
    }
    @Test
    public void test2() {
        //3.remove(Object obj):从当前集合中移除obj 元素

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

        boolean remove = coll.remove(123);
        System.out.println(remove);

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

    }

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

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

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

        //6.equals(Object obj):要想返回true 需要当前集合和形参集合的元素都相同
        Collection coll1 = new ArrayList();

        coll1.add(123);
        coll1.add(456);
        coll1.add(new String("Tom"));
        coll1.add(false);
        coll1.add(new Person("Jerry", 20));

        System.out.println(coll.equals(coll1));

    }

    @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("Jerry", 20));

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

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

        List<int[]> arr1 = Arrays.asList(new int[]{123, 456});
        System.out.println(arr1.size());//将new int[]{123, 456}看成一个对象 所以size()的长度为1

        List<Integer> arr2 = Arrays.asList(123, 456);
        System.out.println(arr2.size());

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

备注

  • 迭代器的使用见本篇第一章
  • 集合与数组的比较见本篇第三章
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值