如何将列表对象的项目放置到另一个列表中

时间:2019-05-25 11:36:07

标签: java

我有两个列表,根据第一个列表的条件,我想将其添加到第二个列表中。

我想将i索引的记录放到新列表中,这是从我进行分配的那一行跳过的。 newFeedDetailsList =(List)feedDetailsList.get(i);

列出feedDetailsList = getFeedDetails();         列出newFeedDetailsList = new ArrayList();         if(feedDetailsList!= null || feedDetailsList.size()!= 0){

        // ************************************************************************88
        for (int i = 0; i < feedDetailsList.size(); i++) {
            for (int j = i + 1; j < feedDetailsList.size(); j++) {

                if (feedDetailsList.get(i).getCircuitId().equals(feedDetailsList.get(j).getCircuitId())) {
                    if (feedDetailsList.get(i).getMeasurementMaxHourly() == feedDetailsList.get(j)
                            .getMeasurementMaxHourly()) {
                        if (feedDetailsList.get(i).getMeasurementDirection().equals("Ingress")) {
                             newFeedDetailsList = (List<BandwidthAlertFeedDetailDTO>)feedDetailsList.get(i);
                        } else {
                            newFeedDetailsList = (List<BandwidthAlertFeedDetailDTO>) feedDetailsList
                                    .get(j);
                        }
                    } else if (feedDetailsList.get(i).getMeasurementMaxHourly() > feedDetailsList.get(j)
                            .getMeasurementMaxHourly()) {
                     newFeedDetailsList = (List<BandwidthAlertFeedDetailDTO>) feedDetailsList
                                .get(i);

                    } else {
                    newFeedDetailsList = (List<BandwidthAlertFeedDetailDTO>) feedDetailsList
                                .get(j);

                    }
                }
            }
        }

}

我的BandwidthAlertFeedDetailDTO类使用getter和setter定义如下。 公共类BandwidthAlertFeedDetailDTO {

private Long rowId;
private String feedId;
private Long gcpOrgId;
private String serviceName;
private Long circuitPok;
private String circuitId;
private Long locationId;
private Long addressId;
private Timestamp alertGeneratedOn;
private String granularity;
private String measurementType;
private String measurementDirection;
private String measurementPeriod;
private Date measurementStartDatetime;
private Date measurementEndDatetime;
private int threshold;
private int thresholdMetCount;
private Timestamp alterExpiryOn;
private String status;
private String thresholdConfigType;
private int measurementMaxHourly;
private String emailNotificationTransactionId;
private Timestamp emailNotificationSentTs;
private String textNotificationTransactionId;
private Timestamp textNotificationSentTs;

}

我想知道如何分配值。

1 个答案:

答案 0 :(得分:0)

您正在尝试将BandwidthAlertFeedDetailDTO对象投射到List<BandwidthAlertFeedDetailDTO>对象。这是不可能的,该对象的类型为BandwidthAlertFeedDetailDTO,而不是列表。您要做的是创建要填充的新列表,并在该列表上使用add()方法将要添加的对象添加到列表中。代码应如下所示:

List<BandwidthAlertFeedDetailDTO> newFeedDetailsList = new ArrayList<BandwidthAlertFeedDetailDTO>();
...
for loops here {
    ...
    BandwidthAlertFeedDetailDTO some_object_you_want_to_add = feedDetailsList.get(...);
    ...
    newFeedDetailsList.add(some_object_you_want_to_add);
    ...
}