Import Java. util. *; import Java. util. map. entry; class person implements comparable {public string name; Public int age; Public Person (string name, int age) {This. name = Name; this. age = age;} Public String getname () {return name;} public
@ Testpublic void test7 () {Map map = new hashmap (); map. put (100, "Jack"); map. put (200, "Marry"); map. put (300, "Sisi"); // converts a map set to a set, and stores the map key value set in the Set set. set = map. keyset (); iterator it =
1. ElaborationFor the way in which map is traversed in Java, many articles recommend using EntrySet, which is more efficient than keyset. The reason is: The EntrySet method gets all the keys and value at once, and keyset gets only the set of keys, and for each key, it goes to the map to find the value in extra time, thus reducing the overall efficiency. So what's the real story?To understand the real differ
1. ElaborationFor the way in which map is traversed in Java, many articles recommend using EntrySet, which is more efficient than keyset. The reason is: The EntrySet method gets all the keys and value at once, and keyset gets only the set of keys, and for each key, it goes to the map to find the value in extra time, thus reducing the overall efficiency. So what's the real story?To understand the real differ
Original: http://www.cnblogs.com/fczjuever/archive/2013/04/07/3005997.html1. ElaborationFor the way in which map is traversed in Java, many articles recommend using EntrySet, which is more efficient than keyset. The reason is: The EntrySet method gets all the keys and value at once, and keyset gets only the set of keys, and for each key, it goes to the map to find the value in extra time, thus reducing the
1. To elaborate
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. What about the actual situation?
To understand the real difference
Http://www.cnblogs.com/fczjuever/archive/2013/04/07/3005997.html1. ElaborationFor the way in which map is traversed in Java, many articles recommend using EntrySet, which is more efficient than keyset. The reason is: The EntrySet method gets all the keys and value at once, and keyset gets only the set of keys, and for each key, it goes to the map to find the value in extra time, thus reducing the overall ef
1. Elaboration For the way in which map is traversed in Java, many articles recommend using EntrySet, which is more efficient than keyset. The reason is: The EntrySet method gets all the keys and value at once, and keyset gets only the set of keys, and for each key, it goes to the map to find the value in extra time, thus reducing the overall efficiency. So what's the real story?To understand the real diffe
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 efficie
Abstractmap is an abstract class that is a skeleton implementation of the map interface, minimizing the abstraction functions provided by this interface. In the Java collection framework basically follows this rule, skeleton implementation between the interface and implementation class to build a layer of abstraction, the purpose is to reuse some of the more common functions and convenient extension, such as the list interface has skeleton implementation abstractlist, The set interface has skele
A deep understanding of the implementation principles of HashMap in Java
HashMap inherits from the abstract class AbstractMap. The abstract class AbstractMap implements the Map interface. The diagram is as follows:
Map in Java
The interface allows us to use an object as the key, that is, we can use an object as the key to find another object.
Before discussing the implementation principle of HashMap, we first implemented a SimpleMap class, which inherits from the AbstractMap class. The spec
contains a key.public boolean ContainsKey (Object key) { return getentry (key)! = NULL;}ContainsKey () First obtains the entry of the key by Getentry (key)and then determines whether the entry is null.The source code of Getentry () is as follows:View CodeThe function of Getentry () is to return the key value pair of key , which has been explained in the source code.It should be emphasized here that theHashMap "key is null" is placed in the table position 0 , that is, table[0], "key is not NU
("unchecked") Entry
E = (Entry
) Tab [index]; for (Entry
Prev = null; e! = Null; prev = e, e = e. next) {if (e. hash = hash) e. key. equals (key) {modCount ++; if (prev! = Null) {prev. next = e. next;} else {tab [index] = e. next;} count --; V oldValue = e. value; e. value = null; return oldValue;} return null;}/*** Add the elements in the Map */public synchronized void putAll (Map
T) {
Foreword: If the traversal of the collection of Java (mainly hashmap in the keyset () and EntrySet () is how to value and can implement traversal) is not very clear, interested in-depth understanding of the small partners, this article can be used as a reference, due to the reason of time, It focuses on the core code of its traversal, the analysis of the underlying iterator. If the traversal of the collection has no basic understanding of the iterator
For HashMap, Hashtable, TreeMap, linkedhashmap of the internal sorting, found that a lot of people on the internet have misunderstood.
For example, some people think:
Hashtable.keyset () Descending
Treemap.keyset () Ascending
Hashmap.keyset () Disorderly order
Linkedhashmap.keyset () Original sequence
Some people think that the difference between keyset and EntrySet is caused by. So I'm going to go through two traversal ways to give you a test.
Put a
Cyclic traversal of map The code is as follows:Copy code Package com. sec. map; Import java. util. HashMap;Import java. util. Iterator;Import java. util. Map; Public class TestMap { Public static void main (String [] args ){ Map Map. put ("1", "value1 ");Map. put ("2", "value2 ");Map. put ("3", "value3 "); // The first type is commonly used. The second value is used.System. out. println ("traverse key and value through Map. ke
comparison return num = 0? This. name. compareTo (o. name): num; // name is a secondary condition for comparison}
B. Comparator sorting: allows the set to have a comparison, implements the Comparator of the Comparator interface, and overwrites the compare method. The class object is passed as an actual parameter to the constructor of the TreeSet set (namely, the anonymous internal class method ).
TreeSetts = new TreeSet
Double row set:
Map (no iterator ):
Ensure the uniqueness of the middle k
is as follows:
Public interface Map
{// Query Operations int size (); boolean isEmpty (); boolean containsKey (Object key); boolean containsValue (Object value); V get (Object key ); // Modification Operations V put (K key, V value); V remove (Object key ); // Bulk Operations/* The behavior of this operation is undefined if the specified map is modified while the operation is in progress. */void putAll (Map
M); void clear (); // Views Set
KeySet (); // because the keys of the Map
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.