Take the 2-minute tour ×

I've been hunting around for a way to do this and it is driving me mad!

What I'm trying to do is as follows:

I have a HashMap:

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

The values I'm assigning to the hashmap are:

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"}

the key sting is the name of an event and the List is a list of competitors. I want to build a new list of all the competitors.

So I've tried various things like:

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

and then iterating through the hashmap:

Collection allCompetitors = qualifiedCompetitors.values();

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

To see what vales I get, which is fine, but I cannot then add them a new list? I know I'm missing something obvious any suggestions?

share|improve this question
Why can't you add them to the list? What code doesn't work, and why doesn't it work? –  Greg Kopff Apr 18 '12 at 7:52
so If I try: ListOfAllqualifiedCompetitors = new ArrayList(qualifiedCompetitors.values()); I cannot compile (cannot find symbol). and if try and iterate using something like the above I get each value back as an incompatible type. –  damagedgoods Apr 18 '12 at 7:57
Do you want one list of all competitors, or a list of a list of competitors? For the former, see my answer below ... –  Greg Kopff Apr 18 '12 at 7:59

2 Answers 2

up vote 1 down vote accepted

What I think you're saying is that you have a map of String to List<String> and you want to accumulate all of the List<String> into a single long list. If that's the case:

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

eventsToCompetitors = ...

allCompetitors = new ArrayList<String>();
for (Collection<String> competitors : eventsToCompetitors.values())
share|improve this answer
got it - makes more sense now... cheers –  damagedgoods Apr 18 '12 at 8:05
I am then using this new list to create a treeset as this will remove the duplicates and sort them, would it make more sense to create a treeset first and populate that rather than the list? –  damagedgoods Apr 18 '12 at 8:16
@damagedgoods, that's what Sets are for afterall. They wouldn't be much good if they didn't remove duplicates. Though unless you have a reason to, use HashSet rather than TreeSet. –  Neil Apr 18 '12 at 8:48
@Greg_Kopff, Map.values() returns a Set, not a List, so this wouldn't compile. –  Neil Apr 18 '12 at 8:49
@Neil: ah yes, values() returns a Collection - I've edited the answer. –  Greg Kopff Apr 18 '12 at 20:34
List<String> allCompetitors = new ArrayList<String>();
for(Set<String> competitors : qualifiedCompetitors.values()) {

Is this what you mean?

share|improve this answer
yup - that worked if I changed the for each from Set<String> to List<String>. Thx –  damagedgoods Apr 18 '12 at 8:19

Your Answer


