2014-10-27 80 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