[Go] Collection and javacollection in JAVA
In Java2, a set of well-designed interfaces and classes constitute a Collection of Java Collection frameworks, making it extremely convenient for programmers to operate data or object elements in batches. These interfaces and classes have many APIs for abstract data type operations, which are common and well-known in data structures. For example, Map, Set, and List. In addition, Java uses object-oriented design to encapsulate these data structures and algorithms, which greatly reduces the programmer's programming burden. Programmers can also define high-level data abstraction based on this collection framework, such as stacks, queues, and thread-safe collections to meet their own needs.
The Collection framework of Java2 draws its core into three types: List, Set, and Map. As shown in:
It should be noted that the Collection, List, Set, and Map operations here are interfaces rather than specific class implementations. List lst = new ArrayList (); this is a frequently used statement to create a new List. Here, List is an interface and ArrayList is a specific class.
The inheritance structure of common collection classes is as follows:
Collection <-- List <-- Vector
Collection <-- List <-- ArrayList
Collection <-- List <-- items List
Collection <-- Set <-- HashSet
Collection <-- Set <-- HashSet <-- revoke HashSet
Collection <-- Set <-- SortedSet <-- TreeSet
Map <-- SortedMap <-- TreeMap
Map <-- HashMap
----------------------------------------------- SB split line ------------------------------------------
List:
List is an ordered Collection, which can be used to precisely control the insert position of each element. You can use an index (the position of an element in the List, similar to an array> MARK) to access the elements in the List, which is similar to an array in Java.
Vector:
Array-based List encapsulates some functions not available in the Array for our convenience. Therefore, it is difficult to avoid Array restrictions and its performance cannot surpass the Array. Therefore, when possible, we need to use more arrays. Another important point is that Vector is synchronized by threads, which is also an important difference between Vector and ArrayList.
ArrayList:
Like Vector, it is an array-based linked list, but the difference is that ArrayList is not synchronized. Therefore, the performance is better than the Vector, but when running in a multi-threaded environment, you need to manage the thread synchronization issues on your own.
Shortlist:
The sorted List is different from the previous two lists. It is not based on arrays, so it is not limited by Array Performance.
Each Node contains two aspects:
1. data of the node itself );
2. Information about the next node (nextNode ).
Therefore, when you add an array list and delete it, you do not need to move a large amount of data like the array-based ArrayList. You only need to change the relevant information of nextNode. This is the advantage of the nextNode list.
List summary:
- All lists can only contain tables composed of a single object of different types, rather than Key-Value pairs. Example: [tom, 1, c]
- All lists can have the same elements, such as [tom, koo, too, koo] in the Vector.
- All lists can have null elements, such as [tom, null, 1].
- Array-based List (Vector, ArrayList) is suitable for queries, while sorted List is suitable for adding and deleting operations.
-------------------------------------- NB split line ------------------------------------
Set:
Set is an unordered Collection that does not contain repeated elements.
HashSet:
Although Set and List all implement the Collection interface, their implementation methods are quite different. List is basically based on Array. However, Set is implemented based on HashMap, which is the fundamental difference between Set and List. The HashSet storage method uses the Key in HashMap as the corresponding storage item of the Set. Let's take a look at the implementation of the add (Object obj) method in HashSet.
Java code
- Public boolean add (Object obj ){
- Return map. put (obj, PRESENT) = null;
- }
This is also the root reason why repeated items cannot be found in the Set as in the List, because the key of HashMap cannot be duplicated.
LinkedHashSet:
A subclass of HashSet, a linked list.
TreeSet:
A subclass of SortedSet. Unlike HashSet, TreeSet is ordered. It is implemented through SortedMap.
Set summary:
- The basis of Set implementation is Map (HashMap)
- The elements in the Set cannot be repeated. If you use the add (Object obj) method to add an existing Object, the existing Object will be overwritten.
-------------------------------------- 2B split line ------------------------------------
Map:
Map is a container that associates key objects and value objects, and a value object can be a Map, and so on. In this way, a multi-level ing can be formed. For key objects, similar to Set, key objects in a Map container are not allowed to be repeated to maintain consistency of the search results. If there are two key objects, the problem occurs when you want to get the value object corresponding to the key object. Maybe what you get is not the value object you want, and the result will be messy, therefore, the uniqueness of keys is very important and also conforms to the set nature. Of course, during use, the value object corresponding to a key may change. At this time, the modified value object will correspond to the key. There is no uniqueness requirement for value objects. You can map any number of keys to a value object, this will not cause any problems (but it may cause inconvenience to your use, you do not know whether you get the value object corresponding to that key ).
Map has two common implementations: HashMap and TreeMap.
HashMap also uses the hash algorithm to quickly find a key,
TreeMap stores keys in sequence, so it has some extended methods, such as firstKey () and lastKey, you can also specify a range from TreeMap to obtain its submap.
The association between keys and values is very simple. You can associate a key with a value Object using the put (Object key, Object value) method. You can use get (Object key) to obtain the value Object corresponding to this key Object.
-------------------------------------- JB split line ------------------------------------
Others:
I. Differences between several common classes
1. ArrayList: single element, high efficiency, mostly used for query
2. Vector: single element, thread-safe, mostly used for query
3. Multiple list: a single element. It is mostly used for insertion and deletion.
4. HashMap: The elements are paired and can be empty.
5. HashTable: The elements are paired and thread-safe. The elements cannot be empty.
2. Vector, ArrayList, and sorted list
In most cases, ArrayList is the best in terms of performance. However, when elements in a set need to be inserted or deleted frequently, the sorted list can be better, but the three of them cannot match the array, in addition, the Vector is synchronized by threads. Therefore:
If you can use arrays (fixed element types and Fixed Array lengths), use arrays instead of List;
If frequent deletion and insertion operations are not performed, you do not need to consider multithreading. Select ArrayList first;
If multiple threads are used, you can consider Vector;
If you need to delete inserts frequently, the sorted list can be used;
If you don't know anything, use ArrayList.
Iii. Collections and Arrays
There are two classes in the Java Collection class framework called Collections (note, not Collection !) And Arrays, which are powerful tools in JCF, are often ignored by beginners. According to the JCF documentation, these two classes provide Wrapper Implementations, data structure algorithms, and array-related applications.
Surely you will not forget the classic algorithms mentioned above, such as "half-lookup" and "sorting, the Collections class provides a variety of static methods to help us easily complete these annoying tasks in the data structure class:
BinarySearch: semi-query.
Sort: sorting, which is similar to a fast sorting method. The efficiency is still O (n * log n), but it is a stable sorting method.
Reverse: this is a classic question of the previous data structure!
Rotate: The linear table is "rotated" with an element as the axis ".
Swap: swap the positions of two elements in a linear table.
......
Another important feature of Collections is Wrapper. It provides some methods to convert a set into a special set, as shown below:
UnmodifiableXXX: convert to a read-only Set. Here XXX represents six basic Set interfaces: Collection, List, Map, Set, SortedMap, and SortedSet. If you insert or delete a read-only set, an UnsupportedOperationException is thrown.
SynchronizedXXX: converts to a synchronization set.
Singleton: Creates a Set with only one element. Here, singleton generates a single element Set,
SingletonList and singletonMap generate the List and Map of a single element respectively.
Empty set: expressed by the static attributes EMPTY_SET, EMPTY_LIST, and EMPTY_MAP of Collections.
The Java Collection class overview is here. Next time we will explain the specific applications of the Java Collection class, such as List sorting and deleting duplicate elements.