Collection, List, Set and Map usage and differences

Source: Internet
Author: User

Collection, List, Set and MAP usage and differences

Author: zccst

The invocation relationship of the Java Spring MVC Framework is clear, but there is still a long way to go, and a lot of things are still mysterious to me, such as list, set, map, and so on, today, one by one to unlock their veil. When there are still a lot of memory-requiring systems that have encapsulated classes, interfaces, and so on, these things will come slowly in the future.

First look at the relationship between them

The collection of interface objects for the Collection interface
├listSub-interfacesAccording to the order of access to save repeatable
│├linkedlist interface Implementation Class list insert Delete no sync thread unsafe
│├arraylist interface Implementation class array random access not synchronized thread unsafe
│└vector interface implementation class array synchronization thread safety
│└stack
└setSub-interfacesReceive only once and do an internal sort

├hashset

│└linkedhashset
└treeset

For the List, the concern is Order, which guarantees that the element is maintained in a particular order (allowing for the same element), and that the interface is used to precisely control where each element is inserted. Users can access the elements in the list by using the index (the position of the element in the list, similar to the array subscript).

For set, only the element is concerned about whether it belongs to set (not allowed to have the same element), not its order.

A collection of MAP interface key-value pairs
├hashtable interface implements class synchronization thread safety
├hashmap interface implementation class not synchronized thread unsafe

│├linkedhashmap

│└weakhashmap

├treemap
└identifyhashmap


For map, the biggest feature is the key value mapping, and one by one mapping, the key can not be repeated, the value is OK, so the key to index the value. Method put (Object key, object value) adds a "value" (What you Want) and a key that is associated with the value (used to find it). The method get (Object key) returns the value associated with the given key.

Map also saves one copy of each element, but this is based on the "key", and the map has a built-in sort, and therefore does not care about the order in which the elements are added. If the order in which you add elements is important to you, you should use Linkedhashset or Linkedhashmap.

For efficiency, the map uses hash hashes to find elements that are significantly faster than ArrayList.

But I have an idea of my own principles: complex questions are simplistic. That is, a lot of obscure problems with the popular straightforward words, all of a sudden see understand, rather than a large paragraph of writing. It must be pointed out that now some so-called "experts" often complicate the simple problem, let a person look daunting, or even deterred, in order to show his inscrutable, of course, may have other intentions, then I do not know.

A more refined summary:

Collection is a collection of objects, Collection has two sub-interfaces List and set

List can be obtained by subscript (1,2.) and the value can be repeated

The Set can only be evaluated by a cursor, and the value cannot be duplicated

ArrayList, Vector, LinkedList is the implementation class of List

ArrayList is thread insecure, Vector is thread safe, and the bottom of these two classes are all implemented by arrays

LinkedList is thread insecure, and the bottom is realized by the linked list

Map is a collection of key-value pairs

HashTable and HASHMAP are the implementation classes of the MAP
HashTable is thread safe and cannot store null values
HASHMAP is not thread-safe and can store null values

So if you're trying to figure out these problems in a very short time, say 1-2 minutes. No and don't want to spend a lot of time on it, so it is advisable to leave now.

If you want to have a detailed understanding of this, please continue to watch.

As we all know, Java from C + +, shielding its low-level implementation, simplifying the implementation of the bottom of the management, so that developers focus on the implementation of the upper level. In C + +, the data storage needs to be very clear to the programmer, and Java programmers can completely ignore these, then Java is how to manage it. In fact, Java still need to face these problems, but after encapsulation, become unrecognizable. So for people like me, from C + + to Java, it will take some time to adapt, Collection, List, Set, Map and other concepts also need an accepted process. In fact, it turns out that no matter what language it is, its underlying storage is the data structure of arrays, linear tables, stacks, queues, strings, trees and graphs. To understand this, everything is bright.


First, container (Collection) interface
A container (Collection) is the most basic set interface, and a container (Collection) holds a set of objects (object), which is the element of the container (Elements). Some Collection allow the same elements while others do not. Some can be sorted and others not. The Java SDK does not provide classes that directly inherit from Collection, and the Java SDK provides classes that inherit from Collection such as List and Set.
All classes that implement the Collection interface must provide two standard constructors: parameterless constructors are used to create an empty Collection, and a constructor for a Collection parameter is used to create a new Collection, the new The Collection has the same element as the incoming Collection. The latter constructor allows the user to replicate a Collection.
How to traverse every element in the Collection. Regardless of the actual type of Collection, it supports a iterator () method that returns an iteration that can be used to access each element of the Collection one at a time. Typical usage is as follows:
Iterator it = Collection.iterator (); To get an iterative child
while (It.hasnext ()) {
Object obj = It.next (); Get the next element
}

The two interfaces that are derived from the Collection interface are List and Set. List saves objects in the order in which they are entered, without sorting or editing operations. Set accepts only once for each object and uses its own internal sorting method (usually, you only care about whether an element belongs to the set, not the order of it--otherwise you should use the List).

1, List interface
List is an ordered Collection, and order is the most important feature of the list: it guarantees that the order of elements is maintained. Use this interface to accurately control where each element is inserted. The user can access the elements in the list, similar to the Java array, by using the index (the position of the element in the list, similar to the array subscript). Unlike the Set mentioned below, the List allows the same elements.
In addition to the iterator () method with the Collection interface prerequisites, the List also provides a listiterator () method that returns a Listiterator interface that is more iterator than the standard Listiterator interface. Some methods such as add () allow adding, deleting, setting elements, and traversing forward or backwards.
Common classes that implement the List interface are LinkedList, ArrayList, Vector, and Stack. Among them, the most commonly used are LinkedList and ArrayList two.

LinkedList class
LinkedList implements the List interface, allowing null elements. Additionally LinkedList provides additional addfirst (), AddLast (), GetFirst (), GetLast (), Removefirst (), Removelast (), Insertfirst (), Insertlast () methods in the header or tail of the LinkedList, these methods (not defined in any interface or base class) enable LinkedList to be used as stacks, queues (queue), or bidirectional queues (deque).

Note that LinkedList does not have a synchronization method. If multiple threads access a List at the same time, you must implement access synchronization yourself. One workaround is to construct a synchronized list when the list is created:
List List = Collections.synchronizedlist (new LinkedList (...));

Features: The sequential access is optimized, and the overhead of inserting and deleting into the List is not significant. Random access is relatively slow. (use ArrayList instead.) )

ArrayList class
ArrayList is a List implemented by an array and implements a variable sized array. It allows all elements, including null. ArrayList is not synchronized. Size, IsEmpty, get, set method run time is constant. However, the Add method cost is the allocated constant, and the time of O (n) is required to add n elements. The other methods run at a linear time.
Each ArrayList instance has a capacity (Capacity), which is the size of the array used to store the elements. This capacity can automatically increase as new elements are added, but the growth algorithm is not defined. When you need to insert a large number of elements, you can call the Ensurecapacity method before inserting to increase the capacity of the ArrayList to increase the insertion efficiency.
Like LinkedList, ArrayList is also unsynchronized (unsynchronized).

Feature: Allows quick random access to elements, but inserts and removes elements to the middle of the List at a slow rate. Instead of inserting and removing elements, listiterator should only be used to traverse the ArrayList from the back forward. Because it's a lot bigger than the linkedlist overhead.

Vector class
Vectors are very similar to ArrayList, but vectors are synchronized. Iterator created by Vector, although the same interface was created with ArrayList, but because vectors are synchronized, when one iterator is created and is being used, another thread changes the state of the vector (for example, adding Or some elements are removed), the concurrentmodificationexception is thrown when the iterator method is invoked, so the exception must be caught.

   Stack class: Stack inherits from the Vector, implementing a LIFO heap. Stack provides 5 additional methods that allow vectors to be used as stacks. Basic push and Pop methods, and Peek method to get the top of the stack elements, empty method test stack is empty, the search method detects an element in the stack position. Stack is just created after stack is empty.

2, Set interface
Set has exactly the same interface as Collection, so there is no extra functionality, unlike a few different lists in front of it. In fact Set is Collection, but behavior is different. (This is a typical application of inheritance and polymorphic thinking: behavior that behaves differently). Second, set is a Collection that does not contain duplicate elements, and elements that join the set must define the Equals () method to ensure the uniqueness of the object (that is, any two elements E1 and E2 have E1.equals (E2) =false), unlike the List Is that the Set interface does not guarantee the order of elements to be maintained. Finally, Set has a maximum of one null element.
Obviously, the constructor of the Set has a constraint, and the incoming Collection parameter cannot contain duplicate elements.
Note: You must be careful to manipulate variable objects (mutable object). If a variable element in a Set changes its state, causing the Object.Equals (Object) =true to cause some problems.

HashSet class

Set for quick Find design. The object that is stored in the HashSet must define HASHCODE ().

Linkedhashset class: Has a hashset query speed and internally uses a linked list to maintain the order of elements (in the order of insertion). So when you use iterators to traverse Set, the results are displayed in the order in which the elements are inserted.

TreeSet class

The Set of Save order, the bottom is the tree structure. Use it to extract ordered sequences from Set.

Two, MAP interface
Note that the map does not inherit the Collection interface, and the map provides a mapping of key to value, and you can find the value by "key". A map cannot contain the same key, and each key can map only one value. The map interface provides a view of 3 collections, which can be treated as a set of key sets, a set of value sets, or a set of key-value mappings.

Method put (Object key, object value) adds a "value" (What you Want) and a key that is associated with the value (used to find it). The method get (Object key) returns the value associated with the given key. You can use ContainsKey () and Containsvalue () to test whether a "key" or "value" is included in the Map. The standard Java class library contains several different map:hashmap, TreeMap, Linkedhashmap, Weakhashmap, and Identityhashmap. They all have the same basic interface Map, but behavior, efficiency, sorting strategies, the lifecycle of saving objects, and the policy of determining "key" equivalence are different.

Map also saves one copy of each element, but this is based on the "key", and the map has a built-in sort, and therefore does not care about the order in which the elements are added. If the order in which you add elements is important to you, you should use Linkedhashset or Linkedhashmap.

Execution efficiency is a big problem in Map. Look at what gets () to do, and you'll see why it's rather slow to search for "keys" in ArrayList. And that's where HashMap is raising speed. HashMap uses a special value, called hash code, to replace a slow search for keys. "Hash code" is "relatively unique" to represent an int value of an object, which is generated by converting some of the information of that object (summary two below: where the need for attention is further explored). All Java objects can produce hash codes, because Hashcode () is a method defined in the base class object. HashMap is a quick query using an object's Hashcode (). This approach can significantly improve performance.


Hashtable class
Hashtable inherits the map interface and implements a hash table of key-value mappings. Any object that is Non-null (non-null) can be a key or value.
Adding data using put (key, value), fetching data using get (key), the time cost of these two basic operations is constant.
The Hashtable adjusts performance by initializing capacity (initial capacity) and load factor (loading factor) two parameters. Typically, the default load factor 0.75 achieves a better balance of time and space. Increasing the load factor saves space but the corresponding lookup time increases, which can affect operations like get and put.
Using Hashtable's simple example, put 1, 2, 3 in the Hashtable, their key is "one", "two", "three":
Hashtable numbers = new Hashtable ();
Numbers.put ("One", New Integer (1));
Numbers.put ("Two", New Integer (2));
Numbers.put ("Three", New Integer (3));
To remove a number, such as 2, use the corresponding key:
Integer n = (integer) numbers.get ("two");
System.out.println ("two =" + N);
Since an object that is a key will determine the location of its corresponding value by calculating its hash function, any object that is a key must implement the Hashcode method and the Equals method. The Hashcode method and the Equals method inherit from the root class object, and if you use a custom class as a key, be very careful, according to the definition of the hash function, if two objects are the same, i.e. obj1.equals (OBJ2) =true, their hashcode Must be the same, but if two objects are different, their hashcode is not necessarily different, if two different objects hashcode the same, this phenomenon is called a conflict, the conflict will lead to the operation of the hash table time overhead, so try to define a good hashcode () method, can speed up the operation of the hash table.
If the same object has a different hashcode, the operation of the hash table will have unexpected results (the expected GET method returns null), to avoid this problem, you need to keep in mind one: you have to copy both the Equals method and the Hashcode method, not just one.
The Hashtable is synchronized.

HashMap class
HashMap is similar to Hashtable and is based on a hash table implementation. The difference is that HASHMAP is asynchronous and allows null, that is, null value and null key. When HASHMAP is treated as Collection (the values () method returns Collection), the cost of inserting and querying "key-value pairs" is fixed, but its iteration time overhead is proportional to the capacity of the HashMap. Therefore, if the performance of an iterative operation is significant, do not set the initialization capacity of the HASHMAP (initial capacity) too high, or the load factor (load factor) is too low.


Linkedhashmap class: Similar to HashMap, but when iterating through it, the order in which "key value pairs" is obtained is either the insertion order or the most recent order in which (LRU) is used. Just a little slower than HashMap. It is faster when iterating through the access, because it uses a linked list to maintain the internal order.

Weakhashmap class: Weak key (weak key) Map is an improved HASHMAP, which is designed to solve special problems, "weak reference" to key, if a key is no longer referenced externally (no Map outside of the reference), then the key can Recycled by the garbage collector (GC).

TreeMap class

Based on the data structure of the red-black tree implementation. When you view key or key-value pairs, they are sorted (in order by Comparabel or Comparator). TreeMap is characterized by the fact that the results you get are sorted. TreeMap is the only Map with a SubMap () method that can return a subtree.

Identifyhashmap class

Use = = instead of equals () the hash map for the "key" comparison. Designed to solve specific problems.

Summary one: comparison

1, arrays (array), array class (Arrays)

All Java "store and random access to a series of objects" approach, array is the most efficient one. But the disadvantage is that the capacity is fixed and cannot be changed dynamically. Array also has the disadvantage that it is impossible to tell how many elements are actually in it, and length simply tells us the size of the array.

There is an array class (Arrays) in Java that is specifically used to manipulate an array. An array class (arrays) has a set of static functions.

Equals (): Compares two array for equality. Array has the same number of elements, and all corresponding elements are 22 equal.

Fill (): Fills the value into the array.

Sort (): is used to sort the array.

BinarySearch (): Look for elements in a sorted array.

System.arraycopy (): Replication of array.

If you write the program does not know how many objects, need to be in the space of automatic amplification capacity, you need to use the Container class library, array does not apply.

2, the difference between the container class and the array

A container class can only hold an object reference (a pointer to an object) instead of copy the object information to a location in the sequence. Once an object is placed inside the container, the type information of the object is lost.

3, the relation and difference between the container (Collection) and Map

Collection type, with only one element per location.

Map type, holding Key-value pair (pair), like a small database.

Collections is a Help class for collection classes. Provides a series of static methods to achieve the search, sorting, threading, and other operations of the various sets. A class--arrays that is equivalent to a similar operation on an Array.

For example, Collections.max (Collection coll); Takes the largest element in the Coll.

Collections.sort (list list); Sort the elements in a list

List, set, and Map hold objects as object types.

Collection, List, Set, Map are interfaces and cannot be instantiated. Inherited from their ArrayList, vectors, HashTable, HashMap is the representational class, which can be instantiated.

The vector container knows exactly what type of object it holds. Vectors do not conduct boundary checks.

Summary Two: Places to pay attention to

1, Collection can only traverse the element through iterator (), there is no get () method to obtain an element.

2, Set and Collection have exactly the same interface. But excludes elements that are duplicated in the incoming Collection parameter.

3, List, you can use the Get () method to take out an element at a time. Use numbers to select one of a bunch of objects, get (0) .... (Add/get)

4. Map with put (K,V)/get (k), you can also use ContainsKey ()/containsvalue () to check whether it contains a key/value.

HashMap will use the object's hashcode to quickly find the key.

A hash code (hashing) is a unique int value, which is stored in an array, by transforming the information of an object into a single int. We all know that the array lookup speed is the fastest in all storage structures. So, you can speed up the lookup. When a collision occurs, let the array point to multiple values. That is, a table of durian is generated at each location of the array.

5, the MAP elements, can be the key sequence, value sequence separately extracted.

Extracts the key sequence using keyset () to generate a set for all keys in the map.

Extract the value sequence using values () to generate a Collection for all values in the map.

Why one generates Set, one generates Collection. That's because key is always unique, and value allows repetition.


Summary three: How to choose
From the efficiency point of view:

In various Lists, you should use LinkedList (LinkedList build stack stacks, queue queues) for quick inserts and deletion of elements, and you should use ArrayList if you need quick random access to elements. The best practice is to use ArrayList as the default choice. Vectors are always slower than ArrayList, so try to avoid using them.

In various Sets, HashSet is usually superior to hashtree (insert, find). Use TreeSet only if you need to produce a sorted sequence. The only reason for the existence of Hashtree: the ability to maintain the sort state of its elements.

HashMap is used for quick lookups in various Maps.

Finally, when the number of elements is fixed, the array is used because the array efficiency is the highest.

So the conclusion: the most commonly used is ArrayList, HashSet, HashMap, Array.

a step closer analysis:

If the program is in a single-threaded environment, or if access is only done in one thread, it is more efficient to consider a class that is not synchronized, and should use a synchronized class if multiple threads might be working on a class at the same time.
Special attention should be paid to the operation of the hash table, as the object of the key should simultaneously correct the Equals method and the Hashcode method.
Try to return the interface instead of the actual type, such as returning a List instead of ArrayList, so that if you need to replace ArrayList with LinkedList later, the client code does not have to change. This is for abstract programming.

Note: This article refers to some network literature.

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.