'Iterate the values from the List and get oldest, newest dates

I am working on spring application. I am having a LinkedHashMap<Integer, List<MyDTO>> object, in the value of the map (i.e,List<MyDTO>) I have the property myDate which is holding the date. I want to iterate LinkedHashMap<Integer, List<MyDTO>> and get the oldest and latest date from List<MyDTO> myDate property.

LinkedHashMap<Integer, List<MyDTO>> myResults =  myService.getResultData(myDTO);

 for (Map.Entry results : myResults.entrySet()) {
     List<MyDTO> myDTOList = (List<MyDTO>) results.getValue();

     //some business logic to process the list object, myDate property is in this myDTOList from which i need to get the oldest anad latest date.

}

Above is the sample code, i tried to iterate the Map(myResults) and took the value of the map object which is List<MyDTO>. Any suggestions of how to get the oldest and newest date from the dates stored in myDate property in List object(myDTOList)?? ,



Solution 1:[1]

Here is a simple java code to get the oldest and newest dates. There are many other ways to achieve this.

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

public class CompareDates {

    public static void main(String[] args){
        List<Date> dates = new ArrayList<Date>();
        Date date1 = new GregorianCalendar(2014, Calendar.FEBRUARY, 11).getTime();
        Date date2 = new GregorianCalendar(2015, Calendar.MARCH, 2).getTime();
        Date date3 = new GregorianCalendar(2016, Calendar.JANUARY, 4).getTime();
        Date date4 = new GregorianCalendar(2017, Calendar.MARCH, 17).getTime();
        Date date5 = new GregorianCalendar(2017, Calendar.FEBRUARY, 12).getTime();
        Date date6 = new GregorianCalendar(2017, Calendar.JULY, 14).getTime();

        dates.add(date1);
        dates.add(date2);
        dates.add(date3);
        dates.add(date4);
        dates.add(date5);
        dates.add(date6);

        Date minDate = dates.get(0);
        Date maxDate = dates.get(0);

        for(int i=1; i < dates.size(); i++){
            if(dates.get(i).before(minDate)){
                minDate = dates.get(i);
            }
            if(dates.get(i).after(maxDate)){
                maxDate = dates.get(i);
            }
        }

        System.out.println("Min date is ---"+minDate);
        System.out.println("Max date is ---"+maxDate);

    }
}

In your code, you can use before and after of Date API to perform the comparison in similar way.

LinkedHashMap<Integer, List<MyDTO>> myResults =  myService.getResultData(myDTO);

 for (Map.Entry results : myResults.entrySet()) {
     List<MyDTO> myDTOList = (List<MyDTO>) results.getValue();
        Date minDate, maxDate;
        for(int i=0; i < myDTOList.size(); i++){
            if(i == 0){
                 minDate = myDTOList.get(0).getMyDate();
                 maxDate = myDTOList.get(0).getMyDate();
            }
            if(myDTOList.get(i).before(minDate)){
                minDate = dates.get(i);
            }
            if(myDTOList.get(i).after(maxDate)){
                maxDate = dates.get(i);
            }
        }

        System.out.println("Min date is ---"+minDate);
        System.out.println("Max date is ---"+maxDate);

}

Solution 2:[2]

just iterate the List<MyDTO>, and use two variables max and min to record the newest date and oldest date, which is easy to achieve, so I think you can write code yourself.

hope i didn's misunderstand you question.

Solution 3:[3]

Maybe to late but I used stream():

public static Optional<Calendar> getLastMonth(@NonNull List<Calendar> monthList){
    return monthList.stream().max(Calendar::compareTo);
}

public static Optional<Calendar> getFirstMonth(@NonNull List<Calendar> monthList){
    return monthList.stream().min(Calendar::compareTo);
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Srikanth A
Solution 2 Javdroider
Solution 3 Sebi