I am currently trying to merge two lists while removing the duplicate values inside of them. However when doing this, adds every station and assumes that the mapValue List does not contain any of the stations even though it clearly does (Im ending up with a lot of duplicates). What am I doing wrong?
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());
}
}