Collection中的常用方法

本文详细介绍了Java集合框架中的常用操作,如添加、判断元素存在、删除,以及集合与数组之间的转换。重点强调了元素重写equals方法的重要性。
摘要由CSDN通过智能技术生成

1.常用方法:


1.1添加


(1)add(Object obj):添加元素对象到当前集合中
(2)addAll(Collection other):添加other集合中的所有元素对象到当前集合中,即this = this U other

Collection coll = new ArrayList();

        //add()
        coll.add("AA");
        coll.add(123);//自动装箱
        coll.add("周周");
        coll.add(new Object());
        coll.add(new Person("tom",12));

        System.out.println(coll);

        //addAll(Collection other)
        Collection coll1 = new ArrayList();
        coll1.add("BB");
        coll1.add("456");

        System.out.println(coll1.size());//2

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

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


1.2判断


(3) int size0:获取当前集合中实际存储的元素个数
(4) boolean isEmpty0:判断当前集合是否为空集合
(5) boolean contains1(0bject obj):判断当前集合中是否存在一个与obj对家equal返回true的元素
(5) boolean containsAl(Collection col):判断coll集合中的元素是否在当前集合中都存在。即coll集合是否是当前集合的子集”
(7boolean equals(object obj),判断当前集合与obj是否相等

Collection coll = new ArrayList();
        //add()
        coll.add("AA");
        coll.add(128);//自动装箱
        coll.add("周周");
        coll.add(new Object());
        coll.add(new Person("tom",12));

        //isEmpty
        System.out.println(coll.isEmpty());

        //contains1(0bject obj) 比的是内容是否一致
        System.out.println(coll.contains("AA"));
        System.out.println(coll.contains(128));//true
        System.out.println(coll.contains(new Person("tom",12)));

        //containsAl(Collection coll)
        Collection coll1 = new ArrayList();
        coll1.add("AA");
        coll1.add("BB");

        System.out.println(coll.containsAll(coll1));//false


1.3删除


(8) void clear0: 清空集合元素
(9) boolean remove(Object obj):从当前集合中州除第一个找到的与obj对象equals返回true的元素。
(10) boolean removeAll(Collection coll:从当前集合中州除所有与coll集合中相同的元素。即this =this-this n coll
(11) boolean retainAl(Collection coll):从当前集合中删除两个集合中不同的元素,使得当前集合仅保留与col集合中的元素相同的元素,即当前集合中仅保留两个集合的交集,即this =this n coll;
注意几种删除方法的区别

Collection coll = new ArrayList();
        //add()
        coll.add("AA");
        coll.add("AA");
        Person p1 = new Person("tom",12);
        coll.add(128);//自动装箱
        coll.add("周周");
        coll.add(new Object());
        coll.add(new Person("tom",12));

//        //清空集合元素
//        coll.clear();
//        System.out.println(coll);//[]
//        System.out.println(coll.size());//0

        //remove(Object obj)
        coll.remove(new Person("tom",12));
        System.out.println(coll);
        coll.remove("AA");//只删除一个,找到了就停止


1.4其它


(12) object[] toArray0:返回包含当前集合中所有元素的数组
(13) hashcode0:获取集合对象的哈希值
(14) iterator0:返回迭代器对象,用于集合遍历

Collection coll = new ArrayList();
        //add()
        coll.add("AA");
        coll.add("AA");
        Person p1 = new Person("tom",12);
        coll.add(128);//自动装箱
        coll.add("周周");
        coll.add(new Object());
        coll.add(new Person("tom",12));

        //集合-->数组
        Object[] arr = coll.toArray();
        System.out.println(Arrays.toString(arr));

        //hashcode()
        System.out.println(coll.hashCode());


2.集合与数组之间的转换


数组-->集合:调用Array的静态方法asList(Object ... objs)

//数组-->集合
    @Test
    public void test6(){
        Integer[] arr = new Integer[]{1,2,3};
        List list = Arrays.asList(arr);
        System.out.println(list.size());//3

        int[] arr1 = new int[]{1,2,3};
        List list1 = Arrays.asList(arr1);
        System.out.println(list1.size());//3
    }


集合-->数组:toArray()

//集合-->数组
    @Test
    public void test5(){
        String[] arr = new String[]{"AA","BB","CC"};
        Collection list = Arrays.asList(arr);
        System.out.println(list);
    } 


3.向Collection中添加元素的要求


要求元素所属的类必须重写equals()方法
原因:
Collection中的相关方法(比如:contains(),remove())在使用时,要调用元素所在类的equals()

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值