Turn from:
http://www.cnblogs.com/fczjuever/archive/2013/04/07/3005997.html 1. Elaborates
In the Java map Traversal way, many articles are recommended to use EntrySet, think it is more efficient than keyset. The reason: The EntrySet method gets all the keys and value sets at a time, and keyset gets only a set of keys, which, for each key, goes to the map to find an extra value, which reduces overall efficiency. So what is the situation?
To understand
It seems that map inherits from Collection like other Collection classes. In fact, Map and Collection do not have any relationship at the structural level, by viewing the source code, we can find that all map operations are based on key-value pairs, rather than individual elements.
The following uses HashMap as an example to gain an in-depth understanding of the implementation mechanism of Map. In this proc
, the also look at Concurrenthashmap.In short, unsynchronized to be faster, but in view of Hashtable this concept in the interview than HashMap to come more, so in the face of the test is still used Hashtable bar, in the project development I have always used HashMap.REF:http://blog.csdn.net/steedhorse/article/details/6892569Http://stackoverflow.com/questions/355089/stringbuilder-and-stringbuffer-in-javaHttp://vanillajava.blogspot.de/2013/04/why-synch
Two ways to HashMap traversal in JavaOriginal address: http://www.javaweb.cc/language/java/032291.shtmlThe first type:Map map = new HashMap ();Iterator iter = Map.entryset (). Iterator ();while (Iter.hasnext ()) {Map.entry Entry = (map.entry) iter.next ();Object key = Entry.getkey ();Object val = Entry.getvalue ();}The efficiency is high, must use this kind of wa
HashMap and HashSet are two important members of the Java Collection Framework, where HashMap is the common implementation class of the Map interface1: Create a class first 1 package day; 2 3 Import java.util.ArrayList; import Java.util.HashMap; import java.util.Iterator; import Java.util.Map; import java.u
Taobao in the intranet to see colleagues posted a CPU was 100% of the online fault, and this happened many times, because the Java language in the concurrency of the use of HashMap caused race Condition, resulting in a dead loop. This thing I have experienced 4, 5 years ago, I thought there was nothing to write, because Java
1, Hashtable is a dictionary subclass,
Copy Code code as follows:
public class HashtableExtends dictionaryImplements Map
HASHMAP:
Copy Code code as follows:
public class HashmapExtends abstractmapImplements Map
HashMap and Hashtable are all the implementation classes of the map interface;
2, the method in Hashtable is sync
quickly through the hash function.The Hashcode method in Java is to map the object-related information (such as the object's storage address, the object's field, and so on) into a numeric value, which is called a hash, according to certain rules.Back to see, hashcode corresponds to the key code value of keys, and then we put the Java code in the string hashcode calculation code to see:The hashcode is actua
values.Where key and value are objects and cannot contain duplicate keys, they can contain duplicate value.In Hashtable, null values are not allowed for key and value.In HashMap, NULL can be a key, with only one key, and a value of one or more keys to null. When the Get () method returns a null value, it may be that the key is not in the hashmap, or it may cause the value of the key to be null. Therefore,
the use and traversal of HashMap in JavaCategory: Algorithms and data Structures 2011-10-19 10:53 5345 people read reviews (0) favorite reports Hashmapjavastringobjectiteratorlist1: A simple example of using HashMap[Java]View Plaincopy
Package com.pb.collection;
Import Java.util.HashMap;
Import Java.util.Iterator;
Import Java.util.Set;
Import J
.Hashtable supports iterator and enumeration two ways of traversing . 5) Different initial capacity and expansion modethe default capacity of the HashMap is twice times the capacity of the original, when expanding;the default capacity of the Hashtable is one, and the capacity is changed to twice timesthe original +1.6) The hash value algorithm for adding key-value is differentHashMap When adding elements, a custom hashing algorithm is used ;Hashtable
The analysis of a storage class is nothing more than two points: the data structure used for storage and the operating mechanism of storage.
Data structure:
Array
[Java]/*** The table, resized as necessary. Length MUST Always be a power of two.*/Transient Entry [] table;
/*** The table, resized as necessary. Length MUST Always be a power of two.*/Transient Entry [] table;Entry
Linked List format.
That is, HashMap
Time Angle: Hashtable * @since JDK1.0 ; HashMap * @since 1.2base class and interface angle:public class HashtableExtends dictionaryImplements Mappublic class HashmapExtends abstractmapImplements MapImplements the same interface, inheriting from different base classes (dictionaries) dictionaryThread-Safe angle:Hashmap.java:* * If Multiple threads access a hash map concurrently, and at least one of* The threads modifies the map structurally, it * Sy
HashMap and Linkedhashmap are two implementation classes of the map interface, the biggest difference is that hashmap elements are stored in order, and the elements of Linkedhashmap are stored in an orderly manner, example:mapOutput Result:Hashmap3:32:21:10:07:76:65:54:49:98:8linkedhashmap0:01:12:23:34:45:56:67:78:89:9Previously also published a book to achieve t
Problem
Comparison of Java Native
1. TreeMap
2. HashMap
3. ConcurrentSkipListMap
Efficiency of three Map types.
Result
Simulate the insertion and search of massive data within, and test the performance by adding and searching. The results are as follows:
Map type
Insert
Search (within million data records)
10 W
50 W
100 million
150 million
0-1 W
0-25 W
0-50 W
HashMap for Java Collection Framework LearningThe following features of HashMap can be obtained from the API documentation: implemented based on the hash table, and the chain hash table allows null values and null keys (null = null key-value pairs) hashMap and Hashtable are basically the same. The difference is that
When explaining HashmapThe implementation of a hash table-based MAP interface. This implementation provides all the optional mapping operations and allows NULL values and NULL keys to be used. (The HashMap class is roughly the same as Hashtable except for non-synchronous and null-allowed use.) This class does not guarantee the order of the mappings, especially be
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.