在java的Map集合中,怎样更改value的值

版权声明:本文为博主原创文章,未经博主允许不得转载。https://blog.csdn.net/chenyao1994 https://blog.csdn.net/chenyao1994/article/details/82981642
import java.util.HashMap;

import java.util.Map;



public class Demo {

    public static void main(String[] args) {

        Map<Integer,Integer> m = new HashMap<Integer,Integer>();

        m.put(1, 1);

        print(m);

        m.put(1, 2);

        print(m);

    }



    private static void print(Map<Integer, Integer> m) {

        for(Map.Entry<Integer, Integer> mm : m.entrySet())

            System.out.println("K: "+mm.getKey()+",V: "+mm.getValue());

    }

}



//供参考,重新放一个相同的key,会自动覆盖value的。

猜你喜欢

转载自blog.csdn.net/chenyao1994/article/details/82981642