سلام دوستان
چیزی که من تست کردم و متوجه شدم داخل HashMap و LinkedHashMap درصورتی که Duplicated Key داشته باشیم، آخرین key,value در حافظه ذخیره میشه. و درواقع مقدار keyهای جدید روی keyهای مشابه قدیمی اوررایت میشه.
مثال :
@Test
void Should_Have_unique_keys_and_can_have_duplicated_values() {
Map<String,String> geometrics = new HashMap<>();
geometrics.put("Mosalas","Ghermez");
geometrics.put("Mostatil","Zard");
geometrics.put("Sheshzelei","Sabz");
geometrics.put("Moraba","Ghermez"); // Duplicated Key is not allowed
geometrics.put("Moraba","red"); // Duplicated Key is not allowed
geometrics.put("Moraba","blue"); // last Duplicated Key is allowed
Set<Map.Entry<String, String>> entries = geometrics.entrySet();
for (Map.Entry<String, String> entry : entries) {
String key = entry.getKey();
String value = entry.getValue();
System.out.println("Key is : " + key + " , Value is :" + value);
}
}
//output :
/*
Key is : Mosalas , Value is : Ghermez
Key is : Mostatil , Value is : Zard
Key is : Moraba , Value is : blue ***
Key is : Sheshzelei , Value is : Sabz
*/
دوستان اگر چیزی که دارم میگم اشتباه، لطفا اصلاح کنن.