【问题标题】:Sorting a list of maps对地图列表进行排序
【发布时间】:2013-03-26 14:49:46
【问题描述】:

我有一张地图列表

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

像这样被填充

map.put("firstName",John);
map.put("lastName",Smith);
map.put("type","1"); //type is either 1 or a 0

people.add(map);

我希望在填充此列表后对其执行的操作是让所有人在列表顶部输入0,在底部输入1

我知道我需要使用Comparator,但我以前从未使用过,所以我不知道如何使用它或它是如何工作的。

谁能帮帮我

【问题讨论】:

  • 实现Comparator#compare()方法,检查每个map上key type的值,并根据1或0返回。
  • 你为什么用地图来表示人物信息?像List<Person> 这样创建自己的 Person 类并使用列表不是更简单吗?
  • @Pshemo 是的,但这是 android 用来填充列表的东西,需要该列表适配器的地图列表
  • 您能告诉我们到目前为止您尝试了什么吗?

标签: java list sorting map


【解决方案1】:

像这样

Collections.sort( people, new Comparator<Map<String, Object>>() {
    @Override
    public int compare( Map<String, Object> o1, Map<String, Object> o2 ) {
        return (Integer.parseInt((String)o1.get( "type" ))) - 
                (Integer.parseInt((String)o2.get( "type" )));
    }
} );

但是,有很多方法可以改善这一点。如果您不能按照@Pshemo 的建议使用 Person 对象来表示地图,那么至少为您的 type 属性使用合理的数据类型。最好是枚举:

public enum PersonType {
    TYPE_1, TYPE_2
}

这样比较就更清晰、更快速、更易读了。

【讨论】:

  • 您需要在o1.get( "type" ) 附近的字符串中添加强制转换,因为o1.get 的结果类型是Object -> Map&lt;String, Object&gt;
  • @Pshema,已修复,感谢您的更正。只是展示了没有编译器可以完成的工作:(...
【解决方案2】:

Comparator 只是一个需要实现的接口,它只包含一个需要重写的方法。

例如:

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

    Map<String, Object> map = new HashMap<String, Object>();
    map .put("firstName","John");
    map.put("lastName","Smith");
    map.put("type","1"); //type is either 1 or a 0

    people.add(map);

    Collections.sort(people, new Comparator<Map<String, Object>>() {
        @Override
        public int compare(Map<String, Object> o1, Map<String, Object> o2) {
            // you may compare your map here
            return 0;
        }
    });

【讨论】:

    【解决方案3】:

    试试这个

     Collections.sort(people, new Comparator<Map<String, String>>() {
    
        @Override
        public int compare(Map<String, String> m1, Map<String, String> m2) {
            return m1.get("type").compareTo(m2.get("type"));
        }
    });
    

    【讨论】:

      【解决方案4】:

      你可以这样试试:

      class ListByType 
      {
          private static class MyComparator implements Comparator<HashMap<String,String>>
          {
              @Override
              public int compare(HashMap mp1 , HashMap mp2)
              {
                  return ((String)(mp1.get("type")).compareTo((String)mp2.get("type"));
              }
          }
          public static void main(String[] args) 
          {
              List<Map<String, String>> people = new ArrayList<Map<String,String>>();
              HashMap<String,String> map = new HashMap<String,String>();
              map.put("firstName","John");
              map.put("lastName","Smith");
              map.put("type","1"); //type is either 1 or a 0
              people.add(map);
              /*...
              ..
              ...
              Add more maps here..
              */
              //Sort the list
              Collections.sort(people,new MyComparator());
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-06-25
        • 1970-01-01
        • 2021-07-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-06-21
        相关资源
        最近更新 更多