• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to get date/time an object is added to a Hashtable  RSS feed

 
Aris Tan
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

Is it possible to get the date/time an object is added to a Hashtable?

Thanks for the help.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66306
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Dennis B", please check your private messages for an important administrative matter.
 
Bill Shirley
Ranch Hand
Posts: 457
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No.

You can subclass Hashtable (or HashMap) to provide that additional behavior.
 
sitaram
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
import java.text.DateFormat;
import java.util.Date;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

public class HashTableDemo{

public static void main(String[] args) {
Hashtable ht = new Hashtable();
DateFormat df = DateFormat.getDateInstance();
ht.put("1",new Date(13,04,06)); // format yy,mm(0-11),dd(0-31)
ht.put("2",new Date(10,10,02));
ht.put("3",new Date(15,11,02));
Set s = ht.entrySet();
Iterator e = s.iterator();
while(e.hasNext()){
Map.Entry me = (Map.Entry)e.next();
Object ok = me.getKey();
Object ov = me.getValue();
System.out.print("Key ......"+ok);
System.out.print(" Value......"+df.format(ov)+ "\n");

}
}
}
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Er, no, that's not going to help is it?! In your code, you store only a Date. The original poster wants to know what time and date some entry was added. That entry isn't going to be a Date.

The Hashtable and HashMap classes don't provide such a facility, because it would eat up lots of memory and CPU, storing the times in all cases, when 99.99% of applications would not need them.

However, such a facility can easily be added by subclassing, as another poster already suggested. Call the subclass DatedHashMap, or something.

Many times, when people subclass a Hashtable or HashMap, they would be better off containing the map as a member of some other class. However, in the particular case you're talking about, subclassing seems appropriate, to me at least.
 
ankur rathi
Ranch Hand
Posts: 3830
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Dennis B:
Hi

Is it possible to get the date/time an object is added to a Hashtable?

Thanks for the help.


They are not persistence storage. By this, I mean, when JVM (or server) shutdowns, they are gone. So why would you implement such functionality in these collections. Use database instead...
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!