Page 1 of 1
					
				About Question enthuware.ocpjp.v8.2.1325 :
				Posted: Sun Jul 31, 2016 7:29 am
				by badbishop
				Though sub-optimal, can't a TreeMap<Object,Instant> be used? Nobody would do such a nonsense in real life, of course, but in theory one can update the timestamp every time a key is accessed, and iterate over entire Map looking for the most ancient value.
			 
			
					
				Re: About Question enthuware.ocpjp.v8.2.1325 :
				Posted: Sun Jul 31, 2016 11:07 am
				by admin
				It is possible but would you do it? The question asks, "Which collection class would you use to store the objects?". And you said nobody would do such nonsense 
Paul.
 
			 
			
					
				Re: About Question enthuware.ocpjp.v8.2.1325 :
				Posted: Wed May 31, 2017 1:03 am
				by safdev
				Explanation:
The LinkedHashMap class maintains the elements in the order of their insertion time. This property can be used to build the required cache as follows:
1. Insert the key-value pairs as you do normally where key will be the object identifier and value will be the object to be cached.
2. When a key is requested, remove it from the LinkedHashMap and then insert it again. This will make sure that this pair marked as inserted latest.
3. If the capacity is full, remove the first element.
Just want to note that 
LinkedHashMap already supports "insertion order" (default) and "access order", with no need for the steps in the explanation. All it takes is calling this ctor to maintain "access order":
Code: Select all
LinkedHashMap(int initialCapacity, float loadFactor, boolean accessOrder=>true)
This way, just calling 
get() (and of course 
put() ) will modify the linked list (typically by moving the most recently accessed entry to the end of the list, while the least recently accessed entries are at the head, to be easily removed in case the cache is full as indicated in the problem statement).
 
			 
			
					
				Re: About Question enthuware.ocpjp.v8.2.1325 :
				Posted: Wed May 31, 2017 1:05 am
				by admin
				Good point. thank you for sharing.
-Paul.
			 
			
					
				Re: About Question enthuware.ocpjp.v8.2.1325 :
				Posted: Wed May 31, 2017 1:15 am
				by safdev
				Thank you, it remains to say that LinkedHashSet, on the other hand, only supports "insertion order", so the steps given in the explanation could be used to maintain "access order" in a LinkedHashSet.
			 
			
					
				Re: About Question enthuware.ocpjp.v8.2.1325 :
				Posted: Fri Aug 03, 2018 11:17 am
				by __JJ__
				safdev wrote: ↑Wed May 31, 2017 1:15 am
Thank you, it remains to say that 
LinkedHashSet, on the other hand, only supports "insertion order", so the steps given in the explanation could be used to maintain "access order" in a LinkedHashSet.
 
LinkedHashSet isn't a Map. From the question:
It should be able to store and retrieve an object when supplied with an object identifier.
That means you're giving some value and getting an object back, which an only be done with a map.
 
			 
			
					
				Re: About Question enthuware.ocpjp.v8.2.1325 :
				Posted: Sat Mar 27, 2021 6:40 am
				by jme_chg
				I tried the following code:
Code: Select all
		LinkedHashMap<Integer, Integer> m = new LinkedHashMap<>();
		m.put(1,1);
		m.put(2,2);
		for(Integer i : m.keySet()) {
		    System.out.print(i + "-" + m.get(i) + " ");
		}
		m.put(1,3);
		System.out.println(" ");
		for(Integer i : m.keySet()) {
		    System.out.print(i + "-" + m.get(i) + " ");
		}
Output is:
1-1 2-2                                                                                                                        
1-3 2-2
But from explanation, I thought output would be:
1-1 2-2                                                                                                                        
2-2 1-3
i.e. entry with key as 1 moved to end...?
Am I misunderstanding something?
 
			 
			
					
				Re: About Question enthuware.ocpjp.v8.2.1325 :
				Posted: Sat Mar 27, 2021 7:59 am
				by admin
				Your output is consistent with the explanation. You did not remove 1,1 before putting 1,3. and so, as the explanation says, the order didn't change.
Do:
m.remove(1);
m.put(1,3);