Page 1 of 1

About Question enthuware.ocpjp.v8.2.1564 :

Posted: Sun May 21, 2017 7:30 pm
by lenalena
Comment for the right option states:
Note that insertion order is not affected if a key is re-inserted into the map.
By "re-inserted" - does it mean just put again, or removed and then put again? Because I'm not sure how the insertion order is maintained if an item is removed and then inserted - wouldn't the backing linked list redirect its pointers to the before/after elements after an element is removed?

Re: About Question enthuware.ocpjp.v8.2.1564 :

Posted: Sun May 21, 2017 9:56 pm
by admin
It means that if the map already has a key mapped to a value and if you put a new value for the same key, the order doesn't change. Here is an example:

Code: Select all

  LinkedHashMap lhm = new LinkedHashMap();
  lhm.put("2", "2");
  lhm.put("1", "1");
  lhm.put("5", "5");
  lhm.put("3", "3");
  System.out.println(lhm); //prints {2=2, 1=1, 5=5, 3=3}
  //lhm.remove("2"); //If you uncomment this line the output of the next line will be 
//{1=1, 5=5, 3=3, 2=22} Order changes.
  lhm.put("2", "22"); //prints {2=22, 1=1, 5=5, 3=3} No change in order.

Re: About Question enthuware.ocpjp.v8.2.1564 :

Posted: Mon May 22, 2017 9:16 am
by lenalena
Got it, thanks!