集合体系结构Collection

集合

Collection接口

Collection层次结构中的根接口,Collection表示一组对象,这些对象也称为Collection的元素,Collection是单列集合,分为List和Set两大类List接口里可以重复,有ArrayList,LinkList,Vector等实现类,Set接口里不可以重复,有HashSet,TreeSet等实现类

Collection接口的实现

首先,Collection是一个接口,所以我们想要调用这个接口的时候,就需要用到接口多态的概念,用子类的对象来调用父类的实现java面向对象三大特征之多态_zhongs11的博客-CSDN博客

我们在查询Collection子类的时候发现,Collection类下还有两个接口,分别是List接口和Set接口,我们不可能用接口来实现实例化创建对象,所以我们要从List接口和Set接口继续往下找,这里我是用的是List接口下的ArrayList子类来实现Collection的接口多态的实现。

public class CollectDemo1 {
    public static void main(String[] args) {
        Collection collection = new ArrayList();
//        1、添加功能
//        boolean add(Object e)确保此集合包含指定的元素(可选操作)
//        boolean addAll(Collection c) 将指定集合中的所有元素添加到此集合(可选操作)
        System.out.println(collection.add("hello"));
        System.out.println(collection.add("world"));
        collection.add("java");
        collection.add("java");//这边是可以添加重复数据的
        System.out.println(collection);
        //创建一个新的对象。把上一个对象的东西传进去
        Collection collection1 = new ArrayList();
        collection1.addAll(collection);
        System.out.println(collection1);
        System.out.println("=======================");
//        2、删除功能
//        boolean remove(Object o) 从该集合中删除指定元素的单个实例(如果存在)(可选操作)。
//        boolean removeAll(Collection c) 删除指定集合中包含的所有此集合的元素(可选操作)。
//        void clear() 从此集合中删除所有元素(可选操作)。
        System.out.println(collection.remove("hello"));
        System.out.println(collection);
        System.out.println(collection.remove("hadoop"));
        System.out.println(collection);
        collection1.clear();
        System.out.println(collection1);
        System.out.println("===========================");
/*        4、判断功能
        boolean contains(Object o) 如果此集合包含指定的元素,则返回 true 。
        boolean containsAll(Collection c) 如果此集合包含指定 集合中的所有元素,则返回true。
        boolean isEmpty() 如果此集合不包含元素,则返回 true 。*/
        System.out.println(collection.contains("hello"));
        System.out.println(collection.contains("world"));
        System.out.println(collection1.isEmpty());
        System.out.println("======================================");

/*        5、获取长度方法:
        int size() 返回此集合中的元素数*/
        System.out.println(collection.size());
        System.out.println("=======================================");
/*        6、求交集功能
        boolean retainAll(Collection c) 仅保留此集合中包含在指定集合中的元素(可选操作)。*/
        collection1.add("world");
        collection1.add("hello");
        System.out.println(collection1.retainAll(collection));
        System.out.println(collection1);
        System.out.println("=========================================");
/*        7、将集合转成数组
        Object[] toArray() 返回一个包含此集合中所有元素的数组。*/
        Object[] object=collection.toArray();
        for (int i =0;i<object.length;i++){
            System.out.println(object[i]);
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值