Collection of the Java collection

Source: Internet
Author: User

Collection schema Hierarchy relationships1.Set (cannot have duplicate elements)1.1HashSet1.1.1LinkedHashSet2.List (must maintain element-specific order)2.1ArrayList2.2Vector2.2.1Stack2.3LinkedList3.Queue (Keep a queue (FIFO) in order)3.1PriorityQueue3.2Deque (Interface3.2.1 Arraydeque3.2.2 LinkedList 1.1HashSetRelationship:Java.util.HashSet, Inherit abstractset<e>, implement Set<e>principle:HashSet uses a hash algorithm to store the elements in the collection, so it has good access and lookup performance. When an element is deposited into the HashSet collection, HashSet calls the object's Hashcode () method to get the Hashcode value of the object, and then determines where the object is stored in hashcode based on the HashSet value. Note:HashSet Set the criterion for determining the equality of two elements is that two objects are compared by the Equals () method and the return value of the Hashcode () method of two objects is equal 1.1.1 LinkedhashsetRelationship:Java.util.LinkedHashSet, inherit Hashset<e>, implement Set<e>principle:The Linkedhashset collection also determines where the element is stored, based on the hashcode value of the element. But unlike HashSet, it maintains the order of elements using a list of links, so that elements appear to be saved in the order in which they are inserted. Note:When iterating through the elements in the Linkedhashset collection, Linkedhashset will access the elements in the collection in the order in which they are added. Linkedhashset need to maintain the insertion order of elements, so performance is slightly lower than hashset performance, but when iterating over all the elements in the set (traversal) will have good performance (the list is good for traversal) 2.1 ArrayList Relationship:java.util.ArrayList, inherit Abstractlist<e>, implement List<e>principle:An array that can be dynamically grown; the capacity of the default array is 10Features:
    • Fast random access, poor insertion and removal performance (characteristics of the array);
  • supports NULL elements;
  • have order;
  • elements can be duplicated;
  • Thread is not secure;
  • ArrayList Expansion 1.5 times times
complexity of Time:
  • Get () directly read the first few subscripts, complexity O (1)
  • Add (E) adds elements, added directly at the back, Complexity O (1)
  • Add (index, E) adds an element, inserts after the first element, and the following elements need to be moved backwards, and the complexity O (n)
  • Remove () Removes the element, and the subsequent elements need to be moved one by one, and the complexity O (n)
2.2 Vector Relationship:java.util.Vector; inherit abstractlist<e> implement List<e>principle:
    • The bottom layer consists of an array that can grow
Features:
    • Synchronization classes, with synchronous locks before each method synchronized
    • Thread Safety
    • Lower efficiency than ArrayList
2.2.1 StackRelationship:java.util.Stack; inherit vector<e>principle:
    • Vector-based implementation, thread-safe
Features:
    • Advanced Post-out
2.3 LinkedListRelationship:java.util.LinkedList; inherit abstractsequentiallist, realize List<e> deque<e>principle:a list based on a linked list, which also implements the Deque interface, which can be used as a double-ended queue for LinkedList. You can also go to the stack to usecomplexity of Time:
  • Get () gets the first few elements, in turn traversal, complexity O (n)
  • Add (E) added to the end, Complexity O (1)
  • Add (index, E) after adding the first element, you need to find the first element, the direct pointer to the operation, the complexity O (n)
  • Remove () delete element, direct pointer to operation, complexity O (1)
3.1 PriorityqueueRelationship:java.util.PriorityQueue; inherit abstractqueue<e>;principle:the logical structure of priorityqueue is a completely binary tree, and the storage structure is actually an array. Features:Priorityqueue is not a standard queue implementation, priorityqueue the order in which the queue elements are saved is not in the order in which they are queued, but rather by the size of the queue elements. 3.2 Deque (interface)Relationship:java.util.Deque; inherit queue<e>Features:The Deque interface represents a "double-ended queue" where the two-terminal queue can add and remove elements from both ends, so that the Deque implementation class can be used as a queue or as a stack 3.2.1 ArraydequeRelationship:java.util.ArrayDeque; inherit abstractcollection; realize deque<e>principle:Array-based implementationsFeatures:
    • Default capacity is 16
3.2.2 LinkedListRelationship:java.util.LinkedList; inherit abstractsequentiallist, realize List<e> deque<e>

Collection of the Java collection

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.