I'm trying to create a HashMap from values of a HashSet (which itself is stored as values in a HashMap).
I'm not sure whether to Iterate or use a for loop.
Example:
newMap = new HashMap<String, HashSet<String>>();
HashSet<String> boxing = new HashSet<String>();
newMap.put("fighting", boxing);
boxing.add("jab");
boxing.add("hook");
boxing.add("uppercut");
Now I need to iterate or loop through so that each value in the 'boxing' HashSet creates a new HashMap with the value as the key for the new map.
So newMap1 would have 'jab' as the key, newMap2 would have 'hook' as the key and so on.
Any help is appreciated.
Since Java 9 you can do this:
Map<String, Set<String>> newMap = Map.of("fighting", Set.of("jab","hook","uppercut"));
Related
Hi i have a ArrayList of HashMap and i need the HashMap to be sorted by its key,Value.
ArrayList<HashMap> newList = new ArrayList();
loop start:
HashMap hashData = new HashMap();
hashData.put("name", "string-studentname");
hashData.put("mark", "int-studentmark");
newList.add(hashData);
loop end:
I need the newList to be sorted by the key-mark.
How do i get it?
If you have multiple entries in your ArrayList and the key is the same, you can just sort via:
newList.sort(Comparator.comparingInt(o -> o.get("mark")));
Assuming you're typing the map properly:
HashMap<String, Integer> hashData = new HashMap<>();
hashData.put("mark", 1);
When i try to add hashMap to another HashMap i lose old varaible. How can i fix this problem? My code is something like that.
HashMap<String, String> tmp = new HashMap<String, String>();
HashMap<String, String> map = new HashMap<String, String>();
tmp = ((HashMap<String, String>)intent.getSerializableExtra("map"));
map.putAll(tmp);
when i use this code map elements always equals tmp elements. It is not stored old elements.
Thanks for help.
HashMap<String, String> map = new HashMap<String, String>(); creates a new, empty, HashMap instance. Therefore after a call to map.putAll(tmp), your map would only contain the entries of tmp.
If map has previous entries, you shouldn't assign a new instance to this variable.
That said, even if map had previous entries, putting the entries of tmp in it would overwrite the values of all the keys that exist in both map and tmp.
Once you have updated map to not be re-initialized every time, you can avoid overwriting any existing key-value pairs in map by looping as follows:
for (String key : tmp.keySet()) {
if (!map.containsKey(key)) {
map.put(key, tmp.get(key);
}
}
I'm not sure from your question if this is exactly what you are asking, but it may be useful. Eran's suggestion (using HashMap<String, List<String>>) is also a good one if you want to store multiple Strings per key.
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
Try:
Set<Integer> newVariable = mywordDict.keySet();
or
Set<Integer> newVariable = new HashSet<Integer>(myWordDict.values());
Your declaration should be like below. It will convert your map values to Collection
Collection<Set<Integer>> newVariable = myWordDict.values();
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
Map.Entry#getValue()
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());
flatMap will help you:
myWordDict.values().stream().flatMap(Set::stream).collect(Collectors.toSet());
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).
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);
}
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();
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.`
Given the following Java HashMap:
HashMap<String, Integer> map = new HashMap<String, Integer>();
The following statement gives me a 'backed' set of the maps keys:
Set<Integer> keys = map.keySet();
but suppose I'd like a copy of the key set which I can manipulate without affecting the map. Is there a better/more correct way than
Set<Integer> keys = new HashSet<Integer>();
for( Integer key : map.keySet() )
keys.add( key );
?
There's a slightly simpler way:
Set<Integer> keys = new HashSet<Integer>(map.keySet());
...which just makes a copy in a single line.
Set<Integer> keys = new HashSet<Integer>(map.keySet());
This will initialise a Set which contain exactly the same elements in the map's key set which is passed into the HashSet constructor.
I am working in Java, and have declared two maps as follow:
private Map<MyCustomClass, Integer> map1, map2;
map1 = new HashMap<MyCustomClass, Integer>();
map2 = new HashMap<MyCustomClass, Integer>();
//adding some key value pair into map1
//adding some key value pair into map2
private ArrayList<MyCustomClass> list = new ArrayList<MyCustomClass>();
Now i want to insert the keys of both map in the above declared ArrayList. Is there any built-in method exist for this or i need to writes some custom code?
To add everything:
list.addAll(map1.keySet());
list.addAll(map2.keySet());
To add only unique keys:
Set<MyCustomClass> keys = new HashSet(map1.keySet());
keys.addAll(map2.keySet());
list.addAll(keys);
References: List.addAll(Collection c);
HashMap.keySet()
list.addAll(map1.keySet());
list.addAll(map2.keySet());
keySet() gets all the keys from the map and returns them as a set. The addAll then adds that set to your list.