Java container & amp; generic: 1. Understand the container
Writer: BYSocket)
Weibo: BYSocket
Douban: BYSocket
Containers are an important part of Java language learning. I feel that it is difficult to learn it at the beginning, but when you get familiar with it and get in touch with it, you will know it as a matter of course. The Java container class is derived from two interfaces: Collection and Map.
1. Collection vs Collections
First, Collection and Collections are two different concepts. They are put together for better comparison. Collection is the Root Interface in the container hierarchy. Collections is a class that provides some static methods for processing containers.
JDK does not provide specific implementation of the Collection interface, but provides more specific sub-interfaces (such as Set and List.
What is the function of the Collection interface? Existence is the truth.
The reason is that the implementation classes of all containers (for example, ArrayList implements the List interface, and HashSet implements the Set Interface) provide two 'standard' constructor for implementation: 1. A construction method without parameters (void) 2. A construction method with single parameters of Collection type, used to create a Collection with the same parameters and Its Implementation class. In fact: because all common container classes follow the Collection interface, the second constructor is used to allow inter-container replication.
Ii. Collection class hierarchy
The following figure shows the hierarchy of Collection classes.
Set:
A Collection that does not contain repeated elements (including mutable objects) is an unordered Collection. Set does not contain all elements of a. equals (B) to a and B, and at most one is null. Set interfaces include EnumSet, HashSet, and TreeSet. Is the JDK source code UML diagram of Set.
List:
An ordered Collection (also called a sequence) with repeated elements. To be exact, the list is usually allowed to meetE1.equals (e2)Element pairE1AndE2And if the List itself allows null elements, they usually allow multiple null elements. The following lists are implemented: ArrayList, rule List, Vector, and Stack. Is the List of JDK source code UML diagram.
Queue:
A queue is a dual-end queue that supports inserting and removing elements at the header and tail ends, including ArrayDeque, LinkedBlockingDeque, and LinkedList. The other is a blocking queue. When the queue is full and then inserted with elements, an exception is thrown, including ArrayBlockQueue, PriorityBlockingQueue, and LinkedBlockingQueue. Although the interface does not define the blocking method, the implementation class extends this interface. Is the JDK source code UML diagram of Queue.
3. Hierarchy of Map classes
The following figure shows the Map hierarchy.
Map:
Is a set of key-value pairs. That is to say, a ing cannot contain duplicate keys, and each key can be mapped to a maximum of one value. This interface replaces the Dictionary abstract class. Map implementation includes HashMap, TreeMap, HashTable, Properties, and EnumMap. Is the JDK source code UML diagram of Map.
Iv. Summary of container Interfaces
V. Sample Code
Examples of HashMap, HashSet, rule list, ArrayList, TreeMap, and TreeSet are as follows:
?
| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081 |
import java.util.ArrayList;import java.util.HashMap;import java.util.HashSet;import java.util.LinkedList;import java.util.List;import java.util.Map;import java.util.Set;import java.util.TreeMap;import java.util.TreeSet; @SuppressWarnings("unchecked")public class CollectionAll{ public static void main(String[] args) { printLists(); printSets(); printMaps(); } private static void printLists() { List<String> a1 = new ArrayList<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 = new LinkedList<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 void printSets() { Set h1 = new HashSet<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 = new TreeSet<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 void printMaps() { Map<String, String> h1 = new HashMap<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 = new TreeMap<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 is printed as follows:
?
| 123456789101112 |
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-synchronous, so it is thread-safe, while arraylist is asynchronous and insecure. If thread security is not taken into account, arraylist is generally used for high efficiency.
2. If the number of elements in the set is greater than the length of the current set array, the growth rate of vector is 100% of the current array length, and that of arraylist is 50% of the current array length. if you use a large amount of data in a collection, using vector has some advantages.
3. If you look for data at a specified position, the time used by the vector and arraylist is the same, both of which are 0 (1). In this case, you can use both vector and arraylist. If it takes 0 (n-I) n to move the data at a specified position as the total length, you should consider using linklist, because it takes 0 (1) to move data at a specified location, and the time spent querying data at a specified location is 0 (I ).
ArrayList and Vector use arrays to store data. The number of elements in the array is greater than that in the actual storage to add and insert elements. Both allow direct serial number index elements, however, data insertion is designed to move array elements and other memory operations. Therefore, index data is inserted slowly. Because Vector uses the synchronized Method (thread-safe), its performance is inferior to that of ArrayList, the sort list uses a two-way linked list for storage. Data indexed by serial number needs to be traversed forward or backward. However, when inserting data, you only need to record the items before and after this item, so the insertion speed is faster!
Categorraylist and categorlist
1. ArrayList is a dynamic array-based data structure. The ArrayList is based on the linked list data structure.
2. for Random Access to get and set, ArrayList thinks it is better than the sorted list because the sorted list needs to move the pointer.
3. For add and remove operations, LinedList is dominant because ArrayList needs to move data.
This depends on the actual situation. If only a single piece of data is inserted or deleted, the ArrayList speed is better than the sort list speed. However, if data is inserted and deleted randomly in batches, the speed of the sorted list is much higher than that of the ArrayList. Because each inserted data in the ArrayList needs to move the inserted point and all subsequent data.
HashMap and TreeMap
1. HashMap uses hashcode to quickly search its content, while all elements in TreeMap maintain a fixed order, if you need to get an ordered result, you should use TreeMap (the arrangement order of elements in HashMap is not fixed ). The order of elements in HashMap is not fixed ).
2. HashMap uses hashcode to quickly search its content, while all elements in TreeMap maintain a fixed order, if you need to get an ordered result, you should use TreeMap (the arrangement order of elements in HashMap is not fixed ). The Collection framework provides two general Map implementations: HashMap and TreeMap (the SortedMap interface implemented by TreeMap ).
3. insert, delete, and locate elements in Map. HashMap is the best choice. However, if you want to traverse keys in the natural or custom order, it is better to use TreeMap. The key classes required to be added using HashMap clearly define the implementation of hashCode () and equals. This TreeMap has no optimization options because the tree is always in the balance state.
Hashtable and hashmap
1. Historical Reasons: Hashtable is based on the obsolete Dictionary class, And HashMap is an implementation of the Map interface introduced by Java 1.2.
2. Synchronization: Hashtable is thread-safe, that is, synchronous, while HashMap is not secure or synchronous.
3. value: Only HashMap allows you to use a null value as the key or value of a table entry.