0

我一直在寻找一种方法来做到这一点,这让我发疯!

我正在尝试做的事情如下:

我有一个HashMap:

qualifiedCompetitors = new HashMap<String, List<String>>();

我分配给哈希图的值是:

String[] events = {"Helicopter", "Amphibious", 
                     "4x4", "Fire Engine"};

String[][] competitors = 
{
   {"Greg", "Will", "Fleur", "Bill", "Bella", "Sally", "Olive", "Sal", "Dora", "Chas"}, 
   {"Yuri", "Abe", "Tim", "Fleur", "Bonnie", "Vera", "Ed", "Dan", "Jill", "Rose", "Zoe"}, 
   {"Tim", "Fleur", "Will",  "Bill", "Bella", "Dan", "Jill", "Rose", "Greg",  "Abe", "Sally", "Yuri", "Olive", "Sal", "Jim"}, 
   {"Jill", "Rose", "Greg", "Abe", "Bonnie", "Vera", "Ed", "Zoe", "Ben", "Freda", "Chuck", "Fred"}
};

关键是事件的名称,而 List 是竞争对手的列表。我想建立一个所有竞争对手的新列表。

因此,我尝试了各种方法,例如:

ListOfAllqualifiedCompetitors = new ArrayList(qualifiedCompetitors.values());

然后遍历哈希图:

Collection allCompetitors = qualifiedCompetitors.values();

System.out.println("Values of Collection created from Hashtable are :");
//iterate through the collection
Iterator itr = allDrivers.iterator();
while(itr.hasNext())
  System.out.println(itr.next());

看看我得到了什么值,这很好,但我不能再给它们添加一个新列表?我知道我遗漏了一些明显的建议?

4

2 回答 2

1

认为您的意思是您有一张 to 的地图,String并且List<String>您希望将所有内容累积List<String>到一个长列表中。如果是这样的话:

final Map<String, List<String>> eventsToCompetitors;
final List<String> allCompetitors;

eventsToCompetitors = ...

allCompetitors = new ArrayList<String>();
for (Collection<String> competitors : eventsToCompetitors.values())
{
  allCompetitors.addAll(competitors);
}
于 2012-04-18T07:55:01.777 回答
0
List<String> allCompetitors = new ArrayList<String>();
for(Set<String> competitors : qualifiedCompetitors.values()) {
    allCompetitors.addAll(competitors);
}

你是这个意思吗?

于 2012-04-18T07:55:54.913 回答