Java basics-collection interface (1), javacollection
1. Collection Interface DefinitionPublic interfaceCollection <E> extends iterable <E> from the interface definition, we can find that this interface uses the generic definition, and the specific operation type must be specified during the operation. This ensures the security of the class set operation and avoids ClassCastException. Collection is the most basic Collection interface. A Collection represents a group of objects, namely, Elements of the Collection ). Classes provided by Java JDK are "sub-interfaces" that inherit from Collection, such as List and Set. All classes that implement the Collection interface must provide two standard constructors: 1. A constructor without parameters is used to create an empty Collection. 2. A constructor with the Collection parameter is used to create a new Collection. The Collection of this row has the same elements as the traditional Collection. This constructor allows you to copy a Collection. Review: Array
1 int [] x = new int [5]; // declare an array with a length of 5. Each element is of the int type 2 Student [] list = new Student [5]; // declare an array with a length of 5. Each element is of the Student type.
2. Common Collection methods
Boolean add (E) // note its parameter type boolean addAll (Collection <? Extends E> c) add all elements in the specified collection to this collection. boolean remove (Object o) boolean contains (Object o) // judge whether the specified Element in the collection exists boolean containsAll () // if the collection contains all the elements in the specified collection, true is returned. Boolean removeAll (Collection <?> C) boolean retainAll (Collection <?> C) keep only the elements in this collection that are also included in the specified collection (optional operation) (intersection) clear () size () toArray () boolean isEmpty () iterator <E> iterator () returns the Iterator that iterates on the elements of this collection.
Ii. List and set methods
The List Implementation classes include: List, Vector, and ArrayList. Note: List lists are inherited from collections and can be accessed in the order of indexes and indexed collections. With the list function, the element order is listed in the order of addition. The Collection, List, Set, and Map operations here are interfaces, not specific class implementations. List list = new ArrayList (); this is a frequently used statement to create a new List. Here List is an interface, and ArrayList is a specific class.
ListThe List interface extends Collection and declares the features of class sets that store a series of elements. With a zero-based submark, elements can be inserted and accessed through their positions in the list. A list can contain duplicate elements. In addition to the methods defined by Collection, List also defines some of its own methods. Note that when the class set cannot be modified, when one object is incompatible with the other, for example, when an attempt is made to add an incompatible object to a class set, a ClassCastException occurs. For the add () and addAll () methods defined by Collection, add (int, Object) and addAll (int, Collection) methods are added to the List ). These methods Insert elements at the specified subscript. The semantics of add (Object) and addAll (Collection) defined by Collection are also changed by List, so that they can add elements at the end of the List. To obtain objects stored at a specified location, you can use the object subscript to call the get () method. To assign a value to an element in a class table, you can call the set () method to specify the subscript of the object to be changed. You can call indexOf () or lastIndexOf () to obtain the subscript of an object. By calling the subList () method, you can obtain a subList of the list that specifies the start subscript and the end submark. This method makes the list processing very convenient.
SetThe set interface defines a set. It extends Collection and describes the features of class sets that do not allow copying elements. Therefore, if you try to add the copied element to the set, the add () method returns false. It does not define any additional methods. Summary: features of the List Set 1) element ordering (accessible by index) 2) elements can be repeated and multiple null values can be stored in the Set. Features: 1) element unordered (cannot be accessed by index) 2) elements cannot be repeated, and only one null value code instance is allowed:
1/* special features of the List set 2*3 * public interface List <E> extends Collection <E> 4 * -- add (int index, E element) insert the specified element 5 * -- addAll (int index, Collection <? Extends E> c) 6 * -- E get (int index) // return the element at the specified position in the list. 7 * -- int indexOf (Object o) // returns the index of the specified element that appears for the first time in this list 8 * -- listIterator () 9 * -- e set (int index, E element) // use the specified element to replace 10 * -- void remove (int position) of the element at the specified position in the List to remove 11 * -- List <E> subList (int fromIndex, int toIndex) similar to substring () 12 */13 import java. util. arrayList; 14 15 class CollT {16 public static void main (String [] args) {17 ArrayList <String> list = new ArrayList <String> (); 18 list. add ("Java"); // The lower mark is 019 list. add ("Class"); // The lower mark is 120 list. add ("Array"); // The Name Of The parameter is 221 list. add ("Collection"); // The lower mark is 322 list. add ("List"); // The lower name is 423 list. add ("Set"); // The value is 524 System. out. println (list. subList (0, 3); // note that this parameter does not contain the 25} 26 of 3}