About Question enthuware.ocpjp.v8.2.1325 :
Moderator: admin
-
- Posts: 27
- Joined: Fri Jul 22, 2016 9:14 am
- Contact:
About Question enthuware.ocpjp.v8.2.1325 :
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.
-
- Site Admin
- Posts: 10043
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1325 :
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.
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 5
- Joined: Wed May 24, 2017 5:17 pm
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1325 :
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":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.
Code: Select all
LinkedHashMap(int initialCapacity, float loadFactor, boolean accessOrder=>true)
-
- Site Admin
- Posts: 10043
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1325 :
Good point. thank you for sharing.
-Paul.
-Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 5
- Joined: Wed May 24, 2017 5:17 pm
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1325 :
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.
-
- Posts: 125
- Joined: Thu Jul 05, 2018 6:44 pm
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1325 :
LinkedHashSet isn't a Map. From the question:
That means you're giving some value and getting an object back, which an only be done with a map.It should be able to store and retrieve an object when supplied with an object identifier.
-
- Posts: 29
- Joined: Sun Feb 07, 2021 6:30 pm
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1325 :
I tried the following code:
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?
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) + " ");
}
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?
-
- Site Admin
- Posts: 10043
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1325 :
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);
Do:
m.remove(1);
m.put(1,3);
If you like our products and services, please help us by posting your review here.
Who is online
Users browsing this forum: No registered users and 32 guests