JDK HashMap and HashSet analysis, jdkhashmaphashset
HashMap mainly analyzes the operations of putting key and value into Map and retrieving Map, as well as its traversal tool. I personally think that there is an important internal class Entry in HashMap, And the put and get methods of Map depend on this Entry. First, let's analyze this internal class Entry. The Entry contains several important variables: key, value, and next. Needless to say, everyone will understand the meanings of these variab
element corresponding to the specified key from the map.Query method: 1, ContainsKey (key) to detect whether the map contains the specified key Key-value mapping 2, Containsvalue (value) to detect whether the map contains the key-value of the specified value 3, IsEmpty () detects if map is empty 4, size () returns the number of items in the mapThe method of converting map to set: 1, KeySet () calls Map.keyset () takes all the keys in the map to form a set 2, values () call Map.values () that co
the keys whose Hashtable contains the same ing key value:Map tmp = new HashMap (h1 );
Tmp. entrySet (). retainAll (h2.entrySet ());
Set result = tmp. keySet ();
6.6 Arrays. toString (a) print each element of the array
6.7 Arrays. equals (a1, a2) compares the two Arrays and whether each element is equal.
7. Usage exception
7.1 checked exception)
By throwing a checked exception, the caller is forced to hand
The following methods apply to any map implementation (HASHMAP, TreeMap, Linkedhashmap, Hashtable, and so on):Mode one (recommended):1 //Recommended2 //use entries in the For-each loop to traverse3 //Note: The For-each loop is introduced in Java 5, so the method can only be applied to Java 5 or later. 4 //If you traverse an empty map object, the For-each loop will throw nullpointerexception, so you should always check for null references before traversing. 5 Private Static voidTestMethod1 (Mapma
1 Packagemap_show;2 /*3 * Map--properties4 */5 6 7 8 /*9 * Map Interface: A collection of "key-value pairs" with a mapping relationship---similar to high school "functions"Ten * One *------Map Method: A * - ? Add, delete operations: - ---> Object put (object Key,object value) associates the specified value with the specified key in this map the void Putall (Map t) - Example: Map.put ("AA", one-to-one); - ---> Object Remove (Object key) - ? void Clear () + - ? Ways to manipulate meta view
removed from the source image, but you cannot add any elements.Set EntrySet (): Returns the view set of the Map.entry object, which is the keyword/value pair in the imageBecause the mappings are unique, you use set support. You can also remove elements from the view, and these elements will be removed from the source image, but you cannot add any elements.6map.entry interfaceThe map's EntrySet () method re
correctly resize the map before adding all the elements, so if you do not adjust the size of the map yourself (which we will briefly describe), Putall () may be more efficient than expected.View MapThe elements in the iteration Map do not have a straightforward method. If you are querying a map to see which elements satisfy a particular query, or if you want to iterate over all of its elements, regardless of the cause, you first need to get the view of the map. There are three possible views (s
resize the map before adding all the elements, so if you do not adjust the size of the map yourself (which we will briefly describe), Putall () may be more efficient than expected.View MapThe elements in the iteration Map do not have a straightforward method. If you are querying a map to see which elements satisfy a particular query, or if you want to iterate over all of its elements, regardless of the cause, you first need to get the view of the map. There are three possible views (see table 3
: TreeSet 95 No: HashSet 96 do not know, use HashSet 97 98 No: Li St 99 to be safe: 100 Yes: Vector101 No: ArrayList or LinkedList102 Delete more: LinkedList103 query more: ArrayList104 do not know, use ArrayList105 don't know, just use ArrayList106 107 3: Common methods of collection and Traverse mode 108 collection:109 Add () 110 Remove () 111 contains () iterator () 113 size () 114 115 times calendar: 116 Enhanced for117 Iterato
in the For-each loop.If you only need the keys or values in the map, you can implement the traversal through keyset or values instead of using EntrySet.[Java]View PlainCopy
Mapnew Hashmap
Traverse a key in a map
For (Integer Key:map.keySet ()) {
System.out.println ("key =" + key);
}
Traversing values in a map
For (Integer value:map.values ()) {
System.out.println ("value =" + value);
}
This method is sli
non-empty . In order to work efficiently, the class defining the key must implement the Hashcode () method and the equal () method . This class is an inheritance of the previous Java implementation and is usually better used in other classes that implement the image.Select TreeMap when the order of the elements is important, and use hashmap when the elements do not have to be stored in a particular order. The use of Hashtable is not recommended because HashMap provides all the similar features
CompareTo () method and the Equals () method. Thread is not secure.
Java.util.Queue interface: Contains Arraydeque, Delayedquene, Priorityqueue and other implementation classes, not commonly used.
Java.util.Map Interface : Represents the root interface of a key-value pair, and defines methods such as put (), get (), EntrySet (), KeySet (), and values () for the operation of many key-value pairs.
HashMap class
Map is an interface in Java Public InterfaceMap.entry is an internal interface to map Interface entryMap provides a number of common methods, such as keyset (), Values,entryset (), etc.The KeySet () method returns a collection of key values in the Map and EntrySet () returns a set set, but the collection type is map.entrySetKeySet () returns the collection of keysSetVALUES () returns a collection of valueC
11111401In Java, map based on the underlying data structure, there are many different implementations, such as hash HashMap, linked list linkedmap, hash list linkedhashmap, tree table (binary tree) TreeMap and so on.This article discusses several different ways of traversing the most commonly used hashmap hash lists in our programming, as well as the comparison between the methods of writing and efficiency.First, prepare a map hash table and store 100,000 records.Key is starting from Key0 to key
();//Gets the set set of all keys for the map collection, KeySet ()IteratorGet iteratorswhile (It.hasnext ()) {String key = It.next ();String value = Map.get (key);Print key and value valuesSystem.out.println (key+ "=" +value);} ②. entryset () set entry . GetKey (), entry . GetValue () Two methods to take key and value. The entry interface is returned, . The typical usage of is as follows: map Map.put ("Key1", "value1");
over the elements of the map being passed, in addition to the algorithm that the iteration put () is running to add each key-value pair to the map. It should be noted, however, that Putall () can correctly adjust the size of the map before joining all elements, so if you do not adjust the size of the map yourself (which we will introduce), Putall () may be more efficient than expected. View MapThe elements in the iteration Map do not have a straightforward method. Suppose you want to query a ma
returns a set set that contains all the keys.Traversal process: First get all the keys of the collection, traverse the collection, according to the key to get all the values.1 PackageP01.traverseDemo01.keySetDemo;2 3 ImportJava.util.HashMap;4 ImportJava.util.Iterator;5 ImportJava.util.Map;6 ImportJava.util.Set;7 8 Public classKeysetdemo {9 Ten Public Static voidMain (string[] args) { One Demo01 (); A } - - Private Static voidDemo01 () { themapNewHashmap(); -Map.put (1, "Wangcai"
over the elements of the map being passed in addition to the algorithm that the iteration put () runs to add each key-value pair to the map. It should be noted, however, that Putall () correctly adjusts the size of the map before it joins all elements, so if you do not adjust the size of the map yourself (which we will introduce), Putall () may be more efficient than expected. View MapThe elements in the iteration Map do not have a straightforward method. Suppose you want to query a map to see
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.