0

I have a file with data lines like

<Long> <...Other Stuff...>

which I want to read into a Java SortedMap<Long, <OtherStuffType>> (or something similar with a fast get() method for multiple lookups). Now, my data is already sorted, so does there exist an efficient implementation of SortedSet for this case - e.g. one with a guaranteed constant-time put() when a new entry has a larger key than any existing entry.

  • 2
    Are you expecting to ever add/remove entries to/from the Map after you initialize it from the data file? – Eran Sep 05 '16 at 09:16
  • If you just need to keep the order on pre-sorted data and do not need to add elements later, use the `LinkedHashMap` then, because your data is already sorted, it will remain sorted after you populate it. – Oleg Sklyar Sep 05 '16 at 09:25
  • If this is not a [duplicate](http://stackoverflow.com/questions/5279840/how-to-initialize-a-treemap-with-pre-sorted-data), please edit your question to define _efficient_ and explain how the accepted answer is insufficient – trashgod Sep 05 '16 at 09:25
  • @trashgod duplicate of which one? Under efficient the author means constant time insertions for constantly increasing key values (as the question clearly states) – Oleg Sklyar Sep 05 '16 at 09:27
  • @OlegSklyar: duplicate now cited; if "the author means constant time insertions" then the author should say so in context. If you disagree, please vote to reopen. – trashgod Sep 05 '16 at 09:33
  • I'll accept that it's the same question - I asked for a constant time put() for increasing key values, but a linear time put for a pre-sorted putAll() would also be a valid solution. But that said, I don't think the only answer given is terribly useful, since it is based on an assumption about the internal implementation of the putAll() method. I'll continue the discussion on the other issue. – C Rosenthal Sep 05 '16 at 10:13

0 Answers0