如何对HashMap< String,String []>进行排序由android中的String [英] How to sort HashMap<String, String[]> by String in android

查看:96
本文介绍了如何对HashMap< String,String []>进行排序由android中的String的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有此代码:

    HashMap<String, String[]> unsorted = new HashMap<String, String[]>();
    String[] values = new String[3];
    String key;

    //add data to hashmap
    key = "abc";
    values[0] = "a"; values[1]="b"; values[2]="c";
    unsorted.put(key, values);

    key = "abc";
    values[0] = "aa"; values[1]="bb"; values[2]="cb";
    unsorted.put(key, values);

    key = "def";
    values[0] = "d"; values[1]="e"; values[2]="f";
    unsorted.put(key, values);

    //sort hashmap
    /***********/

    //output should be:
    { abc-[a,b,c], abc-[aa,bb,cc], def-[d,e,f] }

    //or

    { abc-[aa,bb,cc], abc-[a,b,c], def-[d,e,f] }

我该如何排序?注意:我尝试使用TreeMap和其他示例,但是它们消除了相等的元素.

How can I sort it like that? Note: I tried with using TreeMap, and other examples, but they eliminate the elements where the keys are equal.

我解决了我的问题:)感谢Guillaume.这是我用的:

I solved my problem :) thanks to Guillaume. Here is what I used:

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

public class test {

public static void main(String[] args) {

    ArrayList<CustomObject> objs = new ArrayList<CustomObject>();

    objs.add(new CustomObject("abc", new String[] {"a", "b", "c"}));
    objs.add(new CustomObject("def", new String[] {"d", "e", "f"}));
    objs.add(new CustomObject("abc", new String[] {"aa", "bb", "cc"}));


    System.out.println(objs.isEmpty());

    Collections.sort(objs, new Comparator<CustomObject>() {
        @Override
        public int compare(CustomObject o1, CustomObject o2) {
            int i = o1.getKey().compareTo(o2.getKey());
            if(i == 0)
                return -1;
            return i;
        }
    });

    for(int i=0; i<objs.size(); i++)
        System.out.println("key/value pair:" + objs.get(i).getKey() + " - " + objs.get(i).getValues()[0]);
    }
}

和CustomObject:

And the CustomObject:

public class CustomObject {

private String key;
private String[] values;

public CustomObject(String key, String[] values) {
    this.key = key;
    this.values = values;
}

public String getKey() {
    return key;
}

public String[] getValues() {
    return values;
}
}

推荐答案

如果您需要特殊的排序,并且具有多个具有相同键"的对象的功能,则可以使用自定义的List >.

If you need special ordering, and the ability to have multiple objects that have an equal "key", that screams for List, with a custom Comparator.

1-定义要存储在列表中的一类元素.在您的情况下,它是一个具有2个字段的对象:键"和String数组.我们称它为CustomObject(您可以随意命名)

1- Define a class of elements to store in your list. In your case, it's an object that has 2 fields: a "key" and an array of String. Let's call it CustomObject (you can call it as you like)

2-将所有对象粘贴在列表中

2- Stick all your objects in the list

喜欢:

list.add(new CustomObject("abc", new String[] {"a", "b", "c"});
list.add(new CustomObject("abc", new String[] {"aa", "bb", "cc"});
list.add(new CustomObject("def", new String[] {"d", "e", "f"});

3-使用自定义比较器对列表进行排序.

3- Order your list using a custom comparator.

要订购列表,请

Collections.sort(list, new Comparator<CustomObject>() {
        @Override
        public int compare(CustomObject o1, CustomObject o2) {
            return o1.getArray().compare(o2.getArray());
        }
    });

(您的比较器需要更复杂一些,才能正确比较数组,但是您明白我的意思了.)

(your comparator needs to be a bit more sophisticated, to properly compare arrays, but you see my point).

一种替代方法是在CustomObject中添加自然顺序(实现Comparable),然后将其粘贴到TreeSet中.

An alternative to that is to add a natural ordering to your CustomObject (implements Comparable), and stick them into a TreeSet.

这篇关于如何对HashMap&lt; String,String []&gt;进行排序由android中的String的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆