Map

import org.omg.CosNaming.NamingContextExtPackage.StringNameHelper;

import java.util.*;

public class Main {

    public static void main(String[] args) {

        Map maps = new HashMap<>();
        maps.put(1,"张三");
        maps.put(2,"李四");
        maps.put(1,"王五");
        maps.put(null,"张三");

        //通过Iterator来遍历
        Set set = maps.keySet();
        Iterator iterator = set.iterator();
        while (iterator.hasNext()){
            Integer key = iterator.next();
            System.out.println("Key:"+key+" values:"+maps.get(key));
        }

        //通过Map.Entry来遍历,可以修改key和values值
        Set> set1 = maps.entrySet();
        Iterator> iterator1 = set1.iterator();
        while (iterator1.hasNext()){
            Map.Entry entry = iterator1.next();
            System.out.println("key:"+entry.getKey()+ " values:"+entry.getValue());
        }

    }
}

你可能感兴趣的:(Map)