-1

I have a list of an object which contain info for different flights and each flight has one date. What i was trying to accomplish is get the user to pick a date then I would iterate through the list to return flights on that date but also a couple of days earlier and after.

How to do it?

Logic-----------

get flights on this date, then get flights around this date

Cœur
  • 37,241
  • 25
  • 195
  • 267
Wil Ferraciolli
  • 449
  • 3
  • 9
  • 21
  • 2
    Could you store a list of flight objects within a Map, using a key which represents the date? Or do you have other constraints that would make it impractical? – IdusOrtus Dec 11 '14 at 13:56
  • 1
    What's the problem? Scan the list, return the flights with dates in range ... Seems straightforward. – Dima Dec 11 '14 at 13:57
  • What have you tried? Are the flight objects in a list? ordered? in a map? What's wrong with simply programming your logic? seems very straightforward. `getFlightsOnDate(Date date)` `getFlightsOnDate(Date incrementedDate)` etc. – stealthjong Dec 11 '14 at 14:08
  • what the hell it means??: get flights on this date, then get flights around this date , can you show what you have tried so far? – Vishwa Ratna Jan 09 '19 at 11:56

3 Answers3

0

When you say "around this date" you need to quantify that. How many days or weeks around the date? Use that to create an upper and lower bound when extracting data from the List.

Nazgul
  • 1,892
  • 1
  • 11
  • 15
0

You could try putting the flight objects into a map where the date is the key and the value is a list of all the flights on that date.

It would mean getting flights on a specific date is n(1) and assuming your date range is a constant (x days) you'll make additional 2x n(1) lookups.

If your bounds are infinite or potentially very large that's probably not the way to go.

Ronny Shapiro
  • 411
  • 3
  • 10
  • thank you for the reply, the problem that i was having is that there are too many flights on the list, but i will have a look at the Map solution it looks like very optimized solution. Thank you – Wil Ferraciolli Dec 11 '14 at 14:15
0

Iterate the List obtain the flight details and define a range of Date which you want to obtain

List<Flight> flights=new ArrayList<Flight>();
for(Flight f:flights)
 {
   Date d=f.getDate();
    if(d is within the desired range)
     {
        //your logic in here
     }
 }

If you want a logic from scratch what you can do is obtain the current time in millseconds and substract the milliseconds consisted in a day(i.e.. 86400000) from current time in milliseconds

 Calendar c= Calendar.getInstance();
 System.out.println(c.getTimeInMillis()-86400000);   // This will give the date one day before the current date
 Date d=new Date(c.getTimeInMillis()-86400000);
 System.out.println(d.toString());
Abhishek
  • 878
  • 12
  • 28