编程知识 cdmana.com

Four ways of Java traversing map

package cn.jdbc.test;import java.util.HashMap;import java.util.Iterator;import java.util.Map;import java.util.Map.Entry; /** * Map  Iterated set  * @data 2020.11.12 * */public class TestMap {   public static void main(String[] args) {    Map<String, Object> map = new HashMap<String, Object>();    map.put("aaa", 111);    map.put("bbb", 222);    map.put("ccc", 333);

//Map Collection loop traversal mode 1 System.out.println(" The first 1 Kind of : adopt Map.keySet() Traverse key and value:"); for(String key:map.keySet()){//keySet obtain map aggregate key Set And then I'm going through key obtain value that will do String value = map.get(key).toString();// System.out.println("key:"+key+" vlaue:"+value); } //Map Set loop traverses two By means of iterators System.out.println(" The first 2 Kind of : adopt Map.entrySet Use iterator Traverse key and value:"); Iterator<Entry<String, Object>> it = map.entrySet().iterator(); while(it.hasNext()){ E.........

版权声明
本文为[Irving the procedural ape]所创,转载请带上原文链接,感谢
https://cdmana.com/2020/12/20201225120653053g.html

Scroll to Top