Sadina Khatun Sadina Khatun - 1 month ago 6
Java Question

How to Union List<Date> list1 and List<Date> list2 in Java?

public class ArrayOfArrayList {
public static void main(String[] args) {
List<Date> dateList1 = new ArrayList<Date>();
List<Date> dateList2 = new ArrayList<Date>();
Calendar cal = new GregorianCalendar();
for(int i = 0; i < 5; i++) {
Date d1 = new Date();
cal.setTime(d1);
cal.add(Calendar.DATE, i);
dateList1.add(cal.getTime());
}
System.out.println(" *************** Date List 1st ****************");
for(Date date1 : dateList1) {
System.out.println("1stList"+date1);
}
System.out.println(" *************** Date List 1st ****************");

for(int i = 2; i < 8; i++) {
Date d2 = new Date();
cal.setTime(d2);
cal.add(Calendar.DATE, i);
dateList2.add(cal.getTime());
}
System.out.println(" *************** Date List 2nd ****************");
for(Date date2 : dateList2) {
System.out.println("2ndList"+date2);
}
System.out.println(" *************** Date List 2nd ****************");

System.out.println(" *********** Start Union Dates ************");
List<Date> finalList = union(dateList1,dateList2);
Collections.sort(finalList);
System.out.println("\n ********* After Union Dates ***********");
for(Date fDate : finalList) {
System.out.println(fDate);
}
System.out.println("\n ********* After Union Dates **********");
}

private static List<Date> union(List<Date> dateList1, List<Date> dateList2) {
HashSet<Date> dateSet = new HashSet<Date>();
dateSet.addAll(dateList1);
dateSet.addAll(dateList2);
List<Date> finalDateList = new ArrayList<Date>(dateSet);
return finalDateList;
}}


with Union Method still i am getting Duplicate Dates could you anyone help me to fix this issue some time answer comes correct but sometime duplicate still present please guide me

O/P

*************** Date List 1st ****************
1stListFri Oct 21 00:38:53 IST 2016
1stListSat Oct 22 00:38:53 IST 2016
1stListSun Oct 23 00:38:53 IST 2016
1stListMon Oct 24 00:38:53 IST 2016
1stListTue Oct 25 00:38:53 IST 2016
*************** Date List 1st ****************
*************** Date List 2nd ****************
2ndListSun Oct 23 00:38:53 IST 2016
2ndListMon Oct 24 00:38:53 IST 2016
2ndListTue Oct 25 00:38:53 IST 2016
2ndListWed Oct 26 00:38:53 IST 2016
2ndListThu Oct 27 00:38:53 IST 2016
2ndListFri Oct 28 00:38:53 IST 2016
*************** Date List 2nd ****************
*************** Start Union Dates ****************

*************** After Union Dates ****************
Fri Oct 21 00:38:53 IST 2016
Sat Oct 22 00:38:53 IST 2016
Sun Oct 23 00:38:53 IST 2016
Sun Oct 23 00:38:53 IST 2016
Mon Oct 24 00:38:53 IST 2016
Mon Oct 24 00:38:53 IST 2016
Tue Oct 25 00:38:53 IST 2016
Tue Oct 25 00:38:53 IST 2016
Wed Oct 26 00:38:53 IST 2016
Thu Oct 27 00:38:53 IST 2016
Fri Oct 28 00:38:53 IST 2016

*************** After Union Dates ****************


As you can see the Above Outputs still duplicate is present in the final list after Union of two dates

Answer

Your problem is here; as you do:

dateList1.add(cal.getTime());

You assume that two dates are equal when their printout (like above) is equal. But that is not true! You simply forgot that "dates" include milli seconds, too. If you change your formatting to include that information, you will find that your dates might have same hours:minutes:seconds ... but all different milli second values!

Thus the Set does not see duplicated entries; it just sees all different objects!

So, in order to make your union work, you have to "normalize" those parts within a Date that you want to ignore. Two ways there:

  1. As mentioned in the comments, just push "0" for milliseconds into all the created Date objects!
  2. You could be using a TreeSet and a custom Comparator; and that Comparator can ensue that Dates that are only different on milliseconds are showing up as equal. But that isn't really a great approach, as you have no control which of your Dates will "survive" then.
Comments