Collection接口集合方法

public class TestCollection {

    @Test
    public void testCollection3() {
        Collection coll = new ArrayList();
        coll.add(123);
        coll.add("AA");
        coll.add(new Date());
        coll.add("BB");
        coll.add(new Person("MM", 23));

        Collection coll1 = new ArrayList();
        coll1.add(123);
        coll1.add(new String("AA"));
        // 10.removeAll(Collection coll):求差集,从当前集合中删除包含在coll中的元素
        // 移除此 collection 中那些也包含在指定 collection 中的所有元素(可选操作)。
        coll.removeAll(coll1);
        System.out.println("$" + coll);
        // 11.equals(Object obj);
        Collection coll2 = new ArrayList();
        coll2.add(123);
        coll2.add(new String("AA"));
        System.out.println(coll1.equals(coll2));
        // hasCode(): 返回此 collection 的哈希码值。
        System.out.println(coll.hashCode());

        // toArray() 集合转化为数组,返回包含此 collection 中所有元素的数组。
        Object[] obj = coll.toArray();
        for (int i = 0; i < obj.length; i++) {
            System.out.println("%" + obj[i]);
        }
        // 数组转化为集合
        Collection colln = Arrays.asList(1, 2, 3);
        System.out.println();
        // iterator():返回在此 collection 的元素上进行迭代的迭代器。
        // 返回一个iterator接口实现类的对象,进而实现集合的遍历
        Iterator iterator = coll.iterator();
        // 方式一:
        // System.out.println(iterator.next());
        // System.out.println(iterator.next());
        // System.out.println(iterator.next());
        // 方式二:
        // for(int i = 0; i < coll.size(); i++){
        // System.out.println(iterator.next());
        // }
        // 方式三:
        while (iterator.hasNext()) {
            System.out.println(iterator.next());
        }

    }

    @Test
    public void testCollection2() {
        Collection coll = new ArrayList();
        coll.add(123);
        coll.add("AA");
        coll.add(new Date());
        coll.add("BB");
        // 6.contains(Object obj):如果此 collection 包含指定的元素,则返回 true。
        // 判断的依据:根据元素所在的类的equals()方法进行比较
        // 明确:如果存入集合中的元素是自定义类的对象,要求:自定义类要重写equals()方法

        boolean b1 = coll.contains(123);
        System.out.println(b1);
        // Person p = new Person("MM",23);
        coll.add(new Person("MM", 23));
        System.out.println(coll);
        boolean b2 = coll.contains(new Person("MM", 23));
        System.out.println(b2); // True,重写了equals()方法,比较内容。不重写就是比较地址值,返回false

        // 7.containsAll(Collection coll): 如果此 collection 包含指定 collection
        // 中的所有元素,则返回 true。
        Collection coll1 = new ArrayList();
        coll1.add(123);
        coll1.add(new String("AA"));
        boolean b3 = coll.containsAll(coll1);
        System.out.println(b3); // true
        coll1.add(456);
        // 8.retainAll(Collection coll): 仅保留此 collection 中那些也包含在指定 collection
        // 的元素(可选操作)。
        // 求交集,返回当前的集合
        coll.retainAll(coll1);
        System.out.println(coll);
        // 9.remove(Object obj): 从此 collection
        // 中移除指定元素的单个实例,如果存在的话(可选操作)。,删除成功返回true
        boolean b4 = coll.remove("BB");
        System.out.println(b4);

    }

    @Test
    public void testCollection1() {
        Collection coll = new ArrayList();
        // 1.size()集合中的元素个数
        System.out.println(coll.size());
        // 2.add(Object obj)
        coll.add(123);
        coll.add("AA");
        coll.add(new Date());
        coll.add("BB");
        System.out.println(coll.size());
        // 3.addAll(Collection coll) 将指定 collection 中的所有元素都添加到此 collection
        // 中(可选操作)。
        Collection coll1 = Arrays.asList(1, 2, 3);
        coll.addAll(coll1);
        System.out.println(coll.size());
        // 查看集合元素 :ArrayList重写了toString()方法
        System.out.println(coll);
        // 4.isEmpty() 如果此 collection 不包含元素,则返回 true
        System.out.println(coll.isEmpty());
        // 5.clear() 移除此 collection 中的所有元素(可选操作)。
        coll.clear();
        System.out.println(coll.isEmpty());
    }

}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值