Java code to Prevent duplicate pairs in HashMap/HashTable

前端 未结 7 2071
梦谈多话
梦谈多话 2021-01-17 02:32

I have a HashMap as below (assuming it has 10,0000 elements)

HashMap hm = new HashMap();
hm.put(\

7条回答
  •  情歌与酒
    2021-01-17 02:42

    List keys = new ArrayList(); (1000000)
    List values = new ArrayList(); (1000000)
    Map map = new HashMap();
    int i =0;
    for(String key : keys){
      String returnedValue = map.put(key, values.get(i));
      if(returnedValue!=null){
             map.put(key, returnedValue);
             system.out.println("Duplicate key trying to be entered with new value so   reverting the duplicate key ="+key+"new Value"+values.get(i));
      }
    }
    

提交回复
热议问题