3
Map<String, String> map ;
List<Map<String, String>> list = new ArrayList<Map<String, String>>();


/////OnCreate.............


function1(){
map = new TreeMap<String, String>();
map.put("id", "id");
map.put("amont", "amount");

list.add(map);

System.out.println(list);
}

id=1,3,5,57,80 的输入值

amount=100,500,200,10,10000 的输入值

无法按金额升序对列表进行排序。它仍然按照插入的顺序显示。

我该如何解决?我很感激任何帮助。提前致谢。

预期输出:金额升序:

amt=10 id=4  
amt=100 id=1  
amt=200 id=3  
amt=500 id=2  
amt=10000 id=5  
4

5 回答 5

2

假设这是您的输入

  Map<String, String> map ;
  List<Map<String, String>> list = new ArrayList<Map<String, String>>();
  map = new TreeMap<String, String>();
  map.put("id","1");
  map.put("amount","100");
  list.add(map);
  map = new TreeMap<String, String>();
  map.put("id","2");
  map.put("amount","500");  
  list.add(map);
  map = new TreeMap<String, String>();
  map.put("id","3");
  map.put("amount","200");
  list.add(map);
  map = new TreeMap<String, String>();
  map.put("id","4");
  map.put("amount","10");
  list.add(map);
  map = new TreeMap<String, String>();
  map.put("id","5");
  map.put("amount","10000");
  list.add(map);

这是您的排序代码

  Collections.sort(list, new Comparator<Map<String, String>>() {

        @Override
        public int compare(Map<String, String> o1, Map<String, String> o2) {
            String value1 =  o1.get("amount");
            String value2 =  o2.get("amount");
            return Integer.parseInt(value1)-Integer.parseInt(value2);
        }
    });

    for (Map<String, String> map1 : list) {
        String id = map1.get("id");
        String amount = map1.get("amount");
        System.out.println("amount= "+amount + " , " +"id = "+id);
    }

输出

amount= 10 , id = 4
amount= 100 , id = 1
amount= 200 , id = 3
amount= 500 , id = 2
amount= 10000 , id = 5

更新

return Integer.parseInt(value1)-Integer.parseInt(value2);如果值为十进制,则替换为以下代码。

return Double.valueOf(value1).compareTo(Double.valueOf(value2));
于 2013-10-09T06:35:31.880 回答
1

您必须使用自定义比较器并将其传递给 Treemap 的构造函数。例如,使用以下比较器对数量进行排序:

请参考以下链接。绝对能解决你的问题

http://java2novice.com/java-collections-and-util/treemap/comparator-user-object/

于 2013-10-09T06:11:39.123 回答
1

不。您不能使用默认排序方法根据值缩短任何地图。您应该编写自定义的排序方法。请参阅此链接 - TreeMap 按值排序

如果你想按键排序,我建议使用树形图。

谢谢!

于 2013-10-09T06:09:41.497 回答
1

默认情况下,列表未排序。您需要使用Comparator的Collections.sort()方法。所以看起来你想要排序,你应该像下面那样实现比较器。amount

Collections.sort(list, new Comparator<Map<String, String>>() {
    @Override
    public int compare(Map<String, String> o1, Map<String, String> o2) {
        String amount1 = o1.get("amount");
        String amount2 = o2.get("amount");
        return new Integer(amount1).compareTo(new Integer(amount2));
    }
});

这是完整的工作副本,

List<Map<String, String>> list = new ArrayList<Map<String, String>>();

Map<String, String> map1 = new TreeMap<String, String>();
map1.put("id", "2");
map1.put("amount", "200");

Map<String, String> map2 = new TreeMap<String, String>();
map2.put("id", "1");
map2.put("amount", "100");

Map<String, String> map3 = new TreeMap<String, String>();
map3.put("id", "3");
map3.put("amount", "300");

list.add(map1);
list.add(map2);
list.add(map3);

Collections.sort(list, new Comparator<Map<String, String>>() {
    @Override
    public int compare(Map<String, String> o1, Map<String, String> o2) {
        String amount1 = o1.get("amount");
        String amount2 = o2.get("amount");
        return amount1.compareTo(amount2);
    }
});

System.out.println(list);

它应该打印,

[{amount=100, id=1}, {amount=200, id=2}, {amount=300, id=3}]
于 2013-10-09T06:30:38.697 回答
1

使用排序()

前任:

list.add(map);
Collections.sort(list)

System.out.println(list)

它现在将按其包含的内容类型的升序打印列表。

于 2013-10-09T06:07:37.577 回答