About Question enthuware.ocpjp.v8.2.1325 :

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
badbishop
Posts: 27
Joined: Fri Jul 22, 2016 9:14 am
Contact:

About Question enthuware.ocpjp.v8.2.1325 :

Post 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.

admin
Site Admin
Posts: 10043
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

safdev
Posts: 5
Joined: Wed May 24, 2017 5:17 pm
Contact:

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

Post 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).

admin
Site Admin
Posts: 10043
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin »

Good point. thank you for sharing.
-Paul.
If you like our products and services, please help us by posting your review here.

safdev
Posts: 5
Joined: Wed May 24, 2017 5:17 pm
Contact:

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

Post 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.

__JJ__
Posts: 125
Joined: Thu Jul 05, 2018 6:44 pm
Contact:

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

Post 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.

jme_chg
Posts: 29
Joined: Sun Feb 07, 2021 6:30 pm
Contact:

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

Post 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?

admin
Site Admin
Posts: 10043
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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);
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: No registered users and 32 guests