Java 浏览多个列表以查找相同的对象

Java 浏览多个列表以查找相同的对象,java,list,duplicates,Java,List,Duplicates,我想知道是否有两条公交线路在交叉(有公共公交车站),然后在列表中返回这些线路和公交车站。 总线类没有返回停止列表以使用列表操作的方法 例如:我在总线列表的第一个元素上->我得到该行的第一个总线站->现在检查每隔一个总线列表元素,如果它的列表上包含相同的总线站 busLineList -> busLine 1 -> busStop A busStop B -> busLine 2 -> bus

我想知道是否有两条公交线路在交叉(有公共公交车站),然后在列表中返回这些线路和公交车站。 总线类没有返回停止列表以使用列表操作的方法

例如:我在总线列表的第一个元素上->我得到该行的第一个总线站->现在检查每隔一个
总线列表
元素,如果它的列表上包含相同的总线站

busLineList -> busLine 1 -> busStop A
                            busStop B

            -> busLine 2 -> busStop B


saving to the list: [ busLine 1, busLine 2, busStop B ]
我的实现返回了错误的列表返回列表的
size()
是我在所有公交线路中使用的每个公交车站的总和。
if()语句有问题,因为当我用
true
替换条件时,它产生了相同的输出。

/* This list consist of `BusLineInterface` objects which constructor 
   takes a `List` of `BusStopInterface` objects as parameter.
*/
private static List<BusLineInterface> busLineList;

/* This is `transferList` I want to create which contains `Lists`
   of objects: bus x of line "x" that have same busStop with bus y on 
   line y, bus y, common bus stop
*/
private static List<List<Object>> transferList;

public PathFinder() {

    busList = new ArrayList<>();
    busLineList = new ArrayList<>();
    transferList = new ArrayList<>();
}

public void transferTab() {

    for (int i = 0; i < busLineList.size(); i++) {

        for (int j = 0; j < busLineList.get(i).getNumberOfBusStops(); j++) {

            for (int k = 0; k < busLineList.size(); k++) {
                boolean flag = true;
                if (i == k) { // Avoiding of checking same Lines
                    flag = false;
                }
                if (flag) {

                    for (int l = 0; l < busLineList.get(k).getNumberOfBusStops(); l++) {

                        if (busLineList.get(i).getBusStop(j).getName().equals(busLineList.get(k).getBusStop(l).getName())) {
                            List<Object> transfer = new ArrayList<>();

                            transfer.add(busLineList.get(i));
                            transfer.add(busLineList.get(k));
                            transfer.add(busLineList.get(k).getBusStop(l));
                            transferList.add(transfer);
                        }
                    }
                }
            }
        }
    }
}
巴士线:

private static List<BusStop> busStore;

BusLine(List<BusStop> b) {

    busStore = new ArrayList<>(b);
}

@Override
public int getNumberOfBusStops() {

    return busStore.size();
}
@Override
public BusStopInterface getBusStop(int number) {

    return busStore.get(number);
}
私有静态列表总线存储;
母线(列表b){
总线存储=新阵列列表(b);
}
@凌驾
公共int GetNumberOfBussstops(){
返回busStore.size();
}
@凌驾
公共总线停止接口getBusStop(整数){
返回busStore.get(编号);
}

听起来你在描述两个列表之间的交集

这将有助于:

List<String> list1 = ...
List<String> list2 = ...

Set<String> intersection = list1.stream()
                               .filter(item -> list2.contains(item))
                               .collect(Collectors.toSet());
列表列表1=。。。
列表2=。。。
设置交叉点=list1.stream()
.filter(项目->列表2.contains(项目))
.collect(收集器.toSet());

概念是一样的。内部“.contains”使用“.equals”检查项目是否已在列表中。如果这还不够,请使用getName()方法按名称将一个停止与另一个停止进行比较。详情如下:

    // Convert the list of bus stops to a set of names.
    Set<String> line1Stops = busLine1.stream()
                                 .map(stop -> stop.getName())
                                 .collect(Collectors.toSet());

    // Create an intersection set by comparing the names in
    // list 1 against the names in list 2.
    Set<String> intersection = busLine2.stream()
                                 .map(stop -> stop.getName())
                                 .filter(name -> line1Stops.contains(name))
                                 .collect(Collectors.toSet());
//将公交站点列表转换为一组名称。
设置line1Stops=busLine1.stream()
.map(停止->停止.getName())
.collect(收集器.toSet());
//通过比较中的名称来创建交点集
//对照清单2中的名称列出清单1。
设置交叉点=总线2.stream()
.map(停止->停止.getName())
.filter(名称->line1Stops.contains(名称))
.collect(收集器.toSet());

您是否尝试使用包含列表的方法?包含(对象o)->它返回布尔值value@Shivaraj我不能在这里使用
contains
,因为我在总线中没有返回其总线停止列表的方法,我只能使用
getNumberOfBusStops
和'getBusStop',不幸的是,我无法编辑该类以返回整个列表,但我知道这样会容易得多,问题是我不能在总线上使用“.contains”,因为该类没有返回构造函数中给定列表的方法。它只包含公交站点数量和具体公交站点的getter,因此我需要手动迭代每个公交站点,我无法添加此方法(返回列表),但我知道这样会更容易。也许您可以共享BusInterface和busineenteface代码。我刚刚做了:)我认为您应该重新表述您的问题。你想干什么?(在公交车、公交线路和公交车站的情况下)。我想知道同一个车站是否有两条不同的公交线路提供服务。谢谢你的建议。我再次编辑了这个问题。我希望现在更清楚。我不知道如何使它工作,因为
busLine1.stream()
假设这是一个
列表
,但它是一个对象,参数是一个
列表
,我无法检索它(我必须在每个公交车站手动迭代),您可以使用迭代为每条公交线路构建一个列表吗?然后对每个列表进行评估。
    // Convert the list of bus stops to a set of names.
    Set<String> line1Stops = busLine1.stream()
                                 .map(stop -> stop.getName())
                                 .collect(Collectors.toSet());

    // Create an intersection set by comparing the names in
    // list 1 against the names in list 2.
    Set<String> intersection = busLine2.stream()
                                 .map(stop -> stop.getName())
                                 .filter(name -> line1Stops.contains(name))
                                 .collect(Collectors.toSet());