A collection of
1 , a collection Overview
Why collections appear?
The existence of the collection is perfect for an array of functions
Arrays and collections are similar containers, What's the difference?
Although objects may be stored in an array, but the length is fixed; set length is variable. Array may be stored in the same data type, only the presence of a set of reference objects.
Collections of features:
Only for storing a set of objects, a set of variable length, a collection of different types of objects may be stored
2 , Collection Interface Overview
collection overview
Root interface Collection hierarchy. Collection represents a group of objects, which are also referred to as the collection element. Some collection allows duplicate elements, while others do not. Some collection is ordered, while others are unordered.
collection method interface member
boolean add (E e) // add an element, returns true if successful, otherwise returns false
boolean remove (Object o) // remove Removes the specified element, returns true if successful, false otherwise
void clear () // remove all elements
boolean contains (Object o) // contains the element contains is true, false otherwise
boolean isEmpty () // whether to return empty empty true, otherwise false
int size () // returns the set length.
boolean addAll (Collection c) // add all the elements
boolean removeAll (Collection c) // remove all elements
Whether boolean containsAll (Collection c) // set of parameters to be determined comprises
boolean retainAll (Collection c) // parameter other than the elements of the set removed
Object[] toArray()
To turn into a set of arrays, can be achieved through the collection of
Iterator iterator()
Iterator, dedicated set of traversal
3 , Iterator Interface
Iterator Interface Overview
To carry out collection iterator
It depends on the collection exists
Member method:
boolean hasNext () // determines whether there is a next element
E next () // returns the next element
4 , List Interface Overview
List Interface Overview:
An ordered collection (also known as a sequence). This user interface can be accurately controlled to the insertion position of each list element. Users can access elements based integer index (position in the list), and search for elements in the list.
And set different, list lists typically allow duplicate elements.
List Case
Storage string and ergodic
Store custom objects and traverse
List interface member method
void add (int index, E element) // added to the target position specified element
E remove (int index) // delete the specified element
E get (int index) // Gets the underlying elements specified
E set (int index, E element) // settings specify the subject of the next element
ListIterator listIterator () // obtain iterator to traverse
5 , ListIterator members of the interface method
boolean hasPrevious()
E previous()
6 , common data structures
After the stack first-out, last in, first out
FIFO queue, after depositing
An array of easy to find, convenient additions and deletions
List additions and deletions easy, convenient lookup
tree
Hash table
7 , ArrayList class overview and use
ArrayList class overview
Underlying data structure is an array, the query fast, slow additions
Thread-safe, high efficiency
ArrayList Case
Storage string and ergodic
Store custom objects and traverse
8 , the Vector class overview and use
Vector class overview
Underlying data structure is an array, the query fast, slow additions
Thread-safe, low efficiency
Vector class-specific features
public void addElement (E obj) add an element
public E elementAt (int index) Gets the specified element
public Enumeration elements() 遍历
Vector Case
Storage string and ergodic
Store custom objects and traverse
9 , LinkedList class overview and use
LinkedList class overview
The underlying data structure is the list, the query is slow, fast additions and deletions
Thread-safe, high efficiency
LinkedList class-specific features
public void addFirst(E e)及addLast(E e)
public E getFirst()及getLast()
public E removeFirst()及public E removeLast()
Exercises focus on collection, list, ArrayList, vector, LinkedList, there are three ways to traverse, iterators, for, enhanced for.
10 , Generics
Why do generics?
Early Object type can receive any type of object, but in actual use, there is a problem type conversion. This risk also exists, it provides a generic Java to solve this security problem.
Generic applications:
Active position generic classes, interfaces, methods,
Generic class:
The generic definition of the class to class called generic class
Format: public class name of the class <generic type, generic type, generic type ...>
Note: The generic class must be a reference type
Generic interface:
To define the generic interface
Format: public interface interface name <generics, generics>
Generic method:
The generic definition of the method to
Format: public <generic type> Method Name Return Type (generic type) {}
Generic most important role, the provisions of parameters and return type.
Senior generic (wildcard)
Generic wildcard <?>
Any type, if not explicitly, then that Object as well as any of the Java class
? extends E
Defining downwardly, E and its subclasses
? overcome
Upwardly defined, E and its parent