Obtain a value in the map and traverse the map to obtain the values of all keys and values in the map.

Source: Internet
Author: User


1. Declare a map: Map map = new hashmap ();
2. Place values in map. Note: map is stored in the form of key-value. For example:

Map. Put ("SA", "dd ");

3. Select string STR = map. Get ("sa"). tostring () from map. The result is str = "DD ";
4. traverse a map to obtain the key and Value
Map map = new hashmap ();

Iterator it = map. entryset (). iterator ();
While (it. hasnext ())
Map. Entry entry = (Map. Entry) it. Next ();
Object key = entry. getkey ();
Object value = entry. getvalue ();

The Java code is as follows:

package Test01;import java.util.HashMap;import java.util.Iterator;import java.util.Map;public class Test03 {public static void main(String[] args){a();b();}@SuppressWarnings("unchecked")public static void a(){@SuppressWarnings("rawtypes")Map map = new HashMap();map.put("1","aa");map.put("2","bb");map.put("3","cc");map.put("4","dd");map.put("5","ee");map.put("6","ff");map.put("7","gg");String str = map.get("5").toString();System.out.println(str);}@SuppressWarnings("unchecked")public static void b(){@SuppressWarnings("rawtypes")Map map = new HashMap();map.put("1","aa");map.put("2","bb");map.put("3","cc");map.put("4","dd");map.put("5","ee");map.put("6","ff");map.put("7","gg");@SuppressWarnings("rawtypes")Iterator it = map.entrySet().iterator() ;while (it.hasNext()){@SuppressWarnings("rawtypes")Map.Entry entry = (Map.Entry) it.next() ;Object key = entry.getKey() ;Object value = entry.getValue() ;System.out.print("["+key+"、");System.out.print(value+"]");System.out.print(",");}}}


After the code is run, the effect is as follows:


Obtain a value in the map and traverse the map to obtain the values of all keys and values in the map.

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.