About Question enthuware.ocpjp.v7.2.1325 :
Moderator: admin
-
- Posts: 35
- Joined: Mon Jul 28, 2014 2:05 am
- Contact:
About Question enthuware.ocpjp.v7.2.1325 :
Hello!
Isn't it possible to store objects in TreeMap ordered by "last accessed time"?
Thanks in advance,
Peter
Isn't it possible to store objects in TreeMap ordered by "last accessed time"?
Thanks in advance,
Peter
Online
-
- Site Admin
- Posts: 10061
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1325 :
How? and also, not sure why you think that is relevant here.
-Paul.
-Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 35
- Joined: Mon Jul 28, 2014 2:05 am
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1325 :
Yes, I think I'm wrong.
Q: You are designing a class that will cache objects. It should be able to store and retrieve an object when supplied with an object identifier.
Further, this class should work by tracking the "last accessed times" of the objects. Thus, if its capacity is full, it should remove only the object that hasn't been accessed the longest.
Which collection class would you use to store the objects?
I thought there could be some Comparator that compares objects on "last accessed times" and TreeMap based on this Comparator.
But the question says to cache "objects" - arbitrary objects, so I can't create such comparator. Am I right?
Q: You are designing a class that will cache objects. It should be able to store and retrieve an object when supplied with an object identifier.
Further, this class should work by tracking the "last accessed times" of the objects. Thus, if its capacity is full, it should remove only the object that hasn't been accessed the longest.
Which collection class would you use to store the objects?
I thought there could be some Comparator that compares objects on "last accessed times" and TreeMap based on this Comparator.
But the question says to cache "objects" - arbitrary objects, so I can't create such comparator. Am I right?
Online
-
- Site Admin
- Posts: 10061
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1325 :
I see what you mean. You are right. Since there could be objects of any class, it would not be possible to write a generic comparator.
-Paul.
-Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 35
- Joined: Mon Jul 28, 2014 2:05 am
- Contact:
-
- Posts: 17
- Joined: Fri Aug 14, 2015 9:35 am
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1325 :
I have a little confuse about this question. Why we can not use ArrayList in this case ?
Online
-
- Site Admin
- Posts: 10061
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1325 :
You need to associate the last accessed time with an object. Can you think of a way to implement this using an ArrayList?
If you like our products and services, please help us by posting your review here.
-
- Posts: 17
- Joined: Fri Aug 14, 2015 9:35 am
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1325 :
Sorry for my stupid question . Am I misunderstand If I think "last accessed time with an object " mean like "last object add to the collection " ?
Online
-
- Site Admin
- Posts: 10061
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1325 :
No, last object added to the collection may not be the last object used. For example, you may add multiple objects to a list. Now, you may want to use an object (i.e. do something with that object like calling a method or accessing a property) from the middle of the list. So this becomes the last used object.
If you like our products and services, please help us by posting your review here.
-
- Posts: 17
- Joined: Fri Aug 14, 2015 9:35 am
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1325 :
Thanks for your answer. Can you tell me how can I check if an object is last used/accessed ?
Last edited by lukenhung on Tue Sep 08, 2015 10:16 pm, edited 1 time in total.
Online
-
- Site Admin
- Posts: 10061
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1325 :
There are several ways. One way could be as simple as this:
For a more complicated one, see this: http://crunchify.com/how-to-create-a-si ... ght-cache/
Code: Select all
Object getObjectFromCache(String key){
Object retval = objMap.get(key);
lastAccessedMap.put(key, new Date());
return retval;
}
If you like our products and services, please help us by posting your review here.
Online
-
- Site Admin
- Posts: 10061
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1325 :
Here are some more: http://stackoverflow.com/questions/2306 ... -cache-api
If you like our products and services, please help us by posting your review here.
-
- Posts: 17
- Joined: Fri Aug 14, 2015 9:35 am
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1325 :
Sorry but what i mean is how to check an object which store in LinkedHashMap is last used/ accessed ?
Online
-
- Site Admin
- Posts: 10061
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1325 :
Did you read the explanation? It explains exactly what you are asking!
If you like our products and services, please help us by posting your review here.
-
- Posts: 17
- Joined: Fri Aug 14, 2015 9:35 am
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1325 :
I still don't understand why ArrayList can not use in this case, Anyway thanks for your support.
Online
-
- Site Admin
- Posts: 10061
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1325 :
How will you find an object with a key in an ArrayList??
If you like our products and services, please help us by posting your review here.
-
- Posts: 17
- Joined: Fri Aug 14, 2015 9:35 am
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1325 :
Can I use the index of the object like array[0] .... ?
Online
-
- Site Admin
- Posts: 10061
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1325 :
And how will you know the index at which the required object is kept in the list?
If you like our products and services, please help us by posting your review here.
-
- Posts: 30
- Joined: Sat May 02, 2015 4:48 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1325 :
Isn't it the same but easier to just use the last-accessed-order constructor?
Code: Select all
LinkedHashMap(int initialCapacity, float loadFactor, boolean accessOrder)
Last edited by Danny Sheridan on Wed Sep 16, 2015 4:20 am, edited 1 time in total.
Online
-
- Site Admin
- Posts: 10061
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1325 :
Not sure how. Can you please post some code that shows what you have in mind?
If you like our products and services, please help us by posting your review here.
-
- Posts: 30
- Joined: Sat May 02, 2015 4:48 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1325 :
Code: Select all
package study.ocp;
import java.util.LinkedHashMap;
public class InsertionOrAccessOrderMap {
public static void main(String[] args) {
LinkedHashMap<Integer, String> map1 = new LinkedHashMap<>();
LinkedHashMap<Integer, String> map2 = new LinkedHashMap<>(50, 0.75F, true);
map1.put(1, "one");
map2.put(1, "one");
map1.put(2, "two");
map2.put(2, "two");
map1.put(3, "three");
map2.put(3, "three");
System.out.println(map2.get(2));
System.out.println(map1.put(2, map1.remove(2)));
System.out.println(map1 + "\n" + map2);
}
}
two
null
{1=one, 3=three, 2=two}
{1=one, 3=three, 2=two}
Online
-
- Site Admin
- Posts: 10061
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1325 :
I am not following this. Your code should show implementation for two methods: Object get(Object key) and void put(Object key, Object value). How do you plan to implement these two methods?
If you like our products and services, please help us by posting your review here.
-
- Posts: 30
- Joined: Sat May 02, 2015 4:48 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1325 :
I see what you mean.
I'll implement a Cache class example now and get back to you!
I'll implement a Cache class example now and get back to you!
-
- Posts: 30
- Joined: Sat May 02, 2015 4:48 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1325 :
(I left out generics for simplicity)
My point is that the implementation seems to be already there through new LinkedHashMap(int,float,boolean)
so LinkedHashMap might be suitable as the cache class you wish to design
for instance couldn't MyCache below here could have been be just:?
But to make the cache I've just delegated directly
Output:
{1=one, 2=two, 3=three, 4=four}
{1=one, 2=two, 3=three, 4=four}
{1=one, 3=three, 4=four, 2=two}
{1=one, 3=three, 4=four, 2=two}
My point is that the implementation seems to be already there through new LinkedHashMap(int,float,boolean)
so LinkedHashMap might be suitable as the cache class you wish to design
for instance couldn't MyCache below here could have been be just:
Code: Select all
MyCache extends LinkedHashMap{}
But to make the cache I've just delegated directly
Code: Select all
package study.ocp;
import java.util.LinkedHashMap;
class MyCache {
// map to store entries according to last accessed order
static LinkedHashMap map = new LinkedHashMap(50, 0.75F, true);
public Object get(Object key) {
return map.get(key);
}
public void put(Object key, Object value) {
map.put(key, value);
}
}
class YourCache {
// map to store entries by insertion order
static LinkedHashMap map = new LinkedHashMap();
public Object get(Object key) {
Object obj = map.remove(key);
map.put(key, obj);
return obj;
}
public void put(Object key, Object value) {
map.put(key, value);
}
}
public class LastAccessedCacheTest {
public static void main(String[] args) {
MyCache mc = new MyCache();
YourCache yc = new YourCache();
mc.put(1, "one");
yc.put(1, "one");
mc.put(2, "two");
yc.put(2, "two");
mc.put(3, "three");
yc.put(3, "three");
mc.put(4, "four");
yc.put(4, "four");
// print contents before access
System.out.println(mc.map + "\n" + yc.map);
System.out.println( );
mc.get(2);
yc.get(2);
// print contents after access
System.out.println(mc.map + "\n" + yc.map);
}
}
{1=one, 2=two, 3=three, 4=four}
{1=one, 2=two, 3=three, 4=four}
{1=one, 3=three, 4=four, 2=two}
{1=one, 3=three, 4=four, 2=two}
Online
-
- Site Admin
- Posts: 10061
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1325 :
Yes, this looks viable implementation as well.
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 95 guests