About Question enthuware.ocpjp.v7.2.1325 :

Moderator: admin

pfilaretov
Posts: 35
Joined: Mon Jul 28, 2014 2:05 am
Contact:

About Question enthuware.ocpjp.v7.2.1325 :

Post by pfilaretov » Fri Mar 13, 2015 1:20 am

Hello!
Isn't it possible to store objects in TreeMap ordered by "last accessed time"?

Thanks in advance,
Peter

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

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

Post by admin » Fri Mar 13, 2015 5:40 am

How? and also, not sure why you think that is relevant here.
-Paul.
If you like our products and services, please help us by posting your review here.

pfilaretov
Posts: 35
Joined: Mon Jul 28, 2014 2:05 am
Contact:

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

Post by pfilaretov » Tue Mar 17, 2015 1:42 am

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?

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

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

Post by admin » Tue Mar 17, 2015 2:06 am

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

pfilaretov
Posts: 35
Joined: Mon Jul 28, 2014 2:05 am
Contact:

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

Post by pfilaretov » Wed Mar 18, 2015 1:53 am

thx, Paul

lukenhung
Posts: 17
Joined: Fri Aug 14, 2015 9:35 am
Contact:

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

Post by lukenhung » Tue Sep 08, 2015 4:05 am

I have a little confuse about this question. Why we can not use ArrayList in this case ?

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

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

Post by admin » Tue Sep 08, 2015 4:12 am

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.

lukenhung
Posts: 17
Joined: Fri Aug 14, 2015 9:35 am
Contact:

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

Post by lukenhung » Tue Sep 08, 2015 8:01 am

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

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

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

Post by admin » Tue Sep 08, 2015 8:28 am

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.

lukenhung
Posts: 17
Joined: Fri Aug 14, 2015 9:35 am
Contact:

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

Post by lukenhung » Tue Sep 08, 2015 9:21 am

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.

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

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

Post by admin » Tue Sep 08, 2015 9:56 am

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

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

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

Post by admin » Tue Sep 08, 2015 10:11 am

If you like our products and services, please help us by posting your review here.

lukenhung
Posts: 17
Joined: Fri Aug 14, 2015 9:35 am
Contact:

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

Post by lukenhung » Tue Sep 08, 2015 10:31 pm

Sorry but what i mean is how to check an object which store in LinkedHashMap is last used/ accessed ?

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

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

Post by admin » Tue Sep 08, 2015 10:35 pm

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.

lukenhung
Posts: 17
Joined: Fri Aug 14, 2015 9:35 am
Contact:

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

Post by lukenhung » Tue Sep 08, 2015 11:02 pm

I still don't understand why ArrayList can not use in this case, Anyway thanks for your support. :D

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

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

Post by admin » Wed Sep 09, 2015 12:56 am

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.

lukenhung
Posts: 17
Joined: Fri Aug 14, 2015 9:35 am
Contact:

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

Post by lukenhung » Wed Sep 09, 2015 2:20 am

Can I use the index of the object like array[0] .... ?

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

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

Post by admin » Wed Sep 09, 2015 8:16 am

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.

Danny Sheridan
Posts: 30
Joined: Sat May 02, 2015 4:48 pm
Contact:

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

Post by Danny Sheridan » Wed Sep 16, 2015 4:16 am

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.

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

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

Post by admin » Wed Sep 16, 2015 4:18 am

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.

Danny Sheridan
Posts: 30
Joined: Sat May 02, 2015 4:48 pm
Contact:

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

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

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}

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

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

Post by admin » Wed Sep 16, 2015 4:33 am

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.

Danny Sheridan
Posts: 30
Joined: Sat May 02, 2015 4:48 pm
Contact:

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

Post by Danny Sheridan » Wed Sep 16, 2015 5:21 am

I see what you mean.
I'll implement a Cache class example now and get back to you!

Danny Sheridan
Posts: 30
Joined: Sat May 02, 2015 4:48 pm
Contact:

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

Post by Danny Sheridan » Wed Sep 16, 2015 6:22 am

(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}

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

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

Post by admin » Wed Sep 16, 2015 6:53 am

Yes, this looks viable implementation as well.
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 6 guests