0

I have a collection of items, and every item is a simple object with several fields.

List<MyObject> 

where

MyObject = {
   long value1,
   long value2
}

I need collection to be sorted by value1, for instance. Quite often I need to retrieve, for instance, 20 "top" records. New items are added very frequently (hundreds times per second) and may contain e.g. 50000 items.

What types of collection I might use to achieve very fast update of an ordered list?

onkami
  • 8,791
  • 17
  • 90
  • 176
  • Hum: http://stackoverflow.com/questions/416266/sorted-collection-in-java?rq=1 ? –  Dec 08 '16 at 18:07
  • Please explain updated ? Are you flushing the list and getting a new one, delete items and add other items or just add ? – JFPicard Dec 08 '16 at 18:10
  • @JFPicard mostly add. – onkami Dec 08 '16 at 18:15
  • Even with that throughput, I would try [TreeSet](http://docs.oracle.com/javase/8/docs/api/java/util/TreeSet.html) before assuming it cannot achieve the performance you need. – VGR Dec 08 '16 at 21:50

1 Answers1

0

You can use Priority Queue which can sort you data with Comparator. It inserts your data with order O(Log(n)).
Insert all your data on it and then get top 20 items on a loop. which is better to save your data on list and then sort and get top 20.

Ehsan Mashhadi
  • 2,568
  • 1
  • 19
  • 30