Map interface traversal map in Java

Source: Internet
Author: User

The Java Collection framework is used to store data, also known as collection classes

Located under the Java.util package

Common interfaces and classes under the Java.util package

Collection and map are the root interfaces of the Java Collection framework

The list collection is an ordered collection, and the elements in the collection can be repeated, and accessing the elements in the collection can be accessed based on the index of the element.

The set collection is an unordered collection, and the elements in the collection cannot be duplicated, and access to the elements in the collection can only be accessed based on the element itself (and the reason that the elements in the collection are not allowed to be duplicated).

The map collection holds elements of the key-value pair form, which can only be accessed by the key of each element when accessing its value.

Map interface

The map interface is not an inheritance of the collection interface. The map interface is used to maintain key/value pairs (Key/value pairs). This interface describes a key-to-value mapping that is never duplicated.

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, that is, any moment can have multiple threads simultaneously write hashmap;

may result in inconsistent data. If synchronization is required, you can use the collections Synchronizedmap method to make the HashMap capable of synchronizing.

TreeMap not only can keep order, but can also be used for sorting

Map and collection:
Map and collection are juxtaposed in the set frame.
The map stores the key-value pairs
Map storage elements using the Put method, collection using the Add method
The map collection does not take the element directly out of the method, but instead turns it into a set set, where the element is fetched by iteration
The key in the map collection is guaranteed to be unique

Common methods:

Add: Put (K key, V value) associates the specified value with the specified key in this map
Putall (map<? extends K,? extends v> m) copy all mappings from the specified mappings into this map

import Java.util.HashMap;    Import Java.util.Map; /** * * Map Collection Add **/   Public classMapdemo { Public Static voidMain (string[] args) {Map<String,Integer> m =NewHashmap<string,integer>(); M.put ("Zhangsan", +); M.put ("Lisi", the); M.put ("Wangwu", +); M.put ("Lisi", -); M.put ("Hanmeimei",NULL); System. out. println (m); }  }  

Delete: Clear () removes all mappings from this map

Remove (Object key) If there is a mapping relationship for a key, remove it from this mapping

import Java.util.HashMap;    Import Java.util.Map; /** * * Map Collection Delete **/   Public classMapdemo { Public Static voidMain (string[] args) {Map<String,Integer> m =NewHashmap<string,integer>(); M.put ("Zhangsan", +); M.put ("Lisi", the); M.put ("Wangwu", +); M.put ("Lisi", -); M.put ("Hanmeimei",NULL); System. out. println (m); System. out. println (M.remove ("Wangwu"));          M.clear (); System. out. println (m); }  }  


Judgment: Containsvalue (Object value) returns true if this mapping maps one or more keys to a specified value

ContainsKey (Object key) returns true if this map contains a mapping relationship for the specified key

IsEmpty () returns true if this mapping does not contain a key-value mapping relationship

import Java.util.HashMap;    Import Java.util.Map; /** * * map Set judgment **/   Public classMapdemo { Public Static voidMain (string[] args) {Map<String,Integer> m =NewHashmap<string,integer>(); M.put ("Zhangsan", +); M.put ("Lisi", the); M.put ("Wangwu", +); M.put ("Lisi", -); M.put ("Hanmeimei",NULL); System. out. println (m); System. out. println (M.containskey ("Lisi")); System. out. println (M.containsvalue ( -)); System. out. println (M.isempty ()); }  }  


Get: Get (Object key) returns the value mapped by the specified key, or if the mapping does not contain a mapping for the key.null

Size () returns the number of key-value mapping relationships in this map.

VALUES () returns a collection view of the values contained in this map

EntrySet () returns the set view of the mapping relationships contained in this map.
KeySet () returns the key view of the mapping relationship contained in this map.

import Java.util.HashMap;    Import Java.util.Map; /** * * Map Collection Get **/   Public classMapdemo { Public Static voidMain (string[] args) {Map<String,Integer> m =NewHashmap<string,integer>(); M.put ("Zhangsan", +); M.put ("Lisi", the); M.put ("Wangwu", +); M.put ("Lisi", -); M.put ("Hanmeimei",NULL); System. out. println (m); System. out. println (M.Get("Lisi")); System. out. println (M.size ()); System. out. println (M.values ()); System. out. println (M.entryset ()); Zhangsan= +, hanmeimei=NULL] System. out. println (M.keyset ()); }  }  

Two ways to remove a map collection:
1. Set<k> KeySet: Deposits all the keys in the map into the set collection. Because set has iterators. All the keys that can be iterated out are taken in accordance with the Get method. Gets the value corresponding to each key.
The extraction principle of the Map collection: The Map collection is turned into a set set. is removed by an iterator.
2. Set<map.entry<k,v>> EntrySet: The mapping relationship in the map collection is stored in the set set, and the data type of the relationship is: map.entry
Entry is actually a static internal interface in the map.
Defined internally because only the map collection has a key-value pair, there is a mapping of the key values. A relationship belongs to an internal thing in the map collection. And the object accesses the elements in the map collection directly.

Several scenarios for map traversal:

1. Use for loop

 for (map.entry<string, string> entry:map.entrySet ()) {          System. out. println (Entry.getkey () +"--->"+Entry.getvalue ());}

2. Using iterations

Set = map.entryset ();      Set . iterator ();       while (I.hasnext ()) {      map.entry<string, string> entry1= (map.entry<string, string>) I.next ();    System. out. println (Entry1.getkey () +"= ="+Entry1.getvalue ());}

3. Using keyset () iteration

Iterator it=Map.keyset (). Iterator ();  while (It.hasnext ()) {    String key;    String value;    Key=It.next (). toString ();    Value=map. Get (key);    System. out. println (key+"--"+value);}

Map interface traversal map in Java

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.