Java集合Colleciton接口

来源:互联网 发布:阿里云短信群发接口 编辑:程序博客网 时间:2024/05/17 08:22

概述:

1,Collection定义:继承Iterable,具有泛型功能,Collection集合体系顶级父接口。

2,Collection方法:add、addAll; clear、isEmpty、size; toArray(重载); iterator; contains、containsAll; remove、removeAll、retainAll;

 

一,首先看一下Collection的定义

public interface Collection<E> extends Iterable<E>从定义中我们可以看出Collection是一个带泛型的接口。实现了Iterable接口,也就是说可以使用迭代器。以上两点很重要,其下所有子类均有这两个属性。还有一点大家需要注意Collection集合并没有定义查找的方法。

二,我们再来看看Colleciton的自有方法(不含继承的方法)。

1,关于add和addAll。

两者都是往集合中添加元素(各自的子类会具体实现)。

前者是添加单个的元素,后者是可以添加一个实现了Collection的子类集合。

例如(例子中我特意使用了不同的Collection子类):

    @Test    public void testAdd(){        Collection<String> collection = new LinkedList<>();        //添加一个对象        collection.add("person1");        collection.add("person2");        List<String> list = new ArrayList<>();        list.add("person3");        Set<String> set = new HashSet<>();        set.add("person4");        //添加一个Collection集合。        collection.addAll(list);        collection.addAll(set);        collection.forEach(System.out::println);//打印控制台    }

 

2,clear、isEmpty、size。

这几个方法比较简单粗暴就放一起了,顺便也不给代码演示了。

clear清空集合里的所有元素。

isEmpty判断集合中是否还有元素,为空时返回true。

size获得集合中元素的个数。

 

3,关于把集合转换成数组 toArray。

toArray重载的方法,一个是无参,一个数需要传入一个存在的数组。

咱们先来说无参的Object[] toArray()。它返回的是一个Object数组,那么问题来了。

如果你需要String[] objects = (String[]) collection.toArray(); 这么做时,他会抛出一个ClassCastException异常。

那么你可能知道带参数的<T> T[] toArray(T[] a)是干嘛的,看下面。

下面这段代码打印的结果如下,也就是说任何情况下都会返回一个一个数组对象。

当传递的数组长度小于集合的size时,会单独返回一个新的数组,而且传递的数组不填入数据。

当传递的数组等于或者大于结合的size时,则填充传入数组,且返回该数组。

注:所以一般我们应该采用带参的方法的第二种情况的使用方式

---打印给定的数组小于集合的情况-----
strings: [null, null]
returnStrings: [escore, wym, cl]
strings==returnStrings: false
---打印给定的数组等于集合的情况-----
strings: [escore, wym, cl]
returnStrings: [escore, wym, cl]
strings==returnStrings: true
---打印给定的数组大于集合的情况-----
strings: [escore, wym, cl, null, null]
returnStrings: [escore, wym, cl, null, null]
strings==returnStrings: true

        Collection<String> collection = new LinkedList<>();        collection.add("escore");        collection.add("wym");        collection.add("cl");       // String[] objects = (String[]) collection.toArray(); //会抛出ClassCastException异常        Object[] objects = collection.toArray();        //System.out.println(Arrays.toString(objects));        String[] strings = new String[2];        String[] returnStrings = collection.toArray(strings);        System.out.println("---打印给定的数组小于集合的情况-----");        System.out.println("strings: "+ Arrays.toString(strings));        System.out.println("returnStrings: " + Arrays.toString(returnStrings));        System.out.println(strings == returnStrings);        String[] strings2 = new String[collection.size()];        String[] returnStrings2 = collection.toArray(strings2);        System.out.println("---打印给定的数组等于集合的情况-----");        System.out.println("strings: "+ Arrays.toString(strings2));        System.out.println("returnStrings: " + Arrays.toString(returnStrings2));        System.out.println(strings2 == returnStrings2);        String[] strings3 = new String[5];        String[] returnStrings3 = collection.toArray(strings3);        System.out.println("---打印给定的数组大于集合的情况-----");        System.out.println("strings: "+ Arrays.toString(strings3));        System.out.println("returnStrings: " + Arrays.toString(returnStrings3));        System.out.println(strings3 == returnStrings3);

 

4Iterator<E> iterator()

关于返回一个迭代器的方法,在这里我们就不讨论了,参看Iterator的内容借口。

 

5,contains 、containsAll ;remove 、removeAll、retainAll

contains、和containsAll分别是判断是否包含一个蒜素,是否包含一个Collection集合。

remove 、removeAll、retainAll 分别是删除集合中的一个元素、删除与Collection集合中相等的元素、保留与集合Collection集合中元素相等的元素。

为什么把这几个放在一起呢?

这里牵涉了equals方法;

也就是说contains是如何判断是否包含的呢,该方法会将调用传入对象的equals方法逐一与集合中的元素作比较看是否相等。而containsAll方法则会放每个元素去调用contains方法。

同理remove为什么知道需要删除那个元素,也是会去调用equals方法逐一的与集合中的元素对比。而removeAll和retainAll会让传入的集合元素逐一调用remove方法,只不过前者是删除相同的,后者是保留相同的。

 

(关于Java集合的部分,我会全部分享在《java基础集合框架》的分类中)