How to limit the number of entries in a Java hash table?
•
Java
Is there a technique so that I can specify a number n so that when inserting the (N1) th entry, the oldest entry is deleted first to ensure that the size of the hash table is always limited to n?
Solution
LinkedHashMap does. Please refer to the Javadoc for removeeldestentry method
Such things should be done by trick, which will delete the oldest insert entry:
Map map = new LinkedHashMap() { @Override protected boolean removeEldestEntry(Entry eldest) { return size() > N; } };
You can also delete the oldest access entry by specifying it in the constructor:
Map map = new LinkedHashMap(16,0.75f,true) { @Override protected boolean removeEldestEntry(Entry eldest) { return size() > N; } };
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码