简体   繁体   English

Java - 如何按日期迭代单独对象的列表?

[英]Java - How to iterate through lists of separate objects by date?

I have two classes representing travel segments - a Segment class and a Camp class. 我有两个代表旅行段的类 - 一个Segment类和一个Camp类。 A Camp is technically a type of Segment but I can't change the entity design at this point so assume they are unrelated. Camp在技术上是一种Segment,但我不能在此时更改实体设计,因此假设它们是无关的。 Both Segments and Camps contain checkIn and checkOut Date fields. Segments和Camps都包含checkIn和checkOut Date字段。

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. 显然我可以分别对段和列的列表进行排序,但如果有一个Camp应该在段之间调用该函数,则会出现问题。

So for example if I have the following: 例如,如果我有以下内容:

List of Segments 细分列表
Segment 1 - 10/16/14 第1节 - 10/16/14
Segment 2 - 10/24/14 第2部分 - 10/24/14

List of Camps 营地名单
Camp 1 - 10/17/14 1号营地 - 2014年10月17日
Camp 2 - 10/18/14 2号营地 - 2014年10月18日

I'd need to perform an operation on the segments/camps in the following order: 我需要按以下顺序对段/阵营执行操作:
Segment 1 第1部分
Camp 1 1号营地
Camp 2 2号营地
Segment 2 第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. 我可以构建一些类型的辅助类,其中包含Segments和Camps的字段,并创建包含Segment或Camp详细信息的此辅助类的对象列表。

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. 我已决定使用Map将Dates映射到Camps的方法,因此我遍历地图的键(日期)并查看这些日期是否在段日期之前,如果是,则对它们执行操作。 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. 非常感谢您的帮助。

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 : 例如,您可以使用一个简单的比较器来使用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. 使用接口还有一个好处,就是您不会对继承层次结构施加任何约束来实现您的后续操作。

有一个自定义Comparatorhttp://docs.oracle.com/javase/7/docs/api/java/util/Comparator.html )可以比较两个段,两个阵营或两者的混合比较日期然后Collections.sort(segmentAndCampList, new SegmentAndCampComparator())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM