Java Collection framework: Java Collection framework

Source: Internet
Author: User

Java Collection framework: Java Collection framework

Structure of the Collection framework:

I. Collection Structure Framework:


Ii. Set, List, Queue, and Map

  • Set: the derivative interface of collection. Repeated elements -- equal () are not allowed. Only the iterator is used to retrieve elements.
    • HashSet: the query and access performance by Hash algorithm is good; the order is not fixed; non-synchronization, thread is not safe; the element can be null. (Hash is used to quickly find the storage location of an element based on its value)
    • LinkedHashSet: A sub-type of Hashset. It also determines the storage location based on hashcode. However, it is maintained using a linked list so that the element sequence is consistent with the addition sequence. Others are slower than HashSet, but faster than HashSet;
    • TreeSet: implemented using the SortedSet interface and stored in a red/black tree data structure. Make sure that the elements are sorted;
      • 1. Natural sorting: Call the Comparable interface Implementation Method to compare the size (the Comparable interface must be implemented );
      • 2. Custom sorting: Specify the ascending or descending order. Provides a Comparator object when creating a TreeSet;
    • EnumSet: the element must be of the specified enumeration type, which is ordered. It is saved in bit vector form, compact and efficient, and has high operation efficiency. It is not allowed to add null. It has the best performance.
* The HashSet and TreeSet classes are recommended to be put into an immutable class. The performance of HashSet is always better than that of TreeSet (addition and query operations), which is not thread-safe!
  • List: The derived interface of collection, allowing repeated elements. ListIterator (), returns the Listiterator object;
    • ArrayList: implements the List Interface Based on arrays, and threads are not secure.
    • Vector: thread security, with Stack subclasses,
    • Shortlist: implements the List Interface Based on the linked List, and inserts and deletes the interface quickly. It implements the Deque interface and can be used as a stack!
    • Fixed-length List: Array. asList ("", ""). It can only be traversed and cannot add or delete elements.
  • Queue: Queue, first-in-first-out;
    • PriorityQueue: first sort the queue by size, and retrieve the minimum element by queue. The Comparable interface must be implemented for natural sorting elements, and the elements belong to the same class. A Comparator object is input for creating a queue for custom sorting, and the Comparable interface is not required for elements.
    • Deque interface: Sub-interface of dual Queue and Queue. Can be used as a stack. Pop and push methods are available;
    • ArrayDeque: Deque implementation class, which can be used as a stack;
    • Inclulist: implements the Deque interface and List interface, which can be used as a dual-end queue and stack.
  • Map: key-value; key must be unique!
    • Hashtable: An ancient Map implementation class. Thread security. Null is not allowed as key and value;
    • HashMap: null can be used as the key and value. The above two objects must implement the hashCode () and equals () methods;
    • LinkedHashMap: a subclass of HashMap. It maintains the key Order (key) of key-value pairs using a two-way linked list and maintains the insertion order;
    • SortedMap interface --> TreeMap implementation class: red/black tree data structure;
    • WeakHashMap implementation class: the key of HashMap retains the strong reference of the actual object, and the key of WeakHashMap only retains the weak reference to the actual object.
    • IdentityHashMap implementation class: key1 = key2, the two keys are exactly the same.
    • EnumMap implementation class: All keys must be the enumerated values of a single enumeration class.
* Using Properties to read and write property files, the key-value pair; 3. Collection and CollectionsCollection are the top-level interfaces of the collection framework. See figure 1. Collections is a tool class for Collection operations, provides static methods (search, sort, reverse, and lock). The main operations are as follows:

1. static <T> int binarySearch (List <? Extends Comparable <? Super T> list, T key)
This method is used to find the location where T objects match elements in the List. It is required that all the items in the List must be T objects, and the T object must implement the Comparable interface. If the search succeeds, the position of the returned object in the List is returned. Otherwise, a negative number is returned. Before executing this method, sort the elements in the List object. This method also has an overload method:
Static <T> int binarySearch (List <? Extends T> list, T key, Comparator <? Super T> c)
This method is also used to find the position of the T object in the List. The List set must be all telements, but the Comparable interface must be implemented instead of the T object. Instead, a comparator is required.
2. sort (List <T> list)
Sort the elements in the List by nature. The Comparator interface must be implemented to sort data in a user-defined manner. Sort (List <T> list, Comparator <? Super T> c) sorts the specified list according to the sequence generated by the specified comparator.
3. swap (List <?> List, int I, int j) Swap elements at the specified position in the specified list.
4. reverse (List <?> List) reverses the order of elements in the specified list.


Iv. Reference: Crazy java handouts (many examples are mainly used for practical purposes) Java: A Collection and Collection interface framework diagram (some references have been found, and some of them are messy, I still like this clear Architecture)

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.