简体   繁体   中英

How can I retrieve the last record inserted into a Java Hashtable data structure?

I have the following problem working on a Java application that uses an Hashtable calcoliTable data structure.

This is the content of my calcoliTable :

{
FITTIZIO-2015=CalcoloValoreDellaGSBean [data=2015-11-27, maturato=249540.544989802560000, sommaMaturatoMovimento=249540.544989802560000], 

1=CalcoloValoreDellaGSBean [data=2015-11-27, maturato=249540.544989802560000, sommaMaturatoMovimento=249540.544989802560000]
}

As you can see it contains 2 entries having id=1 and id=FITTIZIO-2015 .

I want to retrieve the entry that was inserted most recently(that should be the one having id=FITTIZIO-2015 ).

I have tried to do in this way:

CalcoloValoreDellaGSBean calcoloPrecedente = (CalcoloValoreDellaGSBean) calcoliTable.get(calcoliTable.size())

But it won't work because in this way it is searching the entry having id=2 .

How can I retrieve the last entry inserted into my HashTable ? Exist a way to do it without using explitelly the key?

Use a LinkedHashMap with an access-order ordering mode :

Map<KeyType,ValueType> map = new LinkedHashMap<KeyType,ValueType> (16, 0.75F, true);

And then

map.entrySet().iterator().next()

will give you the last entry you accessed (either inserted or retrieved).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM