Home  >  Article  >  Java  >  Detailed introduction to Colleciton in Java

Detailed introduction to Colleciton in Java

PHP中文网
PHP中文网Original
2017-06-20 16:33:541271browse

Overview:

1, Collection definition: inherits Iterable, has generic functions, and is the top parent interface of the Collection collection system.

2, Collection methods: add, addAll; clear, isEmpty, size; toArray (overloaded); iterator; contains, containsAll; remove, removeAll, retainAll;

First, let’s take a look at the definition of Collection

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

Second, let’s take a look at Colleciton own methods (excluding inherited methods).

#1, about add and addAll.

Both add elements to the collection (their respective subclasses will implement it specifically).

The former is to add a single element, and the latter is to add a subclass collection that implements Collection.

For example (I deliberately used different Collection subclasses in the example):

<br>
    @Testpublic 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.

These methods are relatively simple and crude, so I put them together, and I won’t show the code by the way.

clear clears all elements in the collection.

isEmpty determines whether there are any elements in the collection and returns true when it is empty.

size gets the number of elements in the collection.

3, about converting a collection into an array toArray.

ToArray overloaded method, one has no parameters, and a number needs to be passed in an existing array.

Let’s first talk about Object[] toArray() without parameters. It returns an Object array, so here comes the problem.

If you need String[] objects = (String[]) collection.toArray(); When doing this, it will throw a ClassCastException exception.

Then you may know what T[] toArray(T[] a) with parameters is for, see below.

The following code prints the following results, which means that array objects will be returned in any case.

When the length of the passed array is less than the size of the collection, a new array will be returned separately, and the passed array will not be filled with data.

When the passed array is equal to or greater than the combined size, the incoming array is filled and the array is returned.

Note: Therefore, generally we should use the second case of the method with parameters

---Print the situation when the given array is smaller than the set-----<br>strings: [null, null]<br>returnStrings: [escore, wym, cl]<br>strings==returnStrings: false<br>---Print the case when the given array is equal to the set-----<br>strings: [escore, wym, cl]<br>returnStrings: [escore, wym, cl]<br>strings==returnStrings: true<br>---Print the case when the given array is larger than the set--- --<br>strings: [escore, wym, cl, null, null]<br>returnStrings: [escore, wym, cl, null, null]<br>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()

About return We will not discuss the method of an iterator here. Please refer to the content excuse of Iterator.

5, contains, containsAll; remove, removeAll, retainAll

contains, and containsAll are respectively used to determine whether it contains an allicin , whether it contains a Collection collection.

remove, removeAll, and retainAll respectively delete an element in the collection, delete elements equal to the Collection collection, and retain elements equal to the elements in the Collection collection.

Why put these together?

The equals method is involved here;

That is to say, how does contains determine whether it is included? This method will compare the equals method of the incoming object with the elements in the collection one by one. are equal. The containsAll method will put each element to call the contains method.

Similarly, why remove knows which element needs to be deleted, and will also call the equals method to compare it with the elements in the collection one by one. RemoveAll and retainAll will cause the incoming collection elements to call the remove method one by one, except that the former deletes the same ones, and the latter retains the same ones.

(I will share all about Java collections in the category of "Java Basic Collection Framework")

The above is the detailed content of Detailed introduction to Colleciton in Java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn