Reproduced Java Containers & Generics: First, understanding containers

Source: Internet
Author: User

Containers are an important part of the Java language learning. Mason my feeling is just beginning very difficult to learn, but so you know it, contact more, also "logical" to understand. Java's container classes are mainly derived from two interfaces:collection and Map.

First, Collection vs collections

First, Collection and collections are two different concepts. The reason why they are put together is to make a better comparison. collection is the root interface in the container hierarchy. And collections is a class that provides some static methods for handling container classes.                              

The JDK does not provide a concrete implementation of the collection interface, but rather provides a more specific sub-interface (such as set and list) implementations .

What is the function of the collection interface? Existence is the truth.

The reason is that all of the container implementation classes (such as ArrayList implementation of the list interface, HashSet implementation of the set interface) provide two ' standard ' constructors to implement: 1, an argument-free construction Method (void) 2, a collection type a single-parameter construction method that creates a new collection with the same elements as its parameters, its implementation class, and so on. in fact : Because all common container classes conform to the collection interface, the second method of construction is to allow the container to replicate with each other.

Second, the class hierarchy structure of collection

The following figure is a hierarchy of classes on collection.

Set:

A collection that does not include duplicate elements, including mutable objects , is an unordered collection. Set does not contain elements that are full a.equals (b) to A and B, and have a maximum of one null. The interfaces for implementing set are: Enumset, HashSet, TreeSet, etc. Is the set JDK source UML diagram.

List:

An ordered collection (also called a sequence) in which elements can be duplicated . Specifically, lists generally allow e1.equals (E2) elements to E1 and E2, and if the list itself allows null elements, they usually allow multiple null elements. The realization list has: ArrayList, LinkedList, Vector, stack and so on. is a list of JDK source UML diagram.

Queue:

A queue is a double-ended queue that supports inserting and removing elements at both ends of the head and tail, mainly including: Arraydeque, Linkedblockingdeque, LinkedList. The other is the blocking queue , which will throw an exception when the queue is full and then insert the element, including Arrayblockqueue, Priorityblockingqueue, Linkedblockingqueue. Although the interface does not define a blocking method, the implementation class extends this interface. is the JDK source UML diagram for queue.

III. hierarchical structure of map classes

The following figure is the map's hierarchy chart

MAP:

is a collection of key-value pairs. In other words, a map cannot contain duplicate keys, with each key mapped to a value. This interface replaces the dictionary abstract class. The implementation map has: HashMap, TreeMap, HashTable, Properties, Enummap. is a map of the JDK source UML diagram.

Iv. Summary of the container interface

V. Code examples

Examples of Hashmap,hashset,linkedlist,arraylist,treemap,treeset are as follows:

Importjava.util.ArrayList;ImportJava.util.HashMap;ImportJava.util.HashSet;Importjava.util.LinkedList;Importjava.util.List;ImportJava.util.Map;ImportJava.util.Set;ImportJava.util.TreeMap;ImportJava.util.TreeSet; @SuppressWarnings ("Unchecked") Public classcollectionall{ Public Static voidMain (string[] args) {printlists ();                 Printsets ();    Printmaps (); }     Private Static voidprintlists () {List<String> A1 =NewArraylist<string>(); A1.add ("List"); A1.add ("Set"); A1.add ("Queue"); A1.add ("Map"); System.out.println ("ArrayList Elements:"); System.out.print ("\ T" + a1 + "\ n"); List<String> L1 =NewLinkedlist<string>(); L1.add ("List"); L1.add ("Set"); L1.add ("Queue"); L1.add ("Map"); System.out.println ("LinkedList Elements:"); System.out.print ("\ t" + L1 + "\ n"); } @SuppressWarnings ("Rawtypes")    Private Static voidprintsets () {Set H1=NewHashset<string>(); H1.add ("List"); H1.add ("Set"); H1.add ("Queue"); H1.add ("Map"); System.out.println ("HashSet Elements:"); System.out.print ("\ t" + h1 + "\ n"); Set T1=NewTreeset<string>(); T1.add ("List"); T1.add ("Set"); T1.add ("Queue"); T1.add ("Map"); System.out.println ("TreeSet Elements:"); System.out.print ("\ t" + t1 + "\ n"); }         Private Static voidprintmaps () {Map<string, string> h1 =NewHashmap<string, string>(); H1.put ("List", "ArrayList"); H1.put ("Set", "HashSet"); H1.put ("Queue", "Priorityqueue"); H1.put ("Map", "HashMap"); System.out.println ("HashMap Elements:"); System.out.print ("\ t" + h1 + "\ n"); Map<string, string> t1 =NewTreemap<string,string>(); T1.put ("List", "ArrayList"); T1.put ("Set", "HashSet"); T1.put ("Queue", "Priorityqueue"); T1.put ("Map", "HashMap"); System.out.println ("TreeMap Elements:"); System.out.print ("\ t" + t1 + "\ n"); }}

The console prints as follows:

ArrayList Elements:    [List, Set, Queue, Map] LinkedList Elements:    [List, set, Queue, Map]hashset Elements:    [Map, Queue, Set, list]treeset Elements:    [ List, Map, Queue, Set]hashmap Elements:    {Map=hashmap, Queue=priorityqueue, Set=hashset, list=ArrayList} TreeMap Elements:    {List=arraylist, Map=hashmap, Queue=priorityqueue, Set=hashset}
Vi. Summary

Similarities and differences.

Source: http://blog.csdn.net/softwave/article/details/4166598

Vector and ArrayList

1,vector is thread-synchronized, so it is thread-safe, and ArrayList is thread-asynchronous and unsafe. If the thread security factors are not taken into account, the ArrayList efficiency is generally higher.
2, if the number of elements in the collection is greater than the length of the current collection array, the vector growth rate is 100% of the current array length, while the ArrayList growth rate is 50% of the current array length. If you use data with larger data volumes in a collection, vector has some advantages.
3, if you look for a specified location of data, the vector and ArrayList use the same time, are 0 (1), this time using both vector and ArrayList can be. If moving a data at a specified location takes 0 (n-i) n for the total length, this should take into account the use of linklist, since it takes 0 (1) to move the data at a specified location, and the time to query the data at a specified location is 0 (i).

ArrayList and vectors use arrays to store data, which is larger than the actual stored data in order to add and insert elements, allowing direct ordinal index elements, but inserting data to design memory operations such as array element movement, so the index data is fast inserting data slowly, Vector because of the use of the Synchronized method (thread safety) so the performance is worse than the ArrayList, LinkedList using the two-way linked list to implement storage, indexed by ordinal data needs to be traversed forward or backward, but when inserting data only need to record the item's front and rear items, So insert several faster!

A ArrayList and LinkedList

1.ArrayList is the realization of the data structure based on dynamic array, LinkedList data structure based on linked list.
2. For random access get and set,arraylist feel better than LinkedList, because linkedlist to move the pointer.
3. Add and Remove,linedlist are the dominant for new and deleted operations because ArrayList is moving the data.
This point depends on the actual situation. If you insert or delete only a single piece of data, the ArrayList speed is better than LinkedList. But if bulk random insert deletes data, LinkedList speed is much better than ArrayList. Because ArrayList each insertion of data, you move the insertion point and all subsequent data.

HashMap and TreeMap

1, HashMap through the hashcode of its content to quickly find, and treemap all the elements are kept in a fixed order, if you need to get an ordered result you should use TREEMAP (the order of the elements in HashMap is not fixed). The order of the elements in the HashMap is not fixed.

2, HashMap through the hashcode of its content to quickly find, and treemap all the elements are kept in a fixed order, if you need to get an ordered result you should use TREEMAP (the order of the elements in HashMap is not fixed). The collection framework provides two general map implementations: HashMap and TreeMap (TreeMap implements the SortedMap interface).

3, inserting, deleting and locating elements in map, HashMap is the best choice. But if you want to traverse the key in natural order or in a custom order, TreeMap is better. The implementation of Hashcode () and Equals () is clearly defined by the key class that is required to be added using HashMap. There is no tuning option for this treemap because the tree is always in a balanced state.

Hashtable and HashMap

1, historical reason: Hashtable is based on the old dictionary class, HashMap is an implementation of the map interface introduced by Java 1.2.

2, synchronization: Hashtable is thread-safe, that is, synchronous, and HashMap is a line program is not secure, not synchronous.

3. Value: Only HashMap can let you use NULL as the key or value of an entry for a table.

Reprinted from: http://www.bysocket.com/?p=162

Reproduced Java Containers & Generics: First, understanding containers

Related Article

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.