The collection interface is the parent interface of the list, Set, and queue interfaces
The collection interface defines the methods that can be used to manipulate list, set, and queue--adding and removing changes
List interface and its implementation class--arraylist
A list is a set of elements that are ordered and can be duplicated, called sequences.
The list can control exactly where each element is inserted, or delete a location element
List has add () Insert method and get () Get method
arraylist--array sequence, which is an important implementation class of List
The bottom of the ArrayList is implemented by an array, which is the origin of its name.
List interface
The elements in the list are ordered and can be repeated
Add: Arraylist.add (object), added by default at the end of the list, you can add Arraylist.add (index position, object) at the specified location, which can be removed with the Get () method
Arraylist.addall (arrays.aslist (Array object)) method can add multiple objects at once, Arrays.aslist () is the conversion of an array into a list object
Arraylist.get (int index) It is important to note that an element inserted with a DD () or AddAll () method will always be of type object, and the Get () method will also be the object type, which is the element to take out. To do the corresponding type strong turn.
Type Object = (type) arraylist.get (index position);//The object is stored in the set As Object type, which is required for type conversion
Delete: Arraylist.remove (object or object's index position in list)
Modified: Arraylist.set (index position, modify content)
The list length can be obtained by the Arraylist.size () method
You can iterate through the list by using foreach (element type element variable: Traversing object name (that is, array name)) method, get method, iterator iterator method
In a foreach method, the element type is fixed to object, because the type of the element is ignored when it is placed in the collection.
In the iterator method, use the Hasnext () method to determine if the list has elements, and then return True. Iterators do not store things, depend on other structures exist
Generic type
In development, you can create a generic to specify the type of the object. In the collection, you cannot add an object that has an unexpected type of generic rule.
Generics can also add subtypes of the specified type in addition to the specified type.
When you define a subtype, you add an argument-free constructor to the parent type, otherwise an error occurs because the constructor method of the child type is called by default, and the non-parametric construction method of the parent class is called (Super ();).
You cannot use the base data type in a generic collection, you want to use the wrapper class of the base data type, that is, Integer,long,boolean. or for reference type list<course> Course
Set interface
Collection Interface-set interface-Implementation class HashSet
A set is a set of elements that are "unordered and non-repeatable", called a set
Second, hashset--hash set, is the important implementation class of Set
Note: Compare with List
· Set traversal can only be done with foreach and iterator, not with the Get () method (because it is unordered), and no Set () method
· A duplicate object is added to the set, and only one is preserved, and the one that is added for the first time is reserved.
Null objects can be added in set, but not in general development.
Usage of collection interfaces in Java