Java Java Collection Framework notes

Source: Internet
Author: User

Today in the QQ group to communicate with others, the wrong to the list interface said to be the father of the ArrayList, when the face is red, no, must kneel back to review Java, so the Java book out, the collection of the framework of the careful look over, in Evernote wrote a note, So move to the blog to share with you. Here's the thing.
The Java Collection Framework supports the following two types of containers:
    • One is to store a collection of elements, referred to as Collections (collection)
    • The other is to store the key-value pairs, which become the graph (map)

    • In addition: all instance classes within the Java Collection Framework implement the clonable and serializable interfaces, so their instances are replicable and serializable.


Collection :(All the general features of the collection are defined in the interface, and his implementation is provided in a concrete class) Java has three main types of collections: Rule set (set), Linear table (list), queue.
    • An instance of set is to store a set of elements that cannot be duplicated.
    • An instance of the list is to store an ordered collection of elements.
    • The instance of the queue is intended to store objects that are processed in FIFO mode.

The various implementation classes under the collection:
HashSetclass implements the set interface, which is the subclass of set.
Several methods:
  • Contains () method: Whether this element exists
  • Size method: Gets the number of elements.
  • Add method: Adds an element.
  • Remove method: Deletes an element.
  • Get method: There is no subscript for this method because there are no subscripts.
  • Clear method: Empty Set Collection
  • IsEmpty method: Determines whether the collection is empty.
  • Iterator method: Gets the iterator for this collection, which is used to traverse the entire collection. Iterator Iterator = Set.iterator ();
  • Hasnext method
  • Next method
Note: The elements in the HashSet collection are not allowed to be duplicated, plus a single original element, or just one.
Linkedhashsetclass uses a list implementation to extend the HashSet class, which supports elements within the rule setSort(in the order they were inserted)Note:If you do not need to maintain the order in which elements are inserted, you should use HashSet, which is more efficient than linkedhashset.
TreeSetclass implements the specific class Navigableset class of SortedSet, which can be added to a treeset as long as the objects can be compared. Several methods:
    • First () method: Returns the element in TreeSet.
    • Last () Method: Returns the final element in the TreeSet.
    • HeadSet (Object obj) method: Returns all the elements in front of the Obj object in TreeSet.
    • Tailset (Object obj) method: Returns the Obj object in TreeSet and all subsequent elements.
ListInterface extends the collection interface to define aAllow duplicatesAn ordered collection of several methods:
  • Add (Index, Element) method: Used to specify that an element is inserted at the subscript.
  • AddAll (Index, collection) method: Inserts a collection in the specified subscript.
  • Remove (Index) method: Removes the element that specifies the subscript.
  • IndexOf (Element) method: Used to get the index of the first occurrence of the specified element in a linear table.
  • LastIndexOf (Element) method: Used to obtain the last subscript of the specified element in a linear table.
  • Sublist (FromIndex, Toindex) method: Used to get a sub-linear table.
  • The Listiterator ()/listiterator (StartIndex) method is used to get the iterator object.
  • Add (Element) method: Inserts the specified element into the linear table.
The list is implemented in two ways:
    • Array linear table class ArrayList: An array is used to store elements, and this array is created dynamically. If there is not enough space, create a larger array and copy the original array in.
    • Linked List class LinkedList: stores elements in a linked list.

CollectionsThe class contains static methods for manipulating linear tables and collections: (Note that the collections here is a class, to be strictly differentiated from the collection Interface) several methods:
  • Sort (list:list) method: Sort the list of specified lists
  • Sort (list:list, C:comparator) method: Use Comparator to sort the specified list.
  • BinarySearch (List:list, Key:object): Int method: Use binary lookup to find key
  • BinarySearch (List:list, Key:object, C Comparator): Int method: Use comparator and binary lookup to find key
  • Renverse (list:list) method: Reverse the list.
  • Reverseorder (List:list): Comparator method: Returns the comparer in reverse order.
  • Shuffle (list:list) method: Randomly disturbed
Performance comparison: Set is more efficient than list. Specific: HashSet>Linkedhashset>TreeSet>ArrayList>LinkedList.
vector class vectors and stacks
The vector class implements the list interface, in addition to the synchronization method for accessing and modifying vectors, Vectors and ArrayList are the same. The synchronization method is used to prevent data corruption when two or more threads are accessing a vector at the same time. The Stack class is a few ways to implement as an extension of the vector class:
    • Stack () Method: Creates an empty stack.
    • Empty (): Boolean method: Returns True if the stack is empty.
    • Peek (): E method: Returns the top element of the stack.
    • Pop (): E method: Returns and deletes the top element of the stack.
    • Push (O:e): E method: Adds a new element to the top of the stack.
    • Search (O:object): Int method: Returns the position of the specified element in the stack


queues and Priority queuesQueueThe interface extends java.util.Collection with additional insert extraction and validation operations. Several methods:
    • Offer (Element:e): Boolean method: Inserts an element in a relative class.
    • Poll (): E method: Gets and deletes the queue header and returns null if the queue is empty
    • Remove (): E method: Gets and removes the queue header and throws an exception if the queue is empty
    • Peek (): E method: Gets but does not delete the queue header and returns null if the queue is empty
    • Element (): E method: Gets but does not delete the queue header and throws an exception if the queue is empty
dual-ended queues deque and LinkedListLinkedListImplements the Deque interface, and deque extends the queue interface, so you can use LinkedList to create a queue.Dequesupports inserting and deleting elements at both ends. Deque is the short name of the double end queue, pronounced deck. Several methods:
    • AddFirst ():
    • Removefirst ():
    • AddLast ():
    • Removelast ():
    • GetFirst ():
    • GetLast ():
Priorityqueue implements a priority queue that, by default, uses comparable to sort the elements in their natural order, with the smallest values being given the highest priority, and therefore first removed from the queue.
Figure: Used to store key value pairs, key values can make arbitrary types, keys cannot be duplicated, each key corresponds to a value graph of three types: Hash chart (HASHMAP) chain Hash chart (Linkedhashmap) tree (TREEMAP). The map interface provides a way for the query to update the values of the collection and the key values of the collection, as follows:
  • Clear (): Void method: Deletes all entries in the diagram.
  • Containkey (Key:object): Boolean method: Returns True if the figure contains an entry corresponding to the specified key value.
  • Containsvalue (Value:object): Boolean method: Returns True if the graph maps one or more key values to a specific value
  • EntrySet (): Set<map.entry<k, v>> method: Returns a rule set containing an entry on the way
  • Get (Key:object): V method: Returns the value corresponding to the specified entry in the graph.
  • IsEmpty (): Boolean method: Returns True if the figure does not contain any entries
  • KeySet (): Set<k> method: Returns a rule set containing the key values in the graph
  • Put (Key:k, value:v): V method: Puts a map into the diagram.
  • Putall (m:map<? extends K,? extends V>): Void method: Add all entries from M to the diagram
  • Remove (Key:object): V method: Deletes the entry for the specified key value
  • Size (): Int method: Returns the number of entries
  • VALUES (): Collection<v> method: Returns a collection that is worthy of being included in the graph
Points:
    • Entry is an internal interface in the map interface
    • The SortedMap interface extends the map interface. He also has additional methods Firstkey () and Lasstkey () to return the lowest and highest value keys, and the Heapmap (Tokey) method returns the portion of the graph whose key value is less than Tokey, and Tailmap ( Fromkey) returns the portion of the graph with a key value greater than or equal to Fromkey.
    • HashMap is efficient for locating a value, inserting a map, and deleting a mapping.
    • The Linkedhashmap class uses a linked list implementation to extend the HashMap class, which supports the ordering of entries in the diagram. HashMap is not supported for sorting. However, in Linkedhashmap, elements can be sorted in the order in which they were inserted, or in the order they were last accessed, from the earliest to the latest.
    • TreeMap is very efficient when traversing the ordered key values.

Java Java Collection Framework notes

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.