1

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?

4
  • define 'very large'....? Commented May 31, 2012 at 0:22
  • I'm not aware of anything pre-existing in Java that can do that. Though I haven't exhaustively searched all of the queue variants. Commented May 31, 2012 at 0:23
  • LinkedList has a get(index) operator, plus enqueuing/dequeuing front and rear. Commented May 31, 2012 at 0:25
  • Anywhere between 50,000-80,000 strings. It has the potential of increasing too. Commented May 31, 2012 at 0:30

2 Answers 2

6

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

Sign up to request clarification or add additional context in comments.

2 Comments

I am not that familiar with a circular buffer.Will it maintain the order in which the elements are inserted?
yes. get(index) is O(1) and so is delete, but, you can only delete the oldest(or newest) entries. you generally specify a maximum size for array backed implementations, and it self deletes the oldest entry when you add a new entry, if the new entry would overflow the buffer.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.