Page 1 of 2

About Question enthuware.ocpjp.v7.2.1325 :

Posted: Fri Mar 13, 2015 1:20 am
by pfilaretov
Hello!
Isn't it possible to store objects in TreeMap ordered by "last accessed time"?

Thanks in advance,
Peter

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

Posted: Fri Mar 13, 2015 5:40 am
by admin
How? and also, not sure why you think that is relevant here.
-Paul.

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

Posted: Tue Mar 17, 2015 1:42 am
by pfilaretov
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?

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

Posted: Tue Mar 17, 2015 2:06 am
by admin
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.

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

Posted: Wed Mar 18, 2015 1:53 am
by pfilaretov
thx, Paul

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

Posted: Tue Sep 08, 2015 4:05 am
by lukenhung
I have a little confuse about this question. Why we can not use ArrayList in this case ?

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

Posted: Tue Sep 08, 2015 4:12 am
by admin
You need to associate the last accessed time with an object. Can you think of a way to implement this using an ArrayList?

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

Posted: Tue Sep 08, 2015 8:01 am
by lukenhung
Sorry for my stupid question :D. Am I misunderstand If I think "last accessed time with an object " mean like "last object add to the collection " ?

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

Posted: Tue Sep 08, 2015 8:28 am
by admin
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.

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

Posted: Tue Sep 08, 2015 9:21 am
by lukenhung
Thanks for your answer. Can you tell me how can I check if an object is last used/accessed ?

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

Posted: Tue Sep 08, 2015 9:56 am
by admin
There are several ways. One way could be as simple as this:

Code: Select all

Object getObjectFromCache(String key){
  Object retval =  objMap.get(key);
  lastAccessedMap.put(key, new Date());
  return retval;
}
For a more complicated one, see this: http://crunchify.com/how-to-create-a-si ... ght-cache/

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

Posted: Tue Sep 08, 2015 10:11 am
by admin

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

Posted: Tue Sep 08, 2015 10:31 pm
by lukenhung
Sorry but what i mean is how to check an object which store in LinkedHashMap is last used/ accessed ?

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

Posted: Tue Sep 08, 2015 10:35 pm
by admin
Did you read the explanation? It explains exactly what you are asking!

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

Posted: Tue Sep 08, 2015 11:02 pm
by lukenhung
I still don't understand why ArrayList can not use in this case, Anyway thanks for your support. :D

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

Posted: Wed Sep 09, 2015 12:56 am
by admin
How will you find an object with a key in an ArrayList??

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

Posted: Wed Sep 09, 2015 2:20 am
by lukenhung
Can I use the index of the object like array[0] .... ?

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

Posted: Wed Sep 09, 2015 8:16 am
by admin
And how will you know the index at which the required object is kept in the list?

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

Posted: Wed Sep 16, 2015 4:16 am
by Danny Sheridan
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)

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

Posted: Wed Sep 16, 2015 4:18 am
by admin
Not sure how. Can you please post some code that shows what you have in mind?

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

Posted: Wed Sep 16, 2015 4:22 am
by Danny Sheridan

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);

	}

}
Output:
two
null
{1=one, 3=three, 2=two}
{1=one, 3=three, 2=two}

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

Posted: Wed Sep 16, 2015 4:33 am
by admin
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?

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

Posted: Wed Sep 16, 2015 5:21 am
by Danny Sheridan
I see what you mean.
I'll implement a Cache class example now and get back to you!

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

Posted: Wed Sep 16, 2015 6:22 am
by Danny Sheridan
(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:

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);
		
	}
}
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}

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

Posted: Wed Sep 16, 2015 6:53 am
by admin
Yes, this looks viable implementation as well.