Map Query and Modification methods

Source: Internet
Author: User

How to 1:map queries

 PackageCom.cn.util;Importjava.util.ArrayList;ImportJava.util.HashMap;ImportJava.util.Iterator;Importjava.util.List;ImportJava.util.Map;ImportJava.util.Set;//a method of looping through a map Public classUtil { Public Static voidMain (string[] args) {Map<string, integer> tempmap =NewHashmap<string, integer>(); Tempmap.put ("A", 1); Tempmap.put ("B", 2); Tempmap.put ("C", 3); //in JDK1.4//Traversal method One HashMap entryset () traversalSystem.out.println ("Method One")); Iterator it=Tempmap.entryset (). iterator ();  while(It.hasnext ()) {Map.entry Entry=(Map.entry) it.next (); Object Key=Entry.getkey (); Object value=Entry.getvalue (); System.out.println ("key=" + key + "value=" +value); } System.out.println (""); //JDK1.5, apply new features For-each loop//Traversal Method TwoSystem.out.println ("Method Two");  for(Map.entry<string, integer>Entry:tempMap.entrySet ()) {String key=Entry.getkey (). toString (); String value=Entry.getvalue (). toString (); System.out.println ("key=" + key + "value=" +value); } System.out.println (""); //Traversal Method Three HashMap KeySet () traversalSystem.out.println ("Method three");  for(Iterator i =Tempmap.keyset (). iterator (); I.hasnext ();) {Object obj=I.next (); System.out.println (obj);//Loop Output KeySystem.out.println ("key=" + obj + "value=" +tempmap.get (obj)); }         for(Iterator i =tempmap.values (). iterator (); I.hasnext ();) {Object obj=I.next (); System.out.println (obj);//Loop Output Value} System.out.println (""); //Traversal method four TreeMap KeySet () traversalSystem.out.println ("Method Four");  for(Object o:tempmap.keyset ()) {System.out.println ("key=" + O + "value=" +tempmap.get (o)); } System.out.println ("11111"); //how Java traverses map <string, arraylist> map = new HashMap <string,//arraylist> ();System.out. println ("Java Traversal map <string, arraylist> map = new HashMap <string, arraylist> ();"); Map<string, arraylist> map =NewHashmap<string, arraylist>(); Set<String> keys =Map.keyset (); Iterator<String> iterator =Keys.iterator ();  while(Iterator.hasnext ()) {String key=Iterator.next (); ArrayList ArrayList=Map.get (key);  for(Object o:arraylist) {System.out.println (o+ "Traversal Process"); }} System.out.println ("2222"); Map<string, list> maplist =NewHashmap<string, list>();  for(Map.entry entry:mapList.entrySet ()) {String key=Entry.getkey (). toString (); List<String> values =(List) entry.getvalue ();  for(String value:values) {System.out.println (key+ "-+" +value); }        }    }}

Query methods for 2:map

 PackageCom.cn.util;ImportJava.util.HashMap;ImportJava.util.Map;ImportJava.util.Set; Public classMaptest { Public Static voidTest1 () {Map<string, integer> m =NewHashmap<string, integer>(); M.put ("1", 20); M.put ("2", 30); Set<String> s = m.keyset ();//get the Key collection         for(String str:s) {m.put (str, (int) (M.get (str) * 1.3));//value in key} System.out.println (M.get ("1" + "+" + m.get ("2"))); }     Public Static voidMain (string[] args) {Test1 (); }}

Map Query and Modification methods

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.