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