Detailed Java Collection interfaces

Collection interface is a List, parent interface Set and Queue interfaces, it is not normally used directly. Collection interface defines the general method may be implemented on a set of basic operations by these methods. A method for operating a Set both defined collection may also be used to operate a set of List and Queue.

Collection interface common method

Method name Explanation
boolean add(E e) Adding an element to the collection, a collection of objects is added if the operation is changed, it returns true. E is a type of data element
boolean addAll(Collection c) Add all of the elements in the set of c to the collection, a collection of objects is added if the operation is changed, it returns true.
void clear() Clear all the elements in the collection, the collection length becomes 0.
boolean contains(Object o) Determining whether there is a specified set of elements
boolean containsAll(Collection c) Determining whether the collection contains all the elements of the set of c
boolean isEmpty() Determining whether the set is empty
Iteratoriterator() It returns an Iterator object, traversing a collection of elements
boolean remove(Object o) When deleting a specified element from the collection, when the set contains one or more elements O, which removes only the first matching element, the method returns true.
boolean removeAll(Collection c) Delete all the elements appear in the collection c from the set (equivalent to calling the method of collection minus collection c). If the operation of changing the set of calling the method, the method returns true.
boolean retainAll(Collection c) Removing elements c is not included in the collection from the collection (equivalent to calling the method of collection and the collection becomes the set intersection c), if the operation changes the set called the method, the method returns true.
int size() Returns the number of elements in the collection
Object[] toArray() The set is converted to an array of all the set elements into the corresponding array elements.

note: Collections like container, the container function in real life, that is, add objects, delete objects, empty the container and determine whether the container it is blank, collection classes for these functions provide a corresponding method.

The method used to add to the collection Collection interface. code show as below:

public static void main(String[] args) {
    ArrayList list1 = new ArrayList(); // 创建集合 list1
    ArrayList list2 = new ArrayList(); // 创建集合 list2
    list1.add("one"); // 向 list1 添加一个元素
    list1.add("two"); // 向 list1 添加一个元素
    list2.addAll(list1); // 将 list1 的所有元素添加到 list2
    list2.add("three"); // 向 list2 添加一个元素
    System.out.println("list2 集合中的元素如下:");
    Iterator it1 = list2.iterator();
    while (it1.hasNext()) {
        System.out.print(it1.next() + "、");
    }
}

Collection Because the interface is not instantiate it, so the method using the above code ArrayList implementation class to call the Collection Collection interface. add () method to add an element Collection, whereas the addAll call () method may be all of the elements are added to another Collection Collection.

Code creates two sets list1 and list2, and then call the add () method adds two elements to list1, and then call addAll () method to add these two elements in list2. Next ED list2 added in an element, all the elements of the final output list2 set, the following results:

list2 集合中的元素如下:
one、two、three、

Application demos Collection set size (), remove () and removeAll () method. code show as below:

public static void main(String[] args) {
    ArrayList list1 = new ArrayList(); // 创建集合 list1
    ArrayList list2 = new ArrayList(); // 创建集合 list2
    list1.add("one");
    list1.add("two");
    list1.add("three");
    System.out.println("list1 集合中的元素数量:" + list1.size()); // 输出list1中的元素数量
    list2.add("two");
    list2.add("four");
    list2.add("six");
    System.out.println("list2 集合中的元素数量:" + list2.size()); // 输出list2中的元素数量
    list2.remove(2); // 删除第 3 个元素
    System.out.println("\nremoveAll() 方法之后 list2 集合中的元素数量:" + list2.size());
    System.out.println("list2 集合中的元素如下:");
    Iterator it1 = list2.iterator();
    while (it1.hasNext()) {
        System.out.print(it1.next() + "、");
    }
    list1.removeAll(list2);
    System.out.println("\nremoveAll() 方法之后 list1 集合中的元素数量:" + list1.size());
    System.out.println("list1 集合中的元素如下:");
    Iterator it2 = list1.iterator();
    while (it2.hasNext()) {
        System.out.print(it2.next() + "、");
    }
}

list2 call to remove the rest of the collection and four after two (2) method removes the first three elements. list1.removeAll (list2) statement list1 list1 and list2 in the same element is deleted, it is removed from the two elements. Final output results are as follows:

list1 集合中的元素数量:3
list2 集合中的元素数量:3

removeAll() 方法之后 list2 集合中的元素数量:2
list2 集合中的元素如下:
two、four、

removeAll() 方法之后 list1 集合中的元素数量:2
list1 集合中的元素如下:
one、three、

note: In contrast to the effect of removeAll retainAll () method () method, i.e., retain the same two sets of elements, all of the other deleted.

Published 457 original articles · won praise 94 · views 10000 +

Guess you like

Origin blog.csdn.net/weixin_45743799/article/details/104714427