2014-10-27 74 views
0

哪個集合將滿足下面的測試程序:地圖與位置索引

public class TestOrderedList { 

    // The class I'm looking for: 
    MapWithIndex<String,Person> ol = new MapWithIndex<String, Person>(); 

    // class Person left out for brevity 

    public TestOrderedList() { 

    Person benny = new Person("Benny"); 
    Person charles = new Person("Charles"); 
    Person alvin = new Person("Alvin"); 
    Person calvin = new Person("Calvin"); 

    ol.put("Benny", benny);  // should return 0 
    ol.put("Charles", charles); // should return 1 
    ol.put("Alvin", alvin);  // should return 0 
    ol.put("Calvin", calvin); // should return 2 

    int index = ol.findIndex("Benny"); // should return 1 
    Person adam = new Person("Adam"); 
    ol.put("Adam", adam);    // should return 0 (new pos) 
    index = ol.findIndex("Benny");  // should return 2 
    ol.remove("Alvin");    // should return 1 (existing pos) 
    index = ol.findIndex("Benny");  // should return 1 
    } 
} 

集合不必是實現任何特定的接口,或可轉換爲另一個集合(然而這是可能這將是尼斯)。

它不必是線程安全的,但如果是....好!

返回-1找不到或錯誤的情況是OK的時候。

收集的目的是,我很快就需要知道在哪個位置一個新插入的記錄放入。此外,我想在一個記錄存在什麼位置查找。

收集並不需要支持重複鍵。

---更新----

我去ArrayList的解決方案,其中我把它插入之前做一個二進制查找排序(得到它應插入索引)。這樣,列表中的位置總是對應於行號(在與該列表同步的表中)。它快速簡單。我確定必須存在比我更強大的實現,儘管?!?!

+0

該結構非常奇怪,因爲它返回插入元素的「位置」,但該位置不固定。像這樣的結構永遠不會是線程安全的。可能你需要解釋你在做什麼。 – gfelisberto 2014-10-27 23:24:57

+0

好的,我可以補充說,在這種情況下,它不必是線程安全的。但是「put」方法應該返回該記錄插入的位置。 – 2014-10-27 23:27:14

+0

可能是一個可索引的SkipList適合賬單。雖然ConcurrentSkipListMap可能會關閉,但JDK中沒有一個。 http://en.wikipedia.org/wiki/Skip_list#Indexable_skiplist – spudone 2014-10-27 23:32:11

回答

1

您可以使用數組以「發現」的二進制搜索實現,問題是,有一個數組是昂貴的添加/刪除項目。一個indexable skip list會更容易增長/縮小,與數組的查找次數相同,但是數組有更好的最壞情況查找,並且也可能具有更好的高速緩存性能。如果你期望經常增加/刪除,那麼我會說使用可索引跳過列表,如果它們很少,那麼使用一個數組。也可能有某種方法來緩衝數組的增加/刪除以獲得更好的攤銷性能,但是我不知道你會如何做到這一點。

+0

也許你應該引用我:) – spudone 2014-10-27 23:37:10

+0

@spudone我的不好,我是在與你同時輸入的。考慮自己引用:) – 2014-10-27 23:37:40

+0

不用擔心我只是亂搞。如果您在Java中找到了一個很好的實現(使用可索引選項),請發佈它! – spudone 2014-10-27 23:39:11

0

使用linkedHashMap,以確保秩序和保持他們的指數在一個單獨的地圖實現。

+1

你能詳細說明我如何在插入後獲得位置嗎? – 2014-10-27 23:33:07

+0

實施它,以便您單獨記錄位置,例如兩個地圖一個用於對象,另一個用於位置 – Pumpkin 2014-10-28 00:19:50

0
import java.util.Map; 
import java.util.Set; 
import java.util.TreeMap; 

public class MySortMap { 


// The class I'm looking for: 
    Map<String,Person> ol = new TreeMap<String, Person>(); 

    // class Person left out for brevity 

    public void TestOrderedList() { 

    Person benny = new Person("Benny"); 
    Person charles = new Person("Charles"); 
    Person alvin = new Person("Alvin"); 
    Person calvin = new Person("Calvin"); 

    ol.put("Benny", benny);  // should return 0 
    ol.put("Charles", charles); // should return 1 
    ol.put("Alvin", alvin);  // should return 0 
    ol.put("Calvin", calvin); // should return 2 

    int index = getIndex(ol.keySet(), "Benny"); // should return 1 
    System.out.println("expected 1, index=" + index); 
    Person adam = new Person("Adam"); 
    ol.put("Adam", adam);    // should return 0 (new pos) 
    index = getIndex(ol.keySet(), "Benny");  // should return 2 
    System.out.println("expected 2, index=" + index); 
    ol.remove("Alvin");    // should return 1 (existing pos) 
    index = getIndex(ol.keySet(), "Benny");  // should return 1 
    System.out.println("expected 1, index=" + index); 

    } 

    private int getIndex(Set<String> paramSet, String paramSearchStr) { 
     int result = -1; 
     for (String strSetValue : paramSet) { 
     result++; 
     if (strSetValue.equals(paramSearchStr)) { 
      return result; 
     } 
    } 
     return -1; 
    } 
/** 
* @param args 
*/ 
public static void main(String[] args) { 
    MySortMap mySortMap = new MySortMap(); 
    mySortMap.TestOrderedList(); 
} 

    class Person { 
     public Person(String paramPerson) { 

     } 
    } 
} 

RESULT: 
expected 1, index=1 
expected 2, index=2 
expected 1, index=1 
+0

+1工作代碼!在getIndex中進行順序搜索並不是我所希望的,但我猜測它可以在小集合和更新頻率有限的情況下工作。 – 2014-10-28 11:04:30

+0

如果您使用列表,您可以使用indexOf方法獲取索引。 – Anand 2014-10-29 12:48:45