the map collection is turned into a set set, an iterator can be used. The reason why the set is transferred is because the map set has the uniqueness of the key, in fact, the set set is derived from the Map,set set at the bottom of its practical is the map method.3.1The key in the map collection can be removed and stored in the set collection. Iterates over a set set. The iteration is completed, and then the value of the corresponding key is obtained by the Get method.Set keyset = Map.keyset ()
, let us continue to look down, the first thing to say is hashmapentry internal static class, the source code is as follows:Static Class HashmapentryThe hashmapentry class implements the entry interface, and the entry interface is an internal interface inside the map interface. By implementing the entry interface, the HashMap has getkey/getvalue/setvalue and other related functions. At the same time we can see hashmap inside the good multi-functional implementation are aimed at hashmapentry exp
returned by this method is that the data is emitted in a disorderly order.Typical usage is as follows:Map map = new HashMap ();Map.put ("Key1", "LISI1");Map.put ("Key2", "Lisi2");Map.put ("Key3", "Lisi3");Map.put ("Key4", "Lisi4");Gets the set set of all keys for the map collection first, keyset ()Iterator it = Map.keyset (). Iterator ();Get iteratorswhile (It.hasnext ()) {Object key = It.next ();System.out.println (Map.get (key));}Second type:entryset ()SetTypical usage is as follows:Map map =
1. Import Package Fastjson-1.2.2.jar2.JSP file added 3. Usage// List ${id}// Map The properties of the JSTL expression cannot have spaces, traverse entryset (),EntrySet similar to KeyValuePair, and map similar to dictionary ${entry.key}:${entry.value}Usage of the Java Web EL jstl
;
LinkedList class: Sequential access is optimized, and the cost of inserting and deleting to the list is small, and random access is relatively slow.
1.3 Queue Interface
The queue is used to simulate the data structure of queues such as "FIFO". Typically, queues do not allow random access to elements in the queue.
Arraydeque Class: The implementation class for the queue sub-interface deque, array way.
LinkedList class: Is the implementation class of the list interface,
Map is a key-value pair. is also a frequently used data structure.The map interface defines the basic behavior of the MAP. The method that contains the most core get and put operations, defined by this interface, is as Follows:There are different map implementations in the jdk, which are suitable for different application scenarios. such as Thread-safe Hashtable and Non-thread-safe hashmap.For example, the UML class diagram is a subclass of the map interface in the jdk, and there is a special ca
The main three methods:1, KeySet (), gets the key of the map and returns the set view.2. VALUES () Gets the value of the map.3, EntrySet (). Recommended, because this method preserves the properties of key-value pairs. Packagecd.itcast.runble;Importjava.util.Collection;ImportJava.util.HashMap;ImportJava.util.Iterator;ImportJava.util.Map;ImportJava.util.Map.Entry;ImportJava.util.Set;/*** Double-row set map, traverse * 1, KeySet (); * 2, values (); * 3,
. Main () " + Maps. Get ());} View code The third method to traverse the map set // Method 3 for Traversing hashmap Map New Hashmap (); Maps. Put ( " A " , " Shang Xiaofei " ); Maps. Put ( " B " , " Shang xiaoshuai " ); Maps. Put ( " C " , " Shang Xiaoyan " ); Maps. Put ( " A " , " Sdafasd " ); Set Maps. entryset (); For (Iterator = Entryset. iterator (); iterator. ha
Map indicates the ing relationship, which is a pair of (Key, value) relationships.
Duplicate key values are not allowed. The relationship between key and value is many-to-one, that is, each value corresponds to a unique key.
Map. entry is the internal structure of the map interface, which is used to save key-value content.
Import Java. util. hashmap; import Java. util. map; import Java. util. set; public class mapdemo {public static void main (string [] ARGs) {map = new hashmap (); map. put (
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. keyset :");For (string key: map. keyset ()){System. Out. println ("Key =" + key + "and value =" + map. Get (key ));}// Type 2System. Out. println ("Traverse Key and value through map. entryset using iterator :"
Enhance the For Loop
1. The for loop can do things, and most of the enhancement for loops can be done (if you want to obtain the lower mark, you must use a simple for loop)
2. Enhanced for can easily process the traversal of a set, but the standard traversal of a set uses the iterator.
3. It shows its strength in the Collection framework.
Map interface Traversal
Map interface traversal:1. The first method uses the map. Values () method. First, the value is put in a collection.2. The second me
[Java class set] _ notes for using the map interface
Objectives of this chapter:
Master the output operations of the map interfaceMeasure the test taker's knowledge about key class definition standards in the map interface.
Map interface output
For map interfaces, iterations (such as iterator and foreach) cannot be directly used for output, because each location in map stores a pair of values (key-> value), only one value can be found in iterator at a time. Therefore, if you need to use Iteratio
containers are dangerous. We need to find the indirect layer for operations.
We noticed that the above method only maps the key, so we still need to use the get () method. However, it is not required if it is entryset (), because it maps the collection view containing key-value.
Set entries =Map. entryset ();If(Entries! =Null) {Iterator=Entries. iterator ();While(Iterator. hasnext () {map. Entry entr
; Import Java. util. hashset; Import Java. util. iterator; Import Java. util. Map; Import Java. util. Map. entry; Public Class Hashsettest { Public Static Void Main (string [] ARGs) {Map New Hashmap (); M. Put ( New Student (1, "zhangsan"), "AA" ); M. Put ( New Student (1, "zhangsan"), "BB" ); M. Put ( New Student (2, "differentiated"), "cc" ); System. Out. println ( New Student (1, "zhangsan") = New Student (1, "zhangsan" ); System. Out. println ( New Student (1, "zhangsa
key): determines whether the object exists based on the key value. If the object does not exist, null is returned.
Size (): map size
Values (): returns all values.
Entryset ()
Keyset ()
Two unique retrieval methods in map: entryset () and keyset ()
1, set
Store all the keys in the map to the Set set. Because set has an iterator.
All the keys that can be retrieved iteratively are obtained according to the
Are you tired of getting keywords from map every time and getting the corresponding values? Using the map. Entry class, you can get all the information at the same time. The standard MAP access method is as follows:
Set keys = map. keyset ();If (Keys! = NULL ){Iterator = keys. iterator ();While (iterator. hasnext ()){Object key = iterator. Next ();Object value = map. Get (key );;....;}}
Then there is a problem with this method. After obtaining keywords from map, we must repeat the returned v
Conversion] Two Methods of hashmap traversal in Java: http://www.javaweb.cc/language/java/032291.shtmlfirst: 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 ();} is highly efficient. You must use this method later! Type 2: Map map = new hashmap (); iterator iter = map. keyset (). iterator (); Whil
, and replaces the modulo ---- the default size of the hash array in Hashtable is 11, the added method is old * 2 + 1. The default size of the hash array in HashMap is 16, and it must be an index of 2. ---- Both Hashtable and HashMap use Iterator. For historical reasons, Hashtable also uses the Enumeration method:
Map map = new HashMap (); map. put ("a", "100"); map. put ("B", "200"); map. put ("c", "200"); Iterator iter = map. entrySet (). iterator (
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.