1,java.util.map
import
java.util.HashMap;
import
java.util.Iterator;
import
java.util.LinkedHashMap;
import
java.util.Map;
public
class TestLinkedHashMap {
public
static
void
main(String args[])
{
System.out.println(
"*************************LinkedHashMap*************"
);
Map<Integer,String> map =
new LinkedHashMap<Integer,String>();
map.put(
6
,
"apple"
);
map.put(
3
,
"banana"
);
map.put(
2
,
"pear"
);
for
(Iterator it = map.keySet().iterator();it.hasNext();)
{
Object key = it.next();
System.out.println( key+
"="
+ map.get(key));
}
System.out.println(
"*************************HashMap*************"
);
Map<Integer,String> map1 =
new
HashMap<Integer,String>();
map1.put(
6
,
"apple"
);
map1.put(
3
,
"banana"
);
map1.put(
2
,
"pear"
);
for
(Iterator it = map1.keySet().iterator();it.hasNext();)
{
Object key = it.next();
System.out.println( key+
"="
+ map1.get(key));
}
}
}
|
The results of the operation are as follows:
linkedhashmap*************
6=apple
3=banana
2=pear
hashmap**************************
2=pear
6=apple
3=banana
Analysis: Linkedhashmap is characterized by the position of the put in the object has not changed, and the HashMap will change.
Again to popularize under:
Java defines an interface java.util.Map for the mappings in the data structure; it has four implementation classes, namely HashMap Hashtable Linkedhashmap and TreeMap.
The map is primarily used to store the health-value pairs, and the key is not allowed to repeat (overwriting is repeated), but allows the values to be duplicated.
Hashmap is the most commonly used map, it stores data according to the hashcode value of the key, can get its value directly according to the key, has the fast access speed, when traversing, obtains the data the order is completely random. HashMap allows a maximum of one record's key to be null, allowing multiple records to have a value of NULL; HashMap does not support thread synchronization, where multiple threads can write HashMap at any one time, and may result in inconsistent data. If synchronization is required, you can use the collections Synchronizedmap method to make the HashMap capable of synchronizing or using Concurrenthashmap.
Hashtable is similar to HashMap, which inherits from the dictionary class, except that it does not allow the record key or value to be null; it supports thread synchronization, which means that only one thread can write hashtable at any one time, so it also causes the hashtable to be slower to write.
Linkedhashmap is a subclass of HashMap, which preserves the order in which records are inserted, and when traversing linkedhashmap with iterator, the first records must be inserted first. You can also use parameters in construction to sort by number of applications. The traversal will be slower than HashMap, but there is an exception, when the HashMap capacity is large, the actual data is small, the traversal may be slower than Linkedhashmap, because the Linkedhashmap traverse speed is only related to the actual data, and capacity-independent, And HashMap's traverse speed is related to his capacity.
TreeMap implements the Sortmap interface, it can save the record according to the key sort, the default is the key value of ascending order, you can also specify a sort of comparator, when using iterator traversal treemap, the resulting records are ordered.
In general, the most we use is HashMap, inserting, deleting and locating elements in the 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. If the order of the output needs to be the same as the input, then it can be implemented by LINKEDHASHMAP, and it can be arranged in the order of reading.
HashMap is the most commonly used map, which stores data according to the hashcode value of the key, which can be obtained directly from the key, with fast access speed. HashMap allows a maximum of one record's key to be null, allowing multiple records to have a value of NULL.
HashMap does not support thread synchronization, which means that multiple threads can write HashMap at any one time, which may cause inconsistencies in the data. If synchronization is required, you can use the collections Synchronizedmap method to make the HashMap capable of synchronizing.
Hashtable is similar to HashMap, except that it does not allow the record key or value to be null; it supports thread synchronization, which means that only one thread can write hashtable at any one time, so it also causes the hashtable to write slower.
Linkedhashmap saves the order in which records are inserted, and when traversing linkedhashmap with iterator, the first records must be inserted first.
When traversing, it is slower than HashMap treemap to sort the records it saves according to the key, by default it is sorted in ascending order, or a sort comparer can be specified. When traversing treemap with iterator, the resulting record is ordered.
Java Collection usage detailed