代码之家  ›  专栏  ›  技术社区  ›  jagamot

哈希映射的排序和重新排列列表

  •  0
  • jagamot  · 技术社区  · 14 年前

    {fromDate=Wed Mar 17 10:54:12 EDT 2010, eventId=21, toDate=Tue Mar 23 10:54:12 EDT 2010, actionId=1234}
    {fromDate=Wed Mar 17 10:54:12 EDT 2010, eventId=11, toDate=Wed Mar 17 10:54:12 EDT 2010, actionId=456}
    {fromDate=Sat Mar 20 10:54:12 EDT 2010, eventId=20, toDate=Thu Apr 01 10:54:12 EDT 2010, actionId=1234}
    {fromDate=Wed Mar 24 10:54:12 EDT 2010, eventId=22, toDate=Sat Mar 27 10:54:12 EDT 2010, actionId=1234}
    {fromDate=Wed Mar 17 10:54:12 EDT 2010, eventId=11, toDate=Fri Mar 26 10:54:12 EDT 2010, actionId=1234}
    {fromDate=Sat Mar 20 10:54:12 EDT 2010, eventId=11, toDate=Wed Mar 31 10:54:12 EDT 2010, actionId=1234}
    {fromDate=Mon Mar 15 10:54:12 EDT 2010, eventId=12, toDate=Wed Mar 17 10:54:12 EDT 2010, actionId=567}
    

    1) 按actionId和eventId对列表进行排序,之后的数据如下所示-

    {fromDate=Wed Mar 17 10:54:12 EDT 2010, eventId=11, toDate=Wed Mar 17 10:54:12 EDT 2010, actionId=456}
    {fromDate=Mon Mar 15 10:54:12 EDT 2010, eventId=12, toDate=Wed Mar 17 10:54:12 EDT 2010, actionId=567}
    {fromDate=Wed Mar 24 10:54:12 EDT 2010, eventId=22, toDate=Sat Mar 27 10:54:12 EDT 2010, actionId=1234}
    {fromDate=Wed Mar 17 10:54:12 EDT 2010, eventId=21, toDate=Tue Mar 23 10:54:12 EDT 2010, actionId=1234}
    {fromDate=Sat Mar 20 10:54:12 EDT 2010, eventId=20, toDate=Thu Apr 01 10:54:12 EDT 2010, actionId=1234}
    {fromDate=Wed Mar 17 10:54:12 EDT 2010, eventId=11, toDate=Fri Mar 26 10:54:12 EDT 2010, actionId=1234}
    {fromDate=Sat Mar 20 10:54:12 EDT 2010, eventId=11, toDate=Wed Mar 31 10:54:12 EDT 2010, actionId=1234}
    

    2) 如果我们按actionId将上面的列表分组,它们将被分解为3组:actionId=1234、actionId=567和actionId=456-

    有什么想法吗?

    PS:我已经有一些伪代码要开始了。

    import java.util.ArrayList;
    import java.util.Calendar;
    import java.util.Collections;
    import java.util.Comparator;
    import java.util.Date;
    import java.util.HashMap;
    import java.util.List;
    import org.apache.commons.lang.builder.CompareToBuilder;
    public class Tester {
        boolean ascending = true ;
        boolean sortInstrumentIdAsc = true ;
        boolean sortEventTypeIdAsc = true ; 
    
        public static void main(String args[]) {
            Tester tester = new Tester() ;
            tester.printValues() ;
        }
    
        public void printValues ()
        {
    
            List<HashMap<String,Object>> list = new ArrayList<HashMap<String,Object>>() ;
            HashMap<String,Object> map = new HashMap<String,Object>();
    
            map.put("actionId", new Integer(1234)) ;
            map.put("eventId", new Integer(21)) ;
            map.put("fromDate", getDate(1) ) ;
            map.put("toDate", getDate(7) ) ;
            list.add(map);
    
            map = new HashMap<String,Object>();
            map.put("actionId", new Integer(456)) ;
            map.put("eventId", new Integer(11)) ;
            map.put("fromDate", getDate(1)) ;
            map.put("toDate", getDate(1) ) ;
            list.add(map);
    
    
            map = new HashMap<String,Object>();
            map.put("actionId", new Integer(1234)) ;
            map.put("eventId", new Integer(20)) ;
            map.put("fromDate", getDate(4) ) ;
            map.put("toDate", getDate(16) ) ;
            list.add(map);
    
            map = new HashMap<String,Object>();
            map.put("actionId", new Integer(1234)) ;
            map.put("eventId", new Integer(22)) ;
            map.put("fromDate",getDate(8) ) ;
            map.put("toDate", getDate(11)) ;
            list.add(map);
    
    
            map = new HashMap<String,Object>();
            map.put("actionId", new Integer(1234)) ;
            map.put("eventId", new Integer(11)) ;
            map.put("fromDate",getDate(1) ) ;
            map.put("toDate", getDate(10) ) ;
            list.add(map);
    
            map = new HashMap<String,Object>();
            map.put("actionId", new Integer(1234)) ;
            map.put("eventId", new Integer(11)) ;
            map.put("fromDate",getDate(4) ) ;
            map.put("toDate", getDate(15) ) ;
            list.add(map);
    
    
            map = new HashMap<String,Object>();
            map.put("actionId", new Integer(567)) ;
            map.put("eventId", new Integer(12)) ;
            map.put("fromDate", getDate(-1) ) ;
            map.put("toDate",getDate(1)) ;
            list.add(map);
    
    
            System.out.println("\n Before Sorting \n ");
            for(int j = 0 ; j < list.size() ; j ++ ) 
                System.out.println(list.get(j));    
    
            Collections.sort ( list , new HashMapComparator2 () ) ;
    
            System.out.println("\n After Sorting \n ");
            for(int j = 0 ; j < list.size() ; j ++ ) 
                System.out.println(list.get(j));
    
        }
    
    
        public static Date getDate(int days) {
    
            Calendar cal = Calendar.getInstance();
            cal.setTime(new Date());
            cal.add(Calendar.DATE, days);
            return cal.getTime() ;        
    
        }
    
        public class HashMapComparator2 implements Comparator
        {
            public int compare ( Object object1 , Object object2 )
            {
                if ( ascending == true )
                {
                    return new CompareToBuilder()
                    .append(( ( HashMap ) object1 ).get ( "actionId" ), ( ( HashMap ) object2 ).get ( "actionId" ))
                    .append(( ( HashMap ) object2 ).get ( "eventId" ), ( ( HashMap ) object1 ).get ( "eventId" ))
                    .toComparison();
                }
                else
                {
                    return new CompareToBuilder()
                    .append(( ( HashMap ) object2 ).get ( "actionId" ), ( ( HashMap ) object1 ).get ( "actionId" ))
                    .append(( ( HashMap ) object2 ).get ( "eventId" ), ( ( HashMap ) object1 ).get ( "eventId" ))
                    .toComparison();
                }
            }
        }
    
    
    }
    
    6 回复  |  直到 14 年前
        1
  •  2
  •   Roman    14 年前

    据我所知,从您的描述,您的所有数据都是从数据库检索的。你为什么不用SQL对东西进行排序和分组呢?

    UPD(评论后):那我肯定喜欢一个

    TreeMap<Integer, List<DbRecord>> 
    

    其中actionId是这个树映射的键,列表中的每一项都是DbRecord对象。

    更好的方法是使用 TreeMultimap

        2
  •  1
  •   CoolBeans Jake    13 年前
    import java.util.*;
    
    
    public class hasmap {
    
     public static void main(String[] args) {
      List <Map> result=new ArrayList();
      Map emp1 = new HashMap();
      emp1.put("Name", "wivek");
      emp1.put("EmpID", Long.valueOf("1077"));
      emp1.put("JoinDate",new Date());
      emp1.put("MobileNo",Long.valueOf("1234567890"));
    
      Map emp2 = new HashMap();
      emp2.put("Name", "aww");
      emp2.put("EmpID", Long.valueOf("10"));
      emp2.put("JoinDate",new Date());
      emp2.put("MobileNo",Long.valueOf("1234567890"));
    
      Map emp3 = new HashMap();
      emp3.put("Name", "bww");
      emp3.put("EmpID", Long.valueOf("10"));
      emp3.put("JoinDate",new Date());
      emp3.put("MobileNo",Long.valueOf("1234567890"));
    
      result.add(emp1);
      result.add(emp2);
      result.add(emp3);
    
      System.out.println("\n Before Sorting \n" );
                   for(int j = 0 ; j < result.size() ; j ++ ) 
                          System.out.println(result.get(j)); 
                    srt(result,"Name");
    
     }
    
    private static void srt(List<Map> result, final String n) {
    
    
      Collections.sort(result, new Comparator(){
    
                public int compare(Object o1, Object o2) {
                 Map e1 = (Map) o1;
                    Map e2 = (Map) o2;
                    return e1.get(n).toString().compareToIgnoreCase(e2.get(n).toString());
                }
            });
      System.out.println("\n After Sorting \n" );
            for(int j = 0 ; j < result.size() ; j ++ ) 
                System.out.println(result.get(j)); 
     }
    
    
    }
    
        3
  •  0
  •   M. Jessup    14 年前

    public class ObjectFactory{
    
      class Key{
        String eventId, actionId;
      }
    
      HashMap<Key, ObjectXYZ> objects = new HashMap<...,...>();
    
      ObjectXYZ getObject(String actionId, String eventId, Date from, Date to){
        Key k = new Key(actionId, eventId);
        ObjectXYZ ret = objects.get(k);
        if(ret == null){
          ret = new ObjectXYZ(actionid, eventId, from, to);
          objects.put(k, ret);
        }else{
          if(from < ret.from)  ret.from = from;
          if(to < ret.to) ret.to = to;
        }
        return ret;
      }
    
    }
    

    这样您就不需要创建任何额外的对象,并且要排序的对象也就更少了(如果您需要对它们进行排序的话)。

        4
  •  0
  •   Steve B.    14 年前

    public class MapComparator implements Comparator<Map>{
       public MapComparator(String key, boolean asc){..set value properties ...}
       public int comparae(Map a, Map b) { ... compare a.get(key), b.get(key) ... }
    }
    

    由于您的数据也在动态变化,我认为您还有一个额外的复杂性,即为一个操作重新排序地图集合可能会使上一个操作的排序无效。如果您是串行地执行操作,并且一旦您完成了排序,就不需要保留值,这很好,但是如果您同时需要访问两个不同的排序结果,那么它将中断,因为每个排序都覆盖(或者至少可能中断)前一个排序。为此你可以

    • 对映射的深度副本进行排序,为每个操作创建集合的完整副本。如果您的数据集很大,这可能会非常昂贵。
        5
  •  0
  •   fretje    14 年前

    你想给你的单子排序吗?然后使用 TreeSet 和一个定制的比较仪。这样,每次添加地图时,都会将其设置在正确的位置。

    Collections#sort .

    最后请注意,我认为你的排序方法很奇怪,因为排序你的DB数据通常可以通过在SQL语句中使用排序谓词来更一致地进行。

        6
  •  0
  •   CoolBeans Jake    13 年前

    这是我最后的解决办法-

    import java.util.ArrayList;
    import java.util.Calendar;
    import java.util.Collections;
    import java.util.Comparator;
    import java.util.Date;
    import java.util.HashMap;
    import java.util.List;
    
    import org.apache.commons.lang.builder.CompareToBuilder;
    
    public class Tester {
    
        boolean ascending = true ;
    
    
        boolean sortInstrumentIdAsc = true ;
        boolean sortEventTypeIdAsc = true ; 
    
    
        public static void main(String args[]) {
            Tester tester = new Tester() ;
            tester.printValues() ;
        }
    
        public void printValues() {
    
            List<HashMap<String, Object>> list = new ArrayList<HashMap<String, Object>>();
            HashMap<String, Object> map = new HashMap<String, Object>();
    
            map.put("actionId", new Integer(1234));
            map.put("eventId", new Integer(21));
            map.put("fromDate", getDate(1));
            map.put("toDate", getDate(7));
            list.add(map);
    
            map = new HashMap<String, Object>();
            map.put("actionId", new Integer(456));
            map.put("eventId", new Integer(11));
            map.put("fromDate", getDate(1));
            map.put("toDate", getDate(1));
            list.add(map);
    
    
            map = new HashMap<String, Object>();
            map.put("actionId", new Integer(1234));
            map.put("eventId", new Integer(20));
            map.put("fromDate", getDate(4));
            map.put("toDate", getDate(16));
            list.add(map);
    
            map = new HashMap<String, Object>();
            map.put("actionId", new Integer(1234));
            map.put("eventId", new Integer(22));
            map.put("fromDate", getDate(8));
            map.put("toDate", getDate(11));
            list.add(map);
    
    
            map = new HashMap<String, Object>();
            map.put("actionId", new Integer(1234));
            map.put("eventId", new Integer(11));
            map.put("fromDate", getDate(1));
            map.put("toDate", getDate(10));
            list.add(map);
    
            map = new HashMap<String, Object>();
            map.put("actionId", new Integer(1234));
            map.put("eventId", new Integer(11));
            map.put("fromDate", getDate(4));
            map.put("toDate", getDate(15));
            list.add(map);
    
    
            map = new HashMap<String, Object>();
            map.put("actionId", new Integer(1234));
            map.put("eventId", new Integer(11));
            map.put("fromDate", getDate(8));
            map.put("toDate", getDate(30));
            list.add(map);
    
            map = new HashMap<String, Object>();
            map.put("actionId", new Integer(567));
            map.put("eventId", new Integer(12));
            map.put("fromDate", getDate(-1));
            map.put("toDate", getDate(1));
            list.add(map);
    
    
            System.out.println("\n Before Sorting \n ");
            for (int j = 0; j < list.size(); j++) {
                  System.out.println(list.get(j));
            }
    
            // sort the list
            HashMapComparator2 comparator = new HashMapComparator2();
            Collections.sort(list, comparator);
    
            System.out.println("\n After Sorting \n ");
            for (int j = 0; j < list.size(); j++) {
                  System.out.println(list.get(j));
            }
    
    
            HashMap<String, Object> prev = null;
            List<HashMap<String, Object>> same = new ArrayList<HashMap<String, Object>>();
            for (HashMap<String, Object> row : list) {
                  if (prev != null) {
                        int diff = comparator.compare(prev, row);
                        if (diff == 0) {
                              same.add(row);
                              same.add(prev);
                        }
                        else {
                              merge(same);
                              same.clear();
                        }
                  }
                  prev = row;
            }
            merge(same);
    
            System.out.println("\n After Merging \n ");
            for (int j = 0; j < list.size(); j++) {
                  System.out.println(list.get(j));
            }
      }
    
      private void merge(List<HashMap<String, Object>> same) {
            if (!same.isEmpty()) {
                  // Now find min max
                  Date min = null;
                  Date max = null;
                  for (HashMap<String, Object> i : same) {
                        Date from = (Date) i.get("fromDate");
                        Date to = (Date) i.get("toDate");
                        if (min == null) {
                              min = from;
                        }
                        else if (from.before(min)) {
                              min = from;
                        }
                        if (max == null) {
                              max = to;
                        }
                        else if (to.after(max)) {
                              max = to;
                        }
                  }
                  for (HashMap<String, Object> i : same) {
                        i.put("fromDate", min);
                        i.put("toDate", max);
                  }
            }
      }
    
    
        public static Date getDate(int days) {
    
            Calendar cal = Calendar.getInstance();
            cal.setTime(new Date());
            cal.add(Calendar.DATE, days);
            return cal.getTime() ;        
    
        }
    
        public class HashMapComparator2 implements Comparator
        {
            public int compare ( Object object1 , Object object2 )
            {
                if ( ascending == true )
                {
                    return new CompareToBuilder()
                    .append(( ( HashMap ) object1 ).get ( "actionId" ), ( ( HashMap ) object2 ).get ( "actionId" ))
                    .append(( ( HashMap ) object2 ).get ( "eventId" ), ( ( HashMap ) object1 ).get ( "eventId" ))
                    .toComparison();
                }
                else
                {
                    return new CompareToBuilder()
                    .append(( ( HashMap ) object2 ).get ( "actionId" ), ( ( HashMap ) object1 ).get ( "actionId" ))
                    .append(( ( HashMap ) object2 ).get ( "eventId" ), ( ( HashMap ) object1 ).get ( "eventId" ))
                    .toComparison();
                }
            }
        }
    
    
    }