Assaf Adato
Assaf Adato

Reputation: 237

storing entries in a Java map, in LIFO order

I'm trying to achieve something like a LinkedHashMap, with entries added in the top, rather than in the bottom (LIFO). I don't want to sort it afterwards, just looking for an implementation that allows exactly that.

Thanks.

Upvotes: 3

Views: 1154

Answers (4)

enriko
enriko

Reputation: 46

If you are looking for a "StackHashMap", this can probably help you:

http://www.koders.com/java/fidA648AB05902E307E4CDD11A20D41499984C495AE.aspx?s=IHandler

Upvotes: 2

Peter Lawrey
Peter Lawrey

Reputation: 533590

Sometimes you are better off using two collections to make things clearer, esp. when you are doing something unusual. In this case, a Deque (which doesn't have to be synchronized/thread safe) or Stack (which is synchronized) with a Map may be the best option.

Upvotes: 3

Will
Will

Reputation: 75645

The Java Stack class is a LIFO queue.

Upvotes: 0

Perhaps you are looking for a Stack?

http://download.oracle.com/javase/1.4.2/docs/api/java/util/Stack.html

Upvotes: 0

Related Questions