0

我有一个包含大量日期时间数据的 2 个数组列表。一个数组列表包含 10 万个日期时间,另一个数组列表包含 12 万个日期时间。我想将一个数组列表项与每个项的另一个数组列表进行比较。

例如 :

ArrayList<Calendar> al1 = new ArrayList<Calendar>();//contains 100 thousand datetimes,sorted
ArrayList<Calendar> al2 = new ArrayList<Calendar>();//contains 200 thousand datetimes,sorted
//al1.retainall(1l2); // i have tried this.but its taking too much of time.

// i want to retrieve watever same time from al1 to al2
//so i have used loop,still i am getting too much of time.

有什么办法可以减少比较时间数组列表的时间。有人可以帮我吗?

4

2 回答 2

3

我会将一个列表存储在 HashSet 中,其值为毫秒长值。您也可以使用哈希图。也许这个例子可以帮助你。

Set<Long> myTimes = new HashSet<Long>();
for(Calendar auxCal : al1){
    myTimes.add(auxCal.getTimeInMillis());
}

for(Calendar auxCal : al2){
    if(myTimes.contains(auxCal.getTimeInMillis())){
        System.out.println(auxCal.getTime()+ " Matches");
    }
}
于 2013-05-27T07:11:30.917 回答
0

如果可以对您的数据进行排序,则使用二进制搜索或类似的方法

于 2013-05-27T07:18:59.180 回答