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;
}}
使用联合方法时,我仍然收到重复日期,有人可以帮我解决这个问题吗?有时答案正确,但有时重复仍然存在,请指导我
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 ****************
正如您所看到的,在两个日期并集后的最终列表中仍然存在重复的上述输出
最佳答案
你的问题就在这里;正如你所做的:
dateList1.add(cal.getTime());
当两个日期的打印输出(如上)相等时,您假设两个日期相等。但事实并非如此!您只是忘记了“日期”也包括毫秒。如果您更改格式以包含该信息,您会发现您的日期可能具有相同的小时:分钟:秒...但所有不同毫秒值!
因此,集合不会看到重复的条目;它只是看到所有不同的对象!
因此,为了使您的联合发挥作用,您必须“规范化”日期中您想要忽略的那些部分。有两种方法:
- 正如评论中提到的,只需将“0”(毫秒)插入所有创建的 Date 对象即可!
- 您可以使用 TreeSet 和自定义比较器;并且比较器可以确保仅在毫秒上不同的日期显示为相等。但这并不是一个很好的方法,因为您无法控制哪个约会对象会“生存”。
关于java - 如何在 Java 中联合 List<Date> list1 和 List<Date> list2?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40162820/