2013-05-10 65 views
1

序列語句複製HashMapTreeMapTreeMap中不正確排序在這種情況下

private Map<String,Bitmap> imageFilexx= new HashMap<String,Bitmap>(); 

//Assuming map has values 
printMap(imageFilexx); 
TreeMap<String,Bitmap> imageFilexxS; 
imageFilexxS= new TreeMap<String,Bitmap>(imageFilexx); 
printMap(imageFilexxS); 

功能,顯示此地圖:

public static void printMap(Map<String, Bitmap> map) { 
     for (Map.Entry entry : map.entrySet()) { 
      System.out.println("Key : " + entry.getKey()); 
     } 
    } 

輸入:

Key : http://myserver.com/myfolder/ImageDesc/ImageDesc%20p7.png 
Key : http://myserver.com/myfolder/ImageDesc/ImageDesc%20p1.png 
Key : http://myserver.com/myfolder/ImageDesc/ImageDesc%20p5.png 
Key : http://myserver.com/myfolder/ImageDesc/ImageDesc%20p3.png 
Key : http://myserver.com/myfolder/ImageDesc/ImageDesc%20p11.png 
Key : http://myserver.com/myfolder/ImageDesc/ImageDesc%20p9.png 

當前O/P:

Key : http://myserver.com/myfolder/ImageDesc/ImageDesc%20p1.png 
Key : http://myserver.com/myfolder/ImageDesc/ImageDesc%20p11.png //11 should be displayed in the last 
Key : http://myserver.com/myfolder/ImageDesc/ImageDesc%20p3.png 
Key : http://myserver.com/myfolder/ImageDesc/ImageDesc%20p5.png 
Key : http://myserver.com/myfolder/ImageDesc/ImageDesc%20p7.png 
Key : http://myserver.com/myfolder/ImageDesc/ImageDesc%20p9.png 

預期O/P:

Key : http://myserver.com/myfolder/ImageDesc/ImageDesc%20p1.png 
Key : http://myserver.com/myfolder/ImageDesc/ImageDesc%20p3.png 
Key : http://myserver.com/myfolder/ImageDesc/ImageDesc%20p5.png 
Key : http://myserver.com/myfolder/ImageDesc/ImageDesc%20p7.png 
Key : http://myserver.com/myfolder/ImageDesc/ImageDesc%20p9.png 
Key : http://myserver.com/myfolder/ImageDesc/ImageDesc%20p11.png 

回答

6

據字典順序排序(在default排序方法)。

您必須定義您自己的natural sort order的實現。 TreeMap的構造函數允許您傳入自定義比較器。

+0

感謝NaturalOrderComparator。 'imageFilexxS = new TreeMap (new NaturalOrderComparator());'現在是我新的'Treemap'的構造函數。但是,我如何使用它來從中排序我現有的地圖。 – Prateek 2013-05-10 10:52:52

+0

將現有地圖中的成員添加到該地圖(或者只是在代碼中使用自然排序的地圖)。 – 2013-05-10 11:05:44

+1

是的,我做到了,謝謝你的回覆,我今天學到了一些東西。 – Prateek 2013-05-10 11:13:12

相關問題