Collection Container structure diagram:
The purpose of the Java Container Class library is to save objects, divided into two concepts, collection and maps. Collection saves a single element, while map saves the associated key-value pair.
Basic methods in collection :
int size (): Gets the number of elements in the collection
Boolean Add (E): Add an element to the collection
Boolean remove (Object O): Deletes the given element
Boolean IsEmpty (): Determines whether the collection contains no elements
void Clear (): Clears all elements in the collection
Boolean contains (Object O): Whether the collection contains the given element
Boolean AddAll (Collection C): Adds all elements from the given collection to the current collection
Boolean RemoveAll (Collection c) deletes the same element in the current collection as the given set
The list list interface adds a number of methods based on the collection, allowing you to insert and remove elements in the middle of the list.
Boolean Add (int index, E Element)
Boolean AddAll (Index, collection)
Boolean remove (int index)
Set (int index, E Element)
Get (int index)
sublist (int fromIndex, int toindex)
IndexOf ()
LastIndexof ()
Listiterator ()
When using Contains,indexof,remove, the Equals () method is used, and for different classes, the definition of equals is different and should be used with care.
-
ArrayList is suitable for random access, but inserting and deleting elements is slow
-
LinkedList Low-cost insertions and deletions provide optimized sequential access, but are slower in terms of random access.
In addition to implementing the Basic list interface, you have added methods that can be used as stacks, queues, or double-ended queues, some of which have only different names, and some are not very different, just to be more applicable in a specific context. If the queue interface narrows the access to the LinkedList method, the parameter type in the method, if it is a queue, can only access the methods defined by the queue interface and not directly access the LinkedList non-queue method.
GetFirst ()
Element ()
Peek ()
is the first element of the returned list, GetFirst and element throw an exception when the list is empty, and peek returns NULL.
Remove ()
Removefirst ()
Poll ()
Removes and returns the header of the list, and when the list is empty, GetFirst and element throw an exception, and peek returns NULL.
AddFirst ()
Add ()
AddLast ()
Offer ()
Inserting an element into the end of a list
Removelast ()
Remove and return the last element of the list
Stack stack: You can use LinkedList as a stack instead of using an obsolete stack
linkedlist<t> stack = new linkedlist<t> ()
Queue queues: LinkedList implements the queue interface, so linkedlist up to queue implementation
queue<t> queue = new linkedlist<t> ()
set does not save duplicate elements, and collection is exactly the same interface.
TreeSet storing elements in a red-black tree structure
HashSet is using a hash function
Map
Put ()
Get ()
KeySet () returns the set of its key
VALUES () returns the value of the collection
EntrySet () returns the set of its key-value pairs
Iterator iterators
The purpose of an iterator is to traverse and select the objects in the sequence, and the client programmer does not have to know the underlying structure of the sequence.
Collections.iterator () gets an iterator
Iterator.hasnext ()
Iterator.next ()
Iterator.remove () must call the next method before calling this method, which means removing the last element.
Listiterator is a subclass of iterator that can be used only for access to the list class and move in both directions.
List.listiterator () gets an iterator
Iterator.hasnext ()
Iterator.next ()
Iterator.hasprevious ()
Iterator.previous ()
Iterator.remove ()
Iterator.set () replaces the last element accessed
Iterator.nextindex () returns the index of the last element in the current position that the iterator points to in the list
Iterator.previous () returns the index of the previous element in the current position that the iterator points to in the list
Java Collection Learning Notes