This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes How to get date/time an object is added to a Hashtable Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "How to get date/time an object is added to a Hashtable" Watch "How to get date/time an object is added to a Hashtable" New topic
Author

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

Aris Tan
Greenhorn

Joined: Aug 05, 2005
Posts: 13
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

Joined: Jan 10, 2002
Posts: 56200
    
  13

"Dennis B", please check your private messages for an important administrative matter.


[Smart Questions] [JSP FAQ] [Books by Bear] [Bear's FrontMan] [About Bear]
Bill Shirley
Ranch Hand

Joined: Nov 08, 2007
Posts: 457
No.

You can subclass Hashtable (or HashMap) to provide that additional behavior.


Bill Shirley - bshirley - frazerbilt.com
if (Posts < 30) you.read( JavaRanchFAQ);
sitaram
Greenhorn

Joined: Jan 24, 2008
Posts: 26
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

Joined: Oct 30, 2001
Posts: 1970
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.


Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.<br /> <br />#:^P
ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3829
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...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to get date/time an object is added to a Hashtable
 
Similar Threads
Passing my object to one to other activity
Is both Vector and Hashtable maintains object references in the order they were added?
RecordSet Vs. Hash table
Compare two Hashtables ?? !! :-(
Current Date time