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 Java in General and the fly likes Specify max size of 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 » Java in General
Reply Bookmark "Specify max size of HashTable " Watch "Specify max size of HashTable " New topic
Author

Specify max size of HashTable

ben bohl
Greenhorn

Joined: Apr 18, 2008
Posts: 2
I have a situation where i want to save some objects into a HashTable(using this because i have multiple threads accessing the same object). I want to specify the maximum size of the collection to be for example 10. If the size of the table exceeds 10, we should push the first entry out of the HashTable and add the new object into the collection of the HashTable. I also want this collection to be as key value pairs.

Any suggestion on how to do this?

Thanks,
Ben
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 56204
    
  13

As a HashTable is unordered, the concept of "first entry" isn't applicable. Are you really needing a hash table, or just an ordered list of name/value pairs?


[Smart Questions] [JSP FAQ] [Books by Bear] [Bear's FrontMan] [About Bear]
arulk pillai
Author
Ranch Hand

Joined: May 31, 2007
Posts: 3190
There could be other solutions if you explain your full requirements. From what you have exaplained:

You will have to write your own implementation. You can do this by writing a wrapper, but inside the wrapper you can make use of the standard Java Collection classes. For example you may have an instance of a Hashtable to support key, value pairs and an ordinary arraylist to keep tabs of the order with the same keys. Once you reach 10 objects, remove first object from the array list and use that object to remove it from the Hashtable as well. Take care to synchronize your methods correctly, since you will be using two different collections (i.e. a map and a list)
[ August 17, 2008: Message edited by: arulk pillai ]

Java Interview Questions and Answers Blog | Amazon.com profile | Java Interview Books
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 2782
    
    2
There is no standard library class called HashTable. If you mean Hashtable, then Bear's answer applies. Otherwise I guess you mean something more general - a hash-table-like thingie that also remembers what order its entries were added in, and allows you to add special behavior when a new item is added. It wouldn't be too difficult to build something like this yourself, I think. But have you taken a look at LinkedHashMap? They've done most of the work for you - you just need to extend it and override removeEldestEntry() - see the documentation.

[ben]: (using this because i have multiple threads accessing the same object)

Hm, well, you can always use Collections.synchronizedMap() to provide just as much mostly-illusory thread safety as Hashtable provides. In general though, you may well need to analyze your code more carefully, and consider what happens if other threads act in between method calls to your map. Synchronization within the map (or for Collections.synchronizedMap(), within a wrapper for the map) is often insufficient to truly protect your code. Consider explicitly synchronizing within the calling class to provide a higher level of protection. Or use a better concurrent Map API, such as that provided by ConcurrentHashMap. Unfortunately there is, as yet, no ConcurrentLinkedHashMap, so to provide this functionality you would have to write your own.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Specify max size of HashTable
 
Similar Threads
collections framework
collections and clone()
Vector Vs. Hashtable
HASHTABLE Problem
Sorting by using Comparator