如何在Java中联合列出<Date> list1和List <Date> list2?

Sad*_*tun 2 java union arraylist duplicates

  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;
}}
Run Code Online (Sandbox Code Playgroud)

联盟方法仍然我得到重复日期你有没有人帮我解决这个问题一段时间答案是正确的但有时重复仍然存在请指导我

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 ****************
Run Code Online (Sandbox Code Playgroud)

如您所见,在两个日期的联合之后,最终列表中仍存在重复的上述输出

Gho*_*ica 5

你的问题在这里; 像你一样做:

dateList1.add(cal.getTime());
Run Code Online (Sandbox Code Playgroud)

您假设当打印输出(如上所述)相等时,两个日期相等.但事实并非如此!你只是忘了"日期"也包括毫秒.如果您更改格式以包含该信息,您会发现您的日期可能具有相同的小时:分钟:秒...但所有不同的毫秒值!

因此,Set 没有看到重复的条目; 它只是看到所有不同的对象!

因此,为了使您的联合工作,您必须在要忽略的日期内"规范化"这些部分.有两种方式:

  1. 正如评论中所提到的,只需将"0"毫秒推入所有创建的Date对象!
  2. 您可以使用TreeSet和自定义 Comparator; 并且比较器可以确保仅在毫秒上不同的日期显示为相等.但这并不是一个很好的方法,因为你无法控制你的哪个日期会"生存".