如何对哈希图列表进行排序

发布于 2024-08-23 06:42:35 字数 679 浏览 2 评论 0原文

我有一个 HashMapList ,如下所示,

ArrayList l = new ArrayList ();
HashMap m = new HashMap ();
m.add("site_code","AL");
m.add("site_name","Apple");
l.add(m);
m = new HashMap();
m.add("site_code","JL");
m.add("site_name","Cat");
l.add(m);
m = new HashMap();
m.add("site_code","PL");
m.add("site_name","Banana");
l.add(m)

我想根据 site_namelist 进行排序。所以最终它会被排序为。

Apple, Banana, Cat

我正在尝试这样的事情:

Collections.sort(l, new Comparator(){
           public int compare(HashMap one, HashMap two) {
              //what goes here?
           }
});

I have a List of HashMap such as below

ArrayList l = new ArrayList ();
HashMap m = new HashMap ();
m.add("site_code","AL");
m.add("site_name","Apple");
l.add(m);
m = new HashMap();
m.add("site_code","JL");
m.add("site_name","Cat");
l.add(m);
m = new HashMap();
m.add("site_code","PL");
m.add("site_name","Banana");
l.add(m)

I'd like to sort the list based on site_name. So in the end it would be sorted as.

Apple, Banana, Cat

I was trying something like this:

Collections.sort(l, new Comparator(){
           public int compare(HashMap one, HashMap two) {
              //what goes here?
           }
});

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

风铃鹿 2024-08-30 06:42:35

如果你让你的集合通用,它最终会看起来像这样:

Collections.sort(l, new Comparator<HashMap<String, String>>(){ 
        public int compare(HashMap<String, String> one, HashMap<String, String> two) { 
            return one.get("site_name").compareTo(two.get("site_name"));
        } 
});

如果你因为被困在 1.4 或更早的平台上而无法使用泛型,那么你将不得不强制转换 getString

(另外,作为风格问题,我更喜欢将变量声明为 ListMap 而不是 ArrayListHashMap< /代码>。但这与问题无关。)

If you make your collections generic, it will end up looking about like this:

Collections.sort(l, new Comparator<HashMap<String, String>>(){ 
        public int compare(HashMap<String, String> one, HashMap<String, String> two) { 
            return one.get("site_name").compareTo(two.get("site_name"));
        } 
});

If you can't use generics because you're stuck on a 1.4 or earlier platform, then you'll have to cast the get's to String.

(Also, as a matter of style, I'd prefer declaring the variables as List and Map rather than ArrayList and HashMap. But that's not relevant to the question.)

写下不归期 2024-08-30 06:42:35

我认为现在是考虑重新设计的好时机。从您的示例来看,您的所有对象似乎都具有相同的两个字段 - site_namesite_code。在这种情况下,为什么不定义自己的类而不是使用 HashMap 呢?

public class Site implements Comparable<Site> {
    private String site_name;
    private String site_code;

    // getters and setters, equals, and hashCode

    public int compareTo(Site other) {
        return this.site_name.compareTo(other.getSiteName);
    }
}

然后您可以使用Collections.sort()

I think this is a great time to think about a redesign. From your example, it looks like all of your objects have the same two fields - site_name and site_code. In that case, why not define your own class rather than using a HashMap?

public class Site implements Comparable<Site> {
    private String site_name;
    private String site_code;

    // getters and setters, equals, and hashCode

    public int compareTo(Site other) {
        return this.site_name.compareTo(other.getSiteName);
    }
}

And then you can just use Collections.sort().

执着的年纪 2024-08-30 06:42:35

比如:

String codeOne = (String)one.get("site_code");
String codeTwo = (String)two.get("site_code");

return codeOne.compareTo(codeTwo);

我还没有编译或测试过这个,但它应该遵循这些思路。

Something like:

String codeOne = (String)one.get("site_code");
String codeTwo = (String)two.get("site_code");

return codeOne.compareTo(codeTwo);

I haven't compiled or tested this, but it should be along these lines.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文