我有这张地图:
Map<City, TreeSet<Individual>> cityIndividualMap = new HashMap<City, TreeSet<Individual>>();
我使用此函数填充此地图:
public void add(List<Individual> individuals){
for (Individual individual : individuals){
individualSortedSet.add(individual);
for(City city:individual.getCities()){
TreeSet<Individual> individualList;
if (cityIndividualMap.containsKey(city))
individualSet = cityIndividualMap.get(city);
else
individualSet = new TreeSet<Individual>( new FitnessComparator());
individualSet.add(individual);
cityIndividualMap.put(city, individualSet);
}
}
}
这是我的比较器:
public class FitnessComparator implements Comparator<Individual> {
@Override
public int compare(Individual individual1, Individual individual2) {
if (individual1.getFitness() == individual2.getFitness())
return 0;
return (individual1.getFitness() > individual2.getFitness())? 1 : -1;
}
}
单个类只是一个数据类..所以我不会在这里复制它。
由于一些非常奇怪的原因,CityIndividualMaps 值只需要一个元素!我已经在调试模式下执行了很多次,但看不到为什么只能添加一个项目。请你检查一下吗?