Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ruby-on-rails-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 按升序时间对Hashmap数组排序_Java_Arrays_Time_Hashmap_Sorting - Fatal编程技术网

Java 按升序时间对Hashmap数组排序

Java 按升序时间对Hashmap数组排序,java,arrays,time,hashmap,sorting,Java,Arrays,Time,Hashmap,Sorting,我有一个Hashmap数组,每个Hashmap都包含作为键值对的24小时时间 我想按时间的升序对这个数组进行排序。我怎样才能做到这一点 以下是我的代码片段: 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");

我有一个Hashmap数组,每个Hashmap都包含作为键值对的24小时时间

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

以下是我的代码片段:

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;
}
HashMap[]arr=newhashmap[100];

对于(inti=0;i,正如Bhavik指出的,您可能没有充分利用JDK的潜力——看看哪一个可能正是您想要的;可能是您自己实现的

SortedMap arr=newtreemap();

对于(int i=0;i您可以使用
Arrays.sort(T[],Comparator)
。这允许您传递任何类型的数组,并编写自己的自定义Comparator方法,如下所示:

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
    }
});
    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
        }
    };
Arrays.sort(arr,新的比较器(){
公共int比较(HashMap o1、HashMap o2){
//比较您感兴趣的值并返回ComparatorAPI指定的int
}
});

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

通常的方法是编写一个
比较器
,根据键对一对
HashMap
对象进行排序,然后将其作为参数传递给
array.sort(T[],Comparator)
方法

这个比较器看起来像这样:

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
    }
});
    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
        }
    };
比较器日期\u顺序=新比较器(){ 公共整数比较(比较器H1、比较器H2){ 字符串time1=h1.get(“某个时间”); 字符串time2=h2.get(“某个时间”); 返回time1.compareTo(time2);//假设时间字符串 //可以那样点吗 } };


话虽如此,你的问题还是有“味道”在编写自定义类时尝试使用映射。

在继续使用此方法之前,一定要考虑注释并确定哈希映射数组是否正确。如我所指出的,如果您有一组映射,每个映射包含大量信息,其中一个条目是您的日期,那么可能是正确的做法,在这种情况下,对数组进行排序的最简单方法是使用
数组。排序方法:

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);
    }
});
HashMap[]arr=newhashmap[100];

对于(int i=0;i,以下是将按时间对数组进行排序的完整代码,格式为
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));
HashMap[]harr=newhashmap[10];
最终日期格式df=新的简化格式(“kk:mm”);
//准备好你的数据

对于(int i=0;i有什么理由不简单地将HashMap用于100个条目,而不是数组?我认为HashMap不是正确的数据结构sort@assylias,BhavikShah我怀疑每个hashmaps都包含多个条目;其中一个条目是排序时间,他想按它排序。@assylias:我有多个条目试试hashmap;其中一个条目是排序时间,我想按它排序。SortedMap如何解决这个问题?他想对数组排序(SortedMap是按键排序的,而不是按值排序的)。感谢您的回答,到目前为止它工作正常,但包含12个时间小时的条目(例如12:01、12:34、12:55等)没有排序并显示在数组的开头。@dd619:请尝试使用
final DateFormat df=new SimpleDateFormat(“kk:mm”);
而不是
“hh:mm”