Map和list<map>的几种遍历方法
public static void main(String[] args) { Map<String, Object> map = new LinkedHashMap<String, Object>(); map.put("username","wangking"); map.put("password", "12345"); map.put("email", "12345@qq.com"); map.put("sex", "男"); //第一种 用for循环的方式 for (Map.Entry<String, Object> m :map.entrySet()) { System.out.println(m.getKey()+"\t"+m.getValue()); } //利用迭代 (Iterator) Set set=map.entrySet(); Iterator iterator=set.iterator(); while(iterator.hasNext()){ Map.Entry<String, Object> enter=(Entry<String, Object>) iterator.next(); System.out.println(enter.getKey()+"\t"+enter.getValue()); } //利用KeySet 迭代 Iterator it = map.keySet().iterator(); while(it.hasNext()){ String key; String value; key=it.next().toString(); value=(String) map.get(key); System.out.println(key+"--"+value); } //利用EnterySet迭代 Iterator i=map.entrySet().iterator(); System.out.println( map.entrySet().size()); String key; String value; while(i.hasNext()){ Map.Entry entry = (Map.Entry)i.next(); key=entry.getKey().toString(); value=entry.getValue().toString(); System.out.println(key+"===="+value); } System.out.println(getKeyByValue(map, "zhaokuo")); } public static String getKeyByValue(Map map, Object value) { String keys=""; Iterator it = map.entrySet().iterator(); while (it.hasNext()) { Map.Entry entry = (Entry) it.next(); Object obj = entry.getValue(); if (obj != null && obj.equals(value)) { keys=(String) entry.getKey(); } } return keys; }
2.
Map<String,String> map=new HashMap<String,String>(); map.put("1","hello"); map.put("2","name"); map.put("3","tom"); for(String key:map.keySet()){ System.out.println("value值为:"+map.get(key)); }
3.
Map map=new HashMap(); map.put("1","hello"); map.put("2","name"); map.put("3","tom"); Object obj=new Object(); String wa; obj=map.get("1"); wa=(String) map.get("2"); System.out.println(obj); System.out.println(wa);
List<Map>遍历方法
List<Map<String,Object>> list = new ArrayList<Map<String, Object>>(); Map<String,Object> map1 = new HashMap<String, Object>(); List<String> list1 = new ArrayList<String>(); List<String> list2 = new ArrayList<String>(); List<String> list3 = new ArrayList<String>(); List<String> list4 = new ArrayList<String>(); list1.add("添加的Object-->a"); list2.add("添加的Object-->b"); list3.add("添加的Object-->c"); list4.add("添加的Object-->d"); map1.put("a",list1); map1.put("b",list2); list.add(map1); Map<String,Object> map2 = new HashMap<String, Object>(); map2.put("c",list3); map2.put("d",list4); list.add(map2); for (Map<String,Object> map:list ) { for (String s:map.keySet() ) { System.out.print("key:"+s+"\t"); System.out.println("value:"+map.get(s)); } } System.out.println("======================华丽的分割线============================="); for (int i = 0; i < list.size(); i++) { Map<String, Object> map = list.get(i); Iterator it = map.keySet().iterator(); while (it.hasNext()) { String str = (String) it.next(); System.out.print("key:"+str+"\t"); System.out.println("value:"+map.get(str)); } } System.out.println("========================华丽的分割线================================"); for (Map<String, Object> map : list) { for (Map.Entry<String, Object> m : map.entrySet()) { System.out.print("key:"+m.getKey() + " "); System.out.println("value:"+m.getValue()); } }
下面这个是运行结果:
key:a value:[添加的Object-->a] key:b value:[添加的Object-->b] key:c value:[添加的Object-->c] key:d value:[添加的Object-->d] ======================华丽的分割线============================= key:a value:[添加的Object-->a] key:b value:[添加的Object-->b] key:c value:[添加的Object-->c] key:d value:[添加的Object-->d] ========================华丽的分割线================================ key:a value:[添加的Object-->a] key:b value:[添加的Object-->b] key:c value:[添加的Object-->c] key:d value:[添加的Object-->d]
JAVA修炼塔,技术世界的探知与交流,欢迎你的加入-----群号:535296702