【发布时间】:2014-05-25 16:10:01
【问题描述】:
我有一个集合,它的键是字符串和字符串类型的列表,我想从这个集合和列表中创建一个 HashMap。该集合包含类别,列表包含属于这些类别的地点。以下是我尝试过的
Set<Entry<String, Integer>> setUserPreferences = sortedUserPreferences.entrySet();
Map<String , Integer> sortedPlaces = new HashMap<String, Integer>();
List<String> topPlaces = new ArrayList<String>();
Map<String , List<String>> finalPlaces = new HashMap<String, List<String>>();
for (Entry<String, Integer> entry : setUserPreferences) {
sortedPlaces = placesService.sortPlaces(categorisedPlaces.get(entry.getKey()));
int counter = 0;
for (Map.Entry<String, Integer> sortedplace : sortedPlaces.entrySet()) {
topPlaces.add(sortedplace.getKey());
counter++;
if(counter == 5){
sortedPlaces.clear();
break;
}
}
finalPlaces.put(entry.getKey(), topPlaces);
topPlaces.clear();
}
首先,我遍历集合,对于每个键,我得到排序的地方,在排序的地方中,我选择每个类别的前 5 个地方,最后我把它放在地图中,其中键是类别,值是该类别中排名前 5 位的列表。
我需要为集合中的每次迭代清除 topPlaces 列表,因为我不想让一个类别中的地点出现在另一个类别中,但是一旦我将列表放入 map(finalPlaces) 并清除列表它也会清除地图值。
如何在不清除地图值的情况下清除列表。
谢谢
【问题讨论】:
标签: java list arraylist hashmap set