12rad
12rad

Reputation: 849

Efficient java data structure to delete and retrieve information?

I have a situation where I have need a data structure that I can add strings to. This data structure is very large.

The specific qualities I need it have are:

  1. get(index)
  2. delete a certain number of entries that were added initially when the limit exceeds.(LIFO)

I've tried using an ArrayList but the delete operation is o(n) and for a linkedList the traverse or get() operation will be o(n).

What other options do I have?

Upvotes: 1

Views: 722

Answers (2)

JHS
JHS

Reputation: 7871

LinkedHashSet might be of interest. It is effectively a HashSet but it also maintains a LinkedList to allow a predictable iteration order - and therefore can also be used as a FIFO queue, with the nice added benefit that it can't contain duplicate entries.

Because it is a HashSet too, searches (as opposed to scans) can be O(1) if they can match on equals()

You can have a look at this question and this too.

Upvotes: 0

goat
goat

Reputation: 31823

circular buffer - one thats implemented with an array under the hood.

Upvotes: 6

Related Questions