ConcurrentSkipListMap使用Comparator排序

时间:2012-03-30 18:47:19

标签: java sorting collections comparator

我正在使用'ConcurrentSkipListMap'(因为我的环境将是多线程的)和'Comparator'来根据其Id和amp;来对插入对象进行排序。日期。在'TestObject'中,它的名称将是唯一的。所以我在地图中用它作为我的“钥匙”。 'Id'将是一个任意值。我需要根据'Id'和'date'值对我的地图进行排序。(如果Id s相等会按日期排序)这里我只需添加当前日期并将焦点放在ID字段上。但是地图没有给我回复我期望的排序顺序。

public class SortTest {

private static final Comparator TEST_COMPARATOR = new TestComparator();
private static Map<String, TestObject> map = new ConcurrentSkipListMap<String, TestObject>();

private static class TestComparator<T> implements Comparator<TestObject> {

    @Override
    public int compare(TestObject o1, TestObject o2) {
        Integer x1 = o1.getId();
        Integer x2 = o2.getId();

        int Comp = x1.compareTo(x2);

        if (Comp != 0) {
            return Comp;
        } else {

            Date d1 = o1.getDate();
            Date d2 = o2.getDate();

            return d1.compareTo(d2);
        }
    }

}

public static void construct() {

    for (int i = 1; i <= 10; i++) {
        TestObject t = new TestObject();
        t.setId(i%3);
        t.setDate(new Date());
        t.setName("Obj_"+i);
        System.out.println(t);
        map.put(t.getName(),t);


    }

}
public static void main(String[] args) {
    SortTest x = new SortTest();
    x.construct();
    System.out.println(map);
}

}

ObjectClass是 -

public class TestObject {

private String name;

private int id;

Date date;

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public Date getDate() {
    return date;
}

public void setDate(Date date) {
    this.date = date;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

/* (non-Javadoc)
 * @see java.lang.Object#hashCode()
 */
@Override
public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result + ((name == null) ? 0 : name.hashCode());
    return result;
}



/* (non-Javadoc)
 * @see java.lang.Object#toString()
 */
@Override
public String toString() {
    return "TestObject [name=" + name + ", id=" + id + "]";
}

/* (non-Javadoc)
 * @see java.lang.Object#equals(java.lang.Object)
 */
@Override
public boolean equals(Object obj) {
    if (this == obj)
        return true;
    if (obj == null)
        return false;
    if (getClass() != obj.getClass())
        return false;
    TestObject other = (TestObject) obj;
    if (name == null) {
        if (other.name != null)
            return false;
    } else if (!name.equals(other.name))
        return false;
    return true;
}

}

Out snippet是 -

  

{Obj_1 = TestObject [name = Obj_1,id = 1],Obj_10 = TestObject [name = Obj_10,id = 1],Obj_2 = TestObject [name = Obj_2,id = 2],Obj_3 = TestObject [name = Obj_3 ,id = 0],......

预期订单是 -

Obj_3(由于Id = 0),Obj_1,Obj_10,(Ids = 1),Obj_2(Id = 2)

有谁能请指出我在这里做错了什么?提前谢谢。

3 个答案:

答案 0 :(得分:3)

地图比较键,而不是值。即它按键的字母串顺序排序。


P.S。您还需要使用比较器初始化地图作为构造函数参数。但在这种情况下它不起作用,因为比较器应该比较字符串(键)。

答案 1 :(得分:1)

您尚未将比较器提供给ConcurrentSkipListMap的构造函数。

请参阅:

http://docs.oracle.com/javase/6/docs/api/java/util/concurrent/ConcurrentSkipListMap.html

因为Comparator为null,所以实现使用键的自然顺序,在本例中是String值。

答案 2 :(得分:0)

TreeMap和ConcurrentSkipListMap只能按Key排序,如果你想按不确定的值排序,你可以用一个构造方法创建一个内部的Comparator类,当你在TestComparator的新实例中传递&#34; map&#34; private static Map<String, TestObject> map = new ConcurrentSkipListMap<String, TestObject>();然后用一个先前的&#34;比较器对象&#34;来新建一个TreeMap参数。最后放入&#34; map&#34;到TreeMap 像这样的代码:`public class SortTest {     private static Map map = new ConcurrentSkipListMap();

private static class TestComparator<String> implements Comparator<String> {
    Map<String, TestObject> tmp;

    public TestComparator(Map<String, TestObject> map) {
        this.tmp = map;
    }

    /*
     * (non-Javadoc)
     * 
     * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
     */
    public int compare(String o1, String o2) {
        Integer x1 = tmp.get(o1).getId();
        Integer x2 = tmp.get(o2).getId();
        int Comp = x1.compareTo(x2);

        if (Comp != 0) {
            return Comp;
        } else {

            Date d1 = tmp.get(o1).getDate();
            Date d2 = tmp.get(o2).getDate();
            return d1.compareTo(d2);
        }
    }

}

public static void construct() {

    for (int i = 1; i <= 10; i++) {
        TestObject t = new TestObject();
        t.setId(i % 3);
        t.setDate(new Date());
        t.setName("Obj_" + i);
        System.out.println(t);
        map.put(t.getName(), t);

    }

}
public static void main(String[] args) {
    SortTest x = new SortTest();
    x.construct();
    System.out.println(map);
    TestComparator comparator = new TestComparator(map);
    TreeMap<String, TestObject> sorted_map = new TreeMap<String, TestObject>(comparator);
    sorted_map.putAll(map);
    System.out.println(sorted_map);
}

}`

我没有考虑代码的效率,只是意识到了这个功能。