java - What is the most efficient way to access particular elements in a SortedSet? -


I want to use a collection that has been sorted, but one in which I can access elements by index , That is, I need something like a set and a list both. Java.util.TreeSet comes close to reality which I want but does not allow access through any index.

I can think of several options:

  1. I can iterate it every time I need a special element.
  2. I could maintain a tree set and when I needed to use any special element, I could prepare a list.
  3. Like the above, only
  4. By the time I need to add an element, I can have a list and can sort myself.
  5. etc. There are different trade-offs between different options. I hope someone can give me good advice, to answer potential questions, "why do you want to do this?", Please Read about algorithms. I had the same problem, so I took the source code of java.util.SreeMap and pressed

    . It implements its own Indexing Map :
      Public Interface Indexed NavigationalMap & lt; K, V> NavigableMap & lt; K, V> {K accurate (index index); Admission and lieutenant; K, V & gt; Exact indicator (int index); Index of kin (k); }   

    This implementation is based on updating the weight of the node in the red-black tree when it is replaced Weight is the number of hair nodes under a node, as well as a self - self example When a tree is moved to the left:

      Private Wide Rotate Left (Entry  K, V> P) {if (p! = Null) {Entry & Lt; K, V> R = p. Right; Int Delta = Late Weight (R. Left) - Late Weight (P. Wright); P.right = r.left; P.updateWeight (Delta); If (R. Left! = Faucet) {r.left.parent = p; } R.parent = p.parent; If (p.parent == faucet) {root = r; } And if (p.parent.left == p) {delta = getWyight (r) - getWeight (p.parent.left); P.parent.left = r; P.parent.updateWeight (delta); } Else {delta = getWeight (r) - getWeight (p.parent.right); P.parent.right = r; P.parent.updateWeight (delta); } Delta = Milan (P) - getWeight (R. left); R.left = p; R.updateWeight (delta); P.parent = r; }}   

    updateWeight just updates the weight over the root:

      Zero update (int delta) {weight + = delta; Admission and lieutenant; K, V & gt; P = parent; While (p! = Null) {p.weight + = delta; P = p.parent; }}   

    And when we need to find an element of the index, then it is an implementation that uses weight:

      Public key accuracy ( Int index) {if (index and lieutenant; 0 ~ index> size () - 1) {new array indexofobide exception (); } Return getExactKey (root, index); } Private Exchanges of Entitlee (Entry & Lt; K, V & gt; E, Int Index) {if (e.left == Blank & Index == 0) {return e.key; } If (E. Left == blank & amp; e.right == faucet) {return e.key; } If (E. Left! = Null & amp; e.bat.weet> index) {return getExactKey (E. left, index); } If (E. Left! = Null & amp; e.left.weight == index) {return e.key; } Get the return of Exactke (E. Wright, Index - (E. Left == blank? 0: E.F.A.T.Vet) - 1); }   

    Also useful in finding a key indicator:

      public int keyIndex (K key) {if (key == null) { New NullPointerException Throw (); } Listing & lt; K, V> E = accessenti (key); If (e == empty) {new nullionist exception (); } If (E == root) {return getWeight (E) - getWeight (E. Wright) - 1; // index to return} int index = 0; Int cmp; Pointer = = Milan (E. left); Admission and lieutenant; K, V & gt; P = e.parent; // Partition comparator and comparable path Comparator & lt;? Super K & gt; CPR = comparator; If (cpr! = Null) {while (p! = Null) {cmp = cpr.compare (key, pk); If (cmp & gt; 0) {index + = getWeight (p.left) + 1; } P = p.parent; }} And {comparative   

    You can find the result of this work on .

Comments