3

I have two classes representing travel segments - a Segment class and a Camp class. A Camp is technically a type of Segment but I can't change the entity design at this point so assume they are unrelated. Both Segments and Camps contain checkIn and checkOut Date fields.

What I need to do is loop through a List of Segments and a List of Camps and call a certain function for each of the segments and camps in those lists. However, the function must be called in sequential date order for each of the segments and camps.

Obviously I can sort the Lists of Segments and Camps separately but the problem arises if there's a Camp for which the function should be called in between Segments.

So for example if I have the following:

List of Segments
Segment 1 - 10/16/14
Segment 2 - 10/24/14

List of Camps
Camp 1 - 10/17/14
Camp 2 - 10/18/14

I'd need to perform an operation on the segments/camps in the following order:
Segment 1
Camp 1
Camp 2
Segment 2

As I see it I have a few options to handle this but I'm wondering what is the most efficient / best way to do it. I could build some type of helper class that contains the fields of both Segments and Camps and create a list of objects of this helper class that contains either Segment or Camp detail.

This is what I've got currently. I've decided to go with a Map approach of mapping Dates to Camps, so I iterate through the keys of the map (Dates) and see if any of those dates are before the segment date, and if so, perform the operation on them. Is there a better way to do this? It seems ugly and confusing to me.

final Map<Date, List<Camp>> campMap = new HashMap();

for (Camp camp : pnr.getCamps()) {
    if (campMap.get(camp.getCheckInDate()) == null) {
        final List<Camp> camps = new ArrayList<Camp>();
        camps.add(camp);
        campMap.put(camp.getCheckInDate(), camps);
    } else {
        campMap.get(camp.getCheckInDate()).add(camp);
    }
}

final List<Date> datesToDelete = new ArrayList<Date>();

for (Segment segment : pnr.getSegments()) {
    // here is where I loop through the segments to perform an operation on each of them 

    for (Map.Entry<Date, List<Camp>> entry : campMap.entrySet()) {
        if (entry.getKey().before(segment.getSegmentDate())
                || entry.getKey().equals(segment.getSegmentDate())) {
            for (Camp camp : entry.getValue()) {
                // do something with the camp 
            }
            datesToDelete.add(entry.getKey());
        }
    }

    for (Date date : datesToDelete) {
        campMap.remove(date);
    }
    datesToDelete.clear();

    // do something with the segment

}

Basically I'm asking if there's a better/more intuitive/cleaner way to do this. Thanks so much for any help.

2
  • Can you change the source of Segments and Camps? i.e. have them implement a common interface?
    – Kong
    Jun 16, 2014 at 7:46
  • Yes, I could have them implement a common interface within the Java source - only thing I can't change is the data model - ie. the DB entities, so I can't make Camp extend Segment for example.
    – Tom Sprud
    Jun 16, 2014 at 7:50

2 Answers 2

4

There's no "correct" way to tackle this; but for me I'd have your classes implement a common interface, something like:

public interface DateAware {

    long getDate();

}

After you've done that you can begin to work with them polymorphically. You could for example use a simple comparator to sort them with Collections.sort:

public class DateAwareComparator implements Comparator<DateAware> {

    int compare(DateAware o1, DateAware o2) {
        return Long.compare(o1.getDate(), o2.getDate());
    }

}

And you can now do things like:

List<DateAware> list = new ArrayList<>();
list.addAll(segments);
list.addAll(camps);

Using an interface also has the benefit that you're not placing any constraints on the inheritance hierarchy to achieve what your after.

2
  • The code is incomplete, but this is the way to go. Have a custom comparator that can compare two segments, two camps or a mix by comparing the dates then Collections.sort(segmentAndCampList, new SegmentAndCampComparator()) Jun 16, 2014 at 8:20
  • 1
    For the provided code from the author, this solution is more enough. The author just needs to make sure his both classes implement the interface, in this case this means replace both methods getCheckInDate and getSegmentDate with the shared method within the interace. Jun 16, 2014 at 8:39
1

Have a custom Comparator (http://docs.oracle.com/javase/7/docs/api/java/util/Comparator.html) that can compare two segments, two camps or a mix of the two by comparing the dates then Collections.sort(segmentAndCampList, new SegmentAndCampComparator())

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.