11

One more question about HashMap<> in Java:

I have the following

Map<String, Set<Integer>> myWordDict = new HashMap<String, Set<Integer>>();

After storing data into the variable myWordDict, I want to iterate through the HashMapValues, and add each value to a new Set variable?

When I try to do Set<Integer> newVariable = myWordDict.entrySet(), it seems the data type is incompatible.

So my question is essentially:

how to convert HashMap values or entrySet() to Set ?

Thanks

6
  • 4
    What is the map value type? Commented Nov 12, 2013 at 6:14
  • Do you want all the key-value combinations as a set? Or do you only want the keys or values? Commented Nov 12, 2013 at 6:14
  • I wish to have only the values in the Set<Integer>. The value type is of Integer. Commented Nov 12, 2013 at 6:18
  • @TonyGW You say the value type is Integer, but in the above example the value type is actually Set<Integer>. Commented Nov 12, 2013 at 6:21
  • yes, the value is Set<Integer>. sorry for the confusion Commented Nov 12, 2013 at 6:22

9 Answers 9

25

Try:

Set<Integer> newVariable = mywordDict.keySet();

or

Set<Integer> newVariable = new HashSet<Integer>(myWordDict.values());
Sign up to request clarification or add additional context in comments.

Comments

11

Your declaration should be like below. It will convert your map values to Collection

  Collection<Set<Integer>> newVariable = myWordDict.values();

1 Comment

Are you sure this will work?! Firstly, you'll have to cast it and even if you do that, it'll throw a ClassCastException.
5

What does entrySet returns?

public Set<Map.Entry<K,V>> entrySet()

Returns a Set view of the mappings contained in this map. The set is backed by the map, so changes to the map are reflected in the set, and vice-versa.

If you want iterate over the myWordDict hash map then just

for(Map.Entry<String, Set<Integer>> entry : myWordDict.entrySet())
{
   Set<Integer> newVariable = entry.getValue();
}

Related links

1 Comment

I am now using an iterator to iterate through the HashMap value:
4

If you need all the values as a Set of Integers and not as a Set of Set, then you could do something like this

Set<Integer> newVariable = new HashSet<Integer>();
for (Set<Integer> set : myWordDict.values()) {
    newVariable.addAll(set);
}

Or if you want them as a Set of Set, you need to do something like this

Set<Set<Integer>> newVariable = new HashSet<Set<Integer>>();
newVariable.addAll(myWordDict.values());

Comments

3

flatMap will help you:

myWordDict.values().stream().flatMap(Set::stream).collect(Collectors.toSet());

Comments

1

use myWordDict.values(), not myWordDict.entrySet(). values is a set of the map's values, whereas entrySet is a set of its mappings (it is a set of java.util.map.Entry objects, each of which describes a key and a value).

2 Comments

values() is of Collections type, so there's also data incompatibility problem unless I do an ugly cast.
@TonyGW AFAIK there's no way around that, unless you want to construct the set manually by adding every value from the entry set.
1

Assuming that you want to add all values from the sets in the map your approach to do it is to use addAll method:

Set<Integer> newVariable = new HashSet<Integer>();
for (Set<Integer> set : myWordDict.values()) {
    newVariable.addAll(set);
}

1 Comment

@TonyGW it runs accordingly to the number of entries in your myWordDict - if you have 10 entries, for loop will make 10 iterations.
1
Map<Integer,String>   map1 = new HashMap<Integer,String>();

map1.put(1, "Rakesh");
map1.put(2, "Amal");
map1.put(3, "Nithish");

Set<Entry<Integer,String>> set1 = map1.entrySet();

Comments

0

So, I came up with an iterator to iterate through the value Set

I have this data structure

Map<String, Set<Integer>> myWordDict = new HashMap<String, Set<Integer>>();

I created

Iterator mapIterator = myWordDict.entrySet().iterator();

Then just use a while() loop to add individual value to a new Set variable. I am not sure this is the most efficient way, but it works for my needs.`

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.