shree
shree

Reputation: 2757

How to compare two calendar in the biggest arraylist in java?

i have a 2 arraylist which contains huge datetime datas. one arraylist contains 100 thousand datetimes, another arraylist contains 120 thousand datetimes. I want to compare one arraylist item with another arralylist of each item.

for example :

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.

Is there any way to reduce the time to compare arraylist of times.?can anyone please help me?

Upvotes: 0

Views: 229

Answers (2)

bmavus
bmavus

Reputation: 892

if it is possible sort your data then use binary search or something like that

Upvotes: 0

PbxMan
PbxMan

Reputation: 7623

I would store one list in a HashSet whose value is the millsecs long value. You could use a hashmap too. Perhaps this example may help you.

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");
    }
}

Upvotes: 3

Related Questions