I have a fairly large number of records of a fixed size. Each record contains many fields, among them ID and value. I am wondering which data structure will be better so that I can
very quickly find the record by identifier (unique),
specify 100 records with the highest values.
Max-heap seems to be work, but far from perfect; Do you have a more reasonable solution?
Thank.
, . ID , , -. 100- . . 100 , , / -100, 100 . , O, .
. , - . . 100 ?
:
add(Item t) { //Add the same object instance to both data structures heap.add(t); hash.add(t); } remove(int id) { heap.removeItemWithId(id);//this is gonna be slow hash.remove(id); } getTopN(int n) { return heap.topNitems(n); } getItemById(int id) { return hash.getItemById(id); } updateValue(int id, String value) { Item t = hash.getItemById(id); //now t is the same object referred to by the heap and hash t.value = value; //updated both. }
, , Java, , TreeSet, ID, .
, 100 100 . ID .
, Binary-Tree Balanced Tree .
Source: https://habr.com/ru/post/1676758/More articles:Are HashMap entries always sorted by key if the key is of type Integer? - javaHow to combine input parser with fullRunInputTask? - scalaMatplotlib 3D graphics color definition - pythonSaving final fields in Widget or in State? - dartHow to convert json that contains LocalDate field to deserializable format? - javaPandas read_hdf is very slow for non-numeric data - performanceGet the number of incidents based on a date range and set the days of the week - dateUsing a command template with parameters - javaHow to use a command template by passing run-time parameters to it - javaShiny & networkD3 responds to node click - javascriptAll Articles