2

我有一个 Hashmap 数组,每个 hashmap 包含24 小时时间作为 key-value pair

我想按时间升序对该数组进行排序。我怎样才能做到这一点?

这是我的代码片段:

HashMap[] arr = new HashMap[100];

for(int i=0;i<100;i++) {
  HashMap<String,String> child=new HashMap<String,String>();
  child.put("some_time","21:09");  //time changes per iteration(time is in 24-hour format)
  arr[i]=child;
}
4

5 回答 5

2

您可以使用Arrays.sort(T[], Comparator<T>). 这允许您传递任何类型的数组并编写自己的自定义比较器方法,如下所示:

Arrays.sort(arr, new Comparator<HashMap>() {
    public int compare(HashMap o1, HashMap o2) {
        // Compare values you're interested in and return int as specified by Comparator API
    }
});

有关返回内容的详细信息,请参阅API 。

于 2012-10-25T13:22:47.163 回答
2

这是按时间对数组进行排序的完整代码,hh:mm格式为:

HashMap<String,String>[] harr = new HashMap[10];
final DateFormat df = new SimpleDateFormat("kk:mm");
// prepare your data
for(int i=0;i<harr.length;i++) {
   HashMap<String,String> child=new HashMap<String,String>();
   int ss = (int)(Math.random() * (59 + 1));
   //time changes per iteration(time is in 24-hour format)
   child.put("some_time", String.format("21:%02d", ss));
   harr[i]=child;
}
System.out.printf("map array is: %s%n", Arrays.deepToString(harr));

// now apply sort using a custom method
Arrays.sort(harr, new Comparator<HashMap<String,String>>() {
    public int compare(HashMap<String,String> o1, HashMap<String,String> o2) {
       String t1 = o1.get("some_time");
       String t2 = o2.get("some_time");
       try {
           Date dt1 = df.parse(t1);
           Date dt2 = df.parse(t2);
           return dt1.compareTo(dt2);
       } catch (ParseException e) {
           e.printStackTrace();
       }
       return 0;
    }
});
System.out.printf("sorted map array is: %s%n", Arrays.deepToString(harr));
于 2012-10-25T13:46:19.247 回答
1

Before proceeding with this approach, do think about the comments and decide whether the array of hashmaps is the right way to go. If, as I pointed out, you have a bunch of maps, each containing large amounts of information, with one entry being your dates, then this may be the right thing to do, in which case the easiest way to sort the array would be to use Arrays.sort method:

HashMap[] arr=new Hashmap[100];

for(int i=0;i<100;i++){
    HashMap<String,String> child=new HashMap<String,String>();
    ... // put all the info into the HashMap
    child.put("some_time","21:09");  //time changes per iteration(time is in 24-hour format)
    arr[i]=child;
}

Arrays.sort(arr, new Comparator<HashMap>() {
    public int compare(HashMap o1, HashMap o2) {
        String d1 = o1.get("some_time");
        String d2 = o2.get("some_time");

        //compare the two dates.  If you're always in the same format, e.g. HH:MM (24 hours, two-digit hour, two-digit year), you might even be able to simply compare strings:
        return d1.compareTo(d2);
    }
});
于 2012-10-25T13:28:51.307 回答
0

正如 Bhavik 指出的那样,您可能没有充分发挥 JDK 的潜力——看看SortedMap可能正是您正在寻找的;可能使用您自己的Comparator实现。

SortedMap arr = new TreeMap<String,HashMap<String,String>>();
for ( int i=0 ; i<100 ; i++ )
{
    Map<String,String> child = HashMap<String,String>();
    child.put( "some_time" , "21:09" );
    arr.put( "21:09" , child );
}

然后你可以arr.values().iterator()用来得到你的排序child仁。

干杯,

于 2012-10-25T13:20:22.487 回答
0

一般的方法是编写一个基于键Comparator对一对HashMap对象进行排序,然后将其作为参数传递给Arrays.sort(T[], Comparator<T>)方法。

比较器看起来像这样:

    Comparator<HashMap> DATE_ORDER = new Comparator<HashMap>() {
        public int compare(Comparator<HashMap>h1, Comparator<HashMap>h2) {
            String time1 = h1.get("some_time");
            String time2 = h2.get("some_time");
            return time1.compareTo(time2);  // assuming that the time strings
                                            // can be ordered that way
        }
    };

话虽如此,您的问题具有在真正应该编写自定义类时尝试使用 Maps 的“气味”。

于 2012-10-25T13:22:57.610 回答