我有一个列表>,它是数据库表的直接表示。我试图在数据加载到 HashMap 列表后进行排序并应用一些魔法。就我而言,这是唯一困难且快速的方法,因为我有一个规则引擎,可以在多次计算后实际更新 HashMap 中的值。
这是 HashMap 的示例数据表示(HashMap 列表)-
{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。现在这是我的问题 -
对于具有相同 eventId 的每个组,我需要更新记录,以便它们具有更宽的 fromDate 到 toDate。
意思是,如果您考虑最后两行,它们具有相同的actionId = 1234和相同的eventId = 11。现在我们可以从这2条记录中选择最少的fromDate,即Wed Mar 17 10:54:12和更远的toDate是 3 月 31 日星期三 10:54:12 并将这 2 条记录的 fromDate 和 toDate 分别更新为 3 月 17 日星期三 10:54:12 和 3 月 31 日星期三 10:54:12。
有什么想法吗?
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();
}
}
}
}
最佳答案
据我从您的描述中了解到,您的所有数据都是从数据库检索的。为什么不通过 SQL 进行排序和分组呢?
UPD(评论后):那么我绝对喜欢带有
的解决方案TreeMap<Integer, List<DbRecord>>
其中 actionIds 是此 TreeMap 的键,列表中的每个项目都是 DbRecord 对象。
在这种情况下,排序和分组问题将隐式解决,您只需迭代 map 即可更新日期值。
更好的方法是使用 TreeMultimap来自 Google Collection 。
关于java - 对 HashMap 列表进行排序和重新排列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2455448/