我目前正在尝试合并两个列表,同时删除其中的重复值。然而,当这样做时,添加每个站点并假设 mapValue 列表不包含任何站点,即使它显然包含(我最终有很多重复项)。我究竟做错了什么?
Map<String, List<Stations>> overview = new TreeMap<String, List<Stations>>();
for(Trains train: trainOverview){
List<Stations> mapValue = overview.get(train.getTrainNumber());
//Merge lists if the key already exists, and replace the old value with the merged list
if(overview.containsKey(train.getTrainNumber())){
for(Stations station: train.getStations()){
if(!mapValue.contains(station)) mapValue.add(station);
}
overview.put(train.getTrainNumber(), mapValue);
}
//If no key exists, create a new entry
else{
overview.put(train.getTrainNumber(), train.getStations());
}
}