Java Collections Framework ---- Collection.list.

The origin and collection inheritance hierarchy Figure 1. collection

To reflect things are in the form of objects, so in order to facilitate the operation of multiple objects, Java provides a collection class object-oriented language. Java frame set includes two types of containers, one is a collection (Collection), a storage element in the set, the other is a view (the Map), stores key / value mapping.
Here Insert Picture Description

2. The difference between arrays and collections

(1): the difference between the length:
length of the array is fixed and the set of variable length
(2): the difference between the stored data types:
an array of basic data types may be stored, may be stored reference data type; and can store a collection of reference data types
(3): content difference:
the array can store the same data types of elements can store a collection of different types of elements

3. Collection features a collection Overview

a: add functionality
boolean add (Object obj): adding an element
boolean addAll (Collection c): adding a set of elements (To add a collection to another collection of all elements)
B: delete function
void clear (): Shift in addition to all the elements of
boolean remove (Object o): removes an element
boolean removeAll (collection c): removing the elements of a collection of (removal of more than one return is true) elements removed is the intersection of the two sets of elements
if there is no intersection remove the failed element returns to false
C: determining function
boolean contains (Object o): determines whether the collection contains the specified element
boolean containsAll (collection c): determining whether the collection contains the specified elements of the collection (set this set contains all other comprising only elements considered returns true)
, such as: l, 2,3 containsAll 12 is containsAll = 2,3,4 = l, 2,3 to false to true
Boolean isEmpty (): determines whether the set is empty
d: acquisition function
Iterator iterator ( ) (key)
E: function length
int size (): the number of elements
is inscribed: have an array length () method does not have a string length () method does not have a set of lengt?? ? h () method does
f: the intersection of function
// For example: A set of set B taking an intersection, the intersection acquired A collection element. Boolean value returned indicates whether the set A is changed
boolean retainAll (Collection c): Get the intersection of two sets of elements (intersection of: the element has two sets)
G: to set into an array
Object [] toArray ()

4.List Interface Overview and Features

Overview: Interface List is an ordered Collection, use this interface to precisely control the inserted location of each element, the element can be accessed by an index List (List elements in position, similar to the array index), the index element is a 0, and allows the same elements.
Features: Ordered elements, and each element there is an index of the element can be repeated.

The set of unique features List Overview

void add (int index, E element ): add an element at the specified index
E remove (int index): Removes the specified index of the element that is returned is to remove the element
E get (int index): Gets the element at the specified index
E set (int index, E element ): change the specified element element index returned but was replaced

6. ListIterator unique features: ListIterator method inherited from Iterator Iterator can use

ListIterator unique features:
boolean hasPrevious (): if the previous element exists
E previous (): returns the first element of the list of
more than two ways to achieve reverse traversal but pay attention to be completed before the first reverse traversal carried forward traverse this pointer to move to the last
if the direct reverse traversal no effect because the default location of the pointer in front of him there is no front element

7. The characteristics of the three subclasses of List

ArrayList:
the underlying data structure is an array, query fast, slow additions and deletions.
Thread-safe, high efficiency.
Vector:
the underlying data structure is an array, query fast, slow additions and deletions.
Thread-safe, low efficiency.
LinkedList:
the underlying data structure is the list, slow queries, additions and deletions fast.
Thread-safe, high efficiency.

8. Data Structure stacks and queues

A: Architectural Overview of common data structures and
data structure is actually a format for storing data
classification: stacks, queues, arrays, linked lists, trees, hash tables
B: Stack features: advanced after
C: queue: FIFO

9. linked list arrays of data structures

A: Array Features: Query fast, slow additions
B: Features list: Query slow, fast additions and deletions

10. ArrayList

ArrayList array based implementation, the queue is a dynamic array. But it is not the same array in Java, its capacity grows automatically, similar to the C language dynamic application memory, dynamic growth in memory!
a.ArrayList store string and ergodic:
void forEach (<? Super E> Consumer Action) Iterable each element performs a specific action until all elements have been processed or operation throws an exception
b integrated AbstractList, AbstractList has inherited. AbstractCollection implements List interface, which is an array queue, provided relevant to add, delete, modify, traversal function!
c.
implements RandomAccess interface that provides random access, in fact, for quick access by index number.
Implements Cloneable interface, i.e. covering function clone (), can be cloned.
Implements the Serializable interface, support for serialization, it means that the sequence of transmission through ArrayList.

11.ArrayList的API

// Collection中定义的API
boolean             add(E object)
boolean             addAll(Collection<? extends E> collection)
void                clear()
boolean             contains(Object object)
boolean             containsAll(Collection<?> collection)
boolean             equals(Object object)
int                 hashCode()
boolean             isEmpty()
Iterator<E>         iterator()
boolean             remove(Object object)
boolean             removeAll(Collection<?> collection)
boolean             retainAll(Collection<?> collection)
int                 size()
<T> T[]             toArray(T[] array)
Object[]            toArray()
// AbstractCollection中定义的API
void                add(int location, E object)
boolean             addAll(int location, Collection<? extends E> collection)
E                   get(int location)
int                 indexOf(Object object)
int                 lastIndexOf(Object object)
ListIterator<E>     listIterator(int location)
ListIterator<E>     listIterator()
E                   remove(int location)
E                   set(int location, E object)
List<E>             subList(int start, int end)
// ArrayList新增的API
Object               clone()
void                 ensureCapacity(int minimumCapacity)
void                 trimToSize()
void                 removeRange(int fromIndex, int toIndex)

12. ArrayList supports three kinds of traversal

  • [1] to traverse through the iterator. That is, by Iterator to traverse
  • [2] a second, random access, by the index value to traverse.
    Since ArrayList implements RandomAccess interface, which supports random access to elements by index value.
  • [3] for loop iterates

13.toArray () abnormal

Call toArray () function will throw "java.lang.ClassCastException" abnormal, but call toArray (T [] contents) can return to normal T [].
toArray () will throw an exception because toArray () returns the Object [] array, the Object [] is converted into another type (e.g., such as the Object [] is converted to the Integer []) is thrown "java. lang.ClassCastException "abnormal, because Java does not support downcast. Specific reference may be introduced toArray source ArrayList.java front portion ().
Solution to this problem is to call T [] toArray (T [] contents), instead of Object [] toArray ().

14. LinkedList unique features

A: LinkedList Class Overview: List linked list implementation of the interface, this implementation is not synchronized
B: LinkedList class-specific features
public void addFirst (E e) and addLast (E E)
public E getFirst () and getLast ()
public E removeFirst ( ) and public E removeLast ()

15. The difference of ArrayList and LinkedList

ArrayList is an implementation of the List interface, it is to use an array to achieve.
LinkedList is an implementation of the List interface, which is implemented using a linked list.
ArrayList traversal and find elements faster. LinkedList traversal and find elements slower.
ArrayList add, delete elements slower. LinkedList add, delete elements faster.

Published 39 original articles · won praise 1 · views 569

Guess you like

Origin blog.csdn.net/love_to_share/article/details/102924761
Recommended