1

要复制HashMap到的语句序​​列TreeMap

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
4

1 回答 1

6

它按字典顺序排序(默认排序方法)。

您必须定义自己的自然排序顺序的实现。的构造函数TreeMap允许您传入自定义比较器。

于 2013-05-10T10:37:20.547 回答