java中的hashmap key可以重复吗?

可以重复,但是重重以后的key会被覆盖

1570764537212

public static void main(String[] args)
{
    System.out.println("dd  World!");

    Map<Integer, Integer> map = new HashMap<>();
    int[] nums=new int[] {1,2,3,1};
    for (int i = 0; i < nums.length; i++) {
        map.put(nums[i], i);
    }

    System.out.println("dd  World!");
}

但是c#中的hashtable ,dictionary key值是不能重复的。 会直接编译不通过

1570764605178

Dictionary<int, int> dic = new Dictionary<int, int>();
dic.Add(0, 0);
dic.Add(1, 1);
dic.Add(2, 2);
dic.Add(2, 2);
Console.WriteLine(111);
Console.ReadKey();

1570764723616


本文由 hcb 创作,采用 知识共享署名 3.0,可自由转载、引用,但需署名作者且注明文章出处。

还不快抢沙发

添加新评论