Java Collection (i)-Container understanding

Source: Internet
Author: User
Tags addall comparable

Java Collection (i)-Container Understanding one: Collection Framework

Java Collection Framework Diagram


Java Collection simplified diagram


Two: Add a set of elements

In the toolkit in the Java Collection (Arrays,collections), as shown, adding a set of elements can take the following:

    1. Arrays.aslist (args) args: arrays or comma-delimited lists convert the results to a list object (it is still an array that cannot be returned directly to the underlying operation)
    2. Collection.addall (args) args: can only be Collection collection (not flexible)
    3. Collections.addall (arg0, arg1) arg0: Added to collection collection arg1: array or comma-delimited list (fast)
III: Printing of containers

Default printing uses the ToString method of the container. Collection print result for [,,,,,];map print result = {Key1=value,key2=value,}

Four: List

Arralist, Vectory, and LinkedList both inherit the list and the collection interface.
ArrayList: Good at random access, but inserting and removing elements in the middle of a list is slow.
LinkedList: Good at sequential access, the cost of inserting and deleting in the middle of a list is low. However, it is relatively slow in terms of random access.

Five: Set

Set saves elements that are not duplicates. The most important operation in set when looking for. The HashSet is optimized for quick find.

    1. HashSet: Used for quick lookups, using hash functions to provide the fastest query speed. The element that is deposited into the hashset must be defined hashcode ()
    2. TreeSet: Stores elements in a red-black tree, keeping the elements in a sorted state. The element must implement the comparable interface.
    3. Linkedhshset: With HashSet query speed, a linked list is used internally to maintain the insertion order of elements, preserving the elements in order of insertion. The Hashcode method must also be defined.
VI: MAP

The keyset () method in map is used to return a set that consists of all the keys in the map. (typically used to traverse the map collection)

    1. HASHMAP: Designed for quick access, based on a hash table. The cost of inserting and querying "key-value pairs" is fixed. You can set capacity and load factors through the constructor to adjust the performance of the container.
    2. Linkedhashmap: Speed is only a bit slower than hashmap, but it is faster for iterative access because the internal order is maintained using a linked list. When iterating through it, its order is the order of insertion or the least recently used order (LRU).
    3. TREEMAP: Based on the implementation of red and black trees. When you view key or key-value pairs, they are sorted (as determined by comparable or comparator). It is characterized by the fact that all the results are sorted. It is also the only map that returns to the moth tree.
    4. Weakhashmap: Weak key mapping, which allows you to release the object that the map points to.
    5. Concurrenthashmap: A thread-safe map that does not involve synchronous acceleration.
    6. Identityhashmap: Use = = instead of equals to compare hash mappings for "keys".
VII: Other Queue

A queue is a FIFO container. LinkedList provides a method to support the behavior of the queue, and it implements the queuing interface.

queue<integer> queue = new linkedlist<integer> (); queue.Offer  ()           //Tell an element to be inserted at the end of the queue, or return false//Return to Team header without removalQueue. Peek() ;//The queue is empty when NULL is returnedQueue. element() ;//Nosuchelementexception exception thrown when queue is empty//remove element and return to team headerQueue. Poll() ;//The queue is empty when NULL is returnedQueue. Remove() ;//Nosuchelementexception exception thrown when queue is empty
Priorityqueue

Priority queue declares that the next pop-up element is the most desired element (highest priority)

PriortyQueue<Integer> pq = new PriortyQueue<Integer>();

When you insert an object using the Offer () method on the Priortyqueue queue, the object is sorted in the queue. The default sort will use the natural order of the objects in the queue, but this order can be repaired by comparator. Priortyqueue also guarantees that when you invoke the Peek (), poll (), and remove () methods, the obtained element is the highest priority in the queue.

Stack

Stack: Refers to a last-in, first-out container. LinkedList has a way to implement all the functions of the stack, so you can use the LinkedList as a stack directly.

Eight: iterators (Iterator)

An iterator is an object whose work is to traverse and select the objects in the sequence. the cost of creating it is small, applicable to set and list sets (the Foreach syntax is also applicable to collection). Iterators in Java (Iterator) can only be moved in one direction and can only be used to:

    1. Using iterator () requires the container to return a iterator. Iterator will be ready to return the first element of a sequence.
    2. Use Next () to get the next element of the container
    3. Use Hasnext () to check if there are elements in the sequence
    4. Use Remove () to delete the newly returned element of the iterator (the next () method must be called before the Remove () method is called)
List<String> list = new ArrayList<String>();Iterator<String>  it = list.iterator();while(it.hasNext()){String s =it.next();System.out.println("s :"+s);}
Listiterator

Listiterator is a subtype of iterator and can only be used for access to various list classes.
Characteristics:

    1. Can move in both directions
    2. You can also produce an index relative to the previous and subsequent elements of the current position that the iterator points to in the list
    3. And you can use the set () method to replace the last element that it accesses
list<string> list = new arraylist<string> ();iterator<string> it = list. listiterator() ;While  (It.hasnext () ) {String s =it. next() ; System. out. println("s:"+s); it. Set("for substitution") ;}

Java Collection (i)-Container understanding

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.