如何对HashMap进行排序<String, String[]>通过 android 中的字符串 [英] How to sort HashMap<String, String[]> by String in android
问题描述
我有这个代码:
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]);
}
}
和自定义对象:
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
来说是一种尖叫,使用自定义 Comparator
.
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 个字段的对象:一个键"和一个字符串数组.我们称之为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.
要对列表进行排序,请执行
To order the list do
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进行排序<String, String[]>通过 android 中的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!