Sempiternal
Sempiternal

Reputation: 127

Make A Hashmap with its keys being Strings values being HashSets

Trying to make a Hash Map with keys of strings, and values of HashSets. I want the hash sets to be all integers

Set<String> numbersSet = new HashSet<Integer>();

// won't work:
HashMap<String, numberSet> database = new HashMap<String, numberSet>();//error - "( or [ expected"

//Also won't work. If it did, how can even I add to the set inside this hashMap?
HashMap<String, HashSet<Integer>> database = new HashMap<String, HashSet<Integer>>(); //error-incompatible types

Upvotes: 1

Views: 2221

Answers (1)

danieln
danieln

Reputation: 4973

HashMap<String, HashSet<Integer>> database = new HashMap<String, HashSet<Integer>>();

Works for me.

BTW, if you are using JDK 1.7 you can use:

HashMap<String, HashSet<Integer>> mymap = new HashMap<>();

Upvotes: 1

Related Questions