Collection in Java (2) Collection interface

Collection in Java (2) Collection interface

        Collection is a highly encapsulated collection interface, inherited from Iterable interface, it provides the default method for all collections to implement. Because Iterable encapsulates Iterator iterator, so Collection can use Iterator iterator. Collection is used to represent a collection of single-value objects, these objects are also called elements. The Collection interface defines a series of abstract methods and regulates the operation standards for single-valued collections.

First, Collection subinterface or subclass

Collection has three sub-interfaces of Queue , List and Set , and an AbstractCollection abstract class. Since the Collection interface has no available implementation classes, you must use its subclasses or subinterfaces when using Collection.

Two, Collection commonly used methods

Three, Collection add and remove elements

 1 String str  = "hello world";
 2 Collection collection = new ArrayList();
 3 collection.add("hello");
 4         
 5 boolean isChange = collection.add(str);
 6 boolean isWas = collection.remove(str);
 7         
 8         
 9 Iterator ite = collection.iterator();
10          
11 while(ite.hasNext()) {
12     System.out.println(ite.next());
13 }

 

 

(1) Add elements

1. Use the add (E e) method to add elements to the Collection. If the structure of the Collection changes, that is, the element is successfully added, this method will return "true", otherwise it will return "false";

2. If the Collection refers to the List interface or a subclass of List, when the add method is called, if the element is already stored, then there are two of the element in the collection (because the List interface is orderly and repeatable);

3. If the Collection refers to the Set interface or Set subclass, when the add method is called, if the element is already stored, it will not be added repeatedly, ensuring that there is only one element in the collection (because the Set interface is unordered and cannot be repeated ).

The difference between add (E e) and addAll ( Collection <? extends E > c):

When the parameter passed to the method is a collection object,

1. The collection object itself added by the add method;

2. The addAll method adds all elements in the collection object, and the collection object itself will not be added.

(2) Remove elements

Use the remove (Object obj) method to remove the element in the Collection. If the element exists in the Collection, it returns "true" after the execution is completed, otherwise it returns "false".

Use the removeAll ( Collection <?> C) method to remove all elements in the Collection passed by the parameter. If some elements in the passed Collection do not exist in the target Collection, these elements will be ignored.

The retainAll ( Collection <?> c) ​​method is just the opposite of the removeAll ( Collection <?> c) ​​method. Instead of removing all the elements in the collection with the given parameters, but keep these elements and remove other elements.

Remember, these elements can only be retained if they already exist in the target Collection. Elements that exist in the parameter Collection but do not exist in the target Collection will not be added to the target Collection. They will be ignored.

Fourth, detect whether a Collection contains a certain element

There are two methods in the Collection interface to determine whether the Collection contains one or more elements. These two methods are: contains (Object o) and containsAll (Collection <?> C).

1. contains (Object o): If this Collection contains this element, the contains ()method will return true, otherwise, return "false".

2. containsAll (Collection <?> C): If this Collection contains all the elements in the parameter Collection, the containsAll ()method will return true, otherwise, return "false".

Five, the size of the Collection

The Collection interface can call the size () method to return the size of the Collection.

Six, Collection traversal

1. Since Collection is an Iterable interface, Iterable encapsulates Iterator iterator, all Collection can use iterator to traverse elements;

2. You can use the foreach loop to traverse the Collection elements.

Seven, AbstractCollection abstract class

AbstractCollection is a direct implementation class of the Collection interface. Subclasses under the Collection interface mostly inherit from AbstractCollection, such as the implementation class of List and the implementation class of Set. In addition to the iterator () and size () methods, it provides default implementations of some other interfaces, and other collection classes can inherit this class for reuse.

In addition to providing default implementations of some other interfaces, AbstractCollection also defines two abstract methods, which are left to subclasses to implement:

1、public abstract Iterator<E> iterator();

2、public abstract int size();

Subclasses must implement the above two abstract methods. In addition, AbstractCollection does not support the operation of adding elements by default. Calling add (E e) directly will report an UnsupportedOperationException error.

 

Guess you like

Origin www.cnblogs.com/lingq/p/12727832.html