1

I have the following use case:

  • a list containing objects with various properties - e.g name,type,date
  • the list can contain a lot of elements(50k - 200k)

I wanted to ask what would be the best list implementation for this situation given the fact that I need to do operations on the list such as

  • order - e.g by type(alphabetically)
  • filtering - e.g. by date
  • no inserts
  • sublists - similar to paging.

Thanks.

4
  • Objects with various properties, that means objects of unrelated classes, or objects of classes who share same superclass? Or you mean objects with properties of various values? Be more specific.. Commented Feb 20, 2012 at 21:37
  • what about thread-safety? how many threads will populate it? Commented Feb 20, 2012 at 21:38
  • @SergeyBenner typically one, since it says 'no inserts'. Commented Feb 20, 2012 at 21:40
  • Yes it is a single thread that will populate the list. Commented Feb 21, 2012 at 9:43

3 Answers 3

2

If you know the size of the list (or an upper bound) before-hand and can guarantee no insertions then you need to use ArrayList.

It is backed up by an array so lookups are fast.

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

Comments

0

java.util.ArrayList seems best in this case (backed up by a vector). One thing that you need to do upfront is reserve enough space when constructing the instance (it's best if you know the number of elements beforehead), to avoid copying on resized vector allocations.

Another win compared to LinkedList is the absense of internode references which reduces the required heap size roughly twice compared to ArrayList.

  • append: O(1) (given no inserts)
  • ordering = sorting: O(n*log(n))
  • filtering: O(n)
  • sublist: O(1)

Comments

0

A single collection type can't satisfy all of those use-cases. The nearest I can see is TreeSet.

  • order - Implement your object to override equals and hascode based on type
  • sublists - headSet, tailSet
  • filtering - roll your won

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.