2012-03-30 186 views
1

我正在使用'ConcurrentSkipListMap'(因爲我的環境是多線程的)和一個'比較器'根據它的Id &日期對插入對象進行排序。在'TestObject'中它的名字將是唯一的。所以我用它作爲我的'鑰匙'在地圖上。 'Id'將是一個任意值。我需要根據'Id'和'date'值對我的地圖進行排序(如果ID相同,則按日期排序),因爲我只是添加當前日期並將焦點放在ID字段上。但是地圖並沒有讓我知道排序順序。ConcurrentSkipListMap使用比較器排序

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); 
} 

}

而對象類是 -

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; 
} 

}

缺貨片段是 -

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

預期訂單是 -

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

任何人都可以請指出我在這裏做錯了什麼?提前致謝。

+0

'ConcurrentSkipListMap'是按鍵排序的,而不是值。坦率地說,我認爲沒有任何類型安全的方法可以通過Java中的值對可變映射進行排序。 – 2012-03-30 19:09:31

回答

3

該圖比較鍵而不是值。即它是按字母順序排列的。


P.S.還需要用比較器作爲構造函數參數初始化映射。但在這種情況下,它不起作用,因爲比較器應該比較字符串(鍵)。

1

您尚未向比較儀提供您的ConcurrentSkipListMap的構造函數。

參見:

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

由於比較空,在實現中使用的鑰匙,在這種情況下是字符串值的自然順序。

+0

即使我將該比較器傳遞給構造函數,這不會被修復。需要想出辦法,減去字符串前綴(「Obj_」)並比較它並返回。我認爲它應該工作。 – Sam 2012-03-31 02:17:19

0

TreeMap和ConcurrentSkipListMap只能通過Key進行排序,如果要按不確定值排序,可以用構造方法創建一個內部Comparator類,當你通過TestComparator的新實例傳遞「map」時private static Map<String, TestObject> map = new ConcurrentSkipListMap<String, TestObject>(); Then new new TreeMap該參數與之前的「比較對象」相對應。最後將「映射」映射到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); 
} 

}`

我不認爲對代碼的效率,它只是實現的功能。