How to initialize a TreeMap with pre-sorted data?

后端 未结 1 1953
旧巷少年郎
旧巷少年郎 2020-12-10 17:35

My app uses a TreeMap to keep data sorted and have log(n) lookups & inserts. This works great in the general case while the app is running, but when the app first starts

相关标签:
1条回答
  • 2020-12-10 17:47

    Sure! The TreeMap.putAll method (and the TreeMap constructor that takes a SortedMap) calls a method called buildFromSorted internally, which is described in the docs as: "Linear time tree building algorithm from sorted data", so that sounds like it does what you want.

    Just give the putAll method something that implements Map, but where the map's entryset iterator (Map.entrySet().iterator()) returns your list of sorted values.

    0 讨论(0)
提交回复
热议问题