TreeMap遍历key和value

来源:互联网 发布:名匠装饰怎么样知乎 编辑:程序博客网 时间:2024/05/21 10:42
package test;import java.util.*;public class Pillar {    public static void main(String[] args) {        Map<String,Integer> map=new TreeMap<String,Integer>();        map.put("Kobe", 24);        map.put("James", 6);        map.put("Durant",35);        map.put("Anthony",7);        if(map.containsKey("Kobe"))        {            System.out.println(map.get("Kobe"));        }        Set<String> keySet = map.keySet();        Iterator<String> iter = keySet.iterator();        //Iterator<String> iter = map.keySet().iterator();        while(iter.hasNext())        {            String str=iter.next();            System.out.println(str+"="+map.get(str));        }    }}

不能从Collection collection=map.values();
Iterator iter = collection.iterator();
因为这个collection《V》只能返回的是值(V);
方法1:Set keySet = map.keySet();
Iterator iter = keySet.iterator();
方法2:Iterator iter = map.keySet().iterator();

1 0
原创粉丝点击