In this problem, I have to find all matching key/value mappings within two maps and then return it into a new map, but I am running into some problems. My idea is to find all matching keys from the two maps, then use these keys to reference it to the values. If the values matched, I would put the key/value into the map. I am trying to find out why it just adds all the keys that are in common; it only add those keys if its corresponding values match too. Thanks.
The prompt:
Write a method intersect that takes two Maps of strings to integers as parameters and that returns a new map whose contents are the intersection of the two. The intersection of two maps is defined here as the set of keys and values that exist in both maps. So if some key K maps to value V in both the first and second map, include it in your result. If K does not exist as a key in both maps, or if K does not map to the same value V in both maps, exclude that pair from your result. For example, consider the following two maps:
{Janet=87, Logan=62, Whitaker=46, Alyssa=100, Stefanie=80, Jeff=88, Kim=52, Sylvia=95}
{Logan=62, Kim=52, Whitaker=52, Jeff=88, Stefanie=80, Brian=60, Lisa=83, Sylvia=87}
Calling your method on the preceding maps would return the following new map (the order of the key/value pairs does not matter):
{Logan=62, Stefanie=80, Jeff=88, Kim=52}
My code:
// we need to store the keys, then get the values in common, then put the key/map into map
public static Map<String, Integer> intersect(Map<String, Integer> first, Map<String, Integer> second) {
Map<String, Integer> output = new HashMap<String, Integer>(first); // combined output
Set<String> keyFirst = new HashSet<String>(); // stores all the keys for first
for (String key: first.keySet()) { // goes through each key in input
keyFirst.add(key); // adds all keys from first into keyFirst
}
// goes through each key in common and checks to see if they reference to the same value
Iterator<String> keyFirstItr = keyFirst.iterator();
while (keyFirstItr.hasNext()) {
String keyTemp = keyFirstItr.next();
if (first.get(keyTemp) == second.get(keyTemp)) { // If same key, same value mapped
output.put(keyTemp, first.get(keyTemp)); // add key value to map
}
}
return output;
}
You are putting all the values from first to output by passing it to the constructor.
Map<String, Integer> output = new HashMap<String, Integer>(first); // you are passing first to the constructor.
You don't need to create another Set, keySet() method returns set so the below lines not required.
Set<String> keyFirst = new HashSet<String>(); // stores all the keys for first
for (String key: first.keySet()) { // goes through each key in input
keyFirst.add(key); // adds all keys from first into keyFirst
}
Here's the correct implemetataion.
// we need to store the keys, then get the values in common, then put the key/map into map
public static Map<String, Integer> intersect(Map<String, Integer> first, Map<String, Integer> second) {
Map<String, Integer> output = new HashMap<String, Integer>(); // combined output
// goes through each key in common and checks to see if they reference to the same value
Iterator<String> keyFirstItr = first.keySet().iterator();
while (keyFirstItr.hasNext()) {
String keyTemp = keyFirstItr.next();
if (first.get(keyTemp).equals(second.get(keyTemp))) { // If same key, same value mapped
output.put(keyTemp, first.get(keyTemp)); // add key value to map
}
}
return output;
}
Simpler solution to this exercise is to skip iterator and use for loop as below. For every name in map1 we check if it exists in map2 and if the values are matching. Then the K, and V are added to the new map:
public static Map intersect(Map<String, Integer> map1, Map<String, Integer> map2){
Map<String, Integer> newMap = new HashMap<>();
for (String name : map1.keySet()){
if(map2.containsKey(name) && map1.get(name).equals(map2.get(name))){
newMap.put(name, map1.get(name));
}
}
return newMap;
}
Related
How do i get both of the values from each map into another map? One map has the name of the ingredient as key. keywordsToIds has the ID as value and firstCounter has the occurance of the ingredient as value. I want to have a map of ID as key and occurance as value. The keys work but the values don't. I hope someone can help me out. I am very new to maps and arraylists.
Map<String, Long> keywordsToIds
Map<String, Integer> firstCounter
Map<Long, Integer> idAndCount = new HashMap<>();
for (Map.Entry<String, Integer> entry : firstCounter.entrySet())
if (keywordsToIds.containsKey(entry.getKey())){
idAndCount.put(keywordsToIds.get(entry.getKey()), firstCounter.get(entry.getValue()));
}
return idAndCount;
#Test
#DisplayName("can detect multiple occurrences of ingredients")
void testCounting() {
// Input-Daten:
String inputLine = "Ich hätte gerne einen Vollkorn Burger mit Cheddar-Käse Cheddar-Käse und noch mehr Cheddar-Käse";
Map<String, Long> keywordsToIds = Map.of(
"Vollkorn", 19L,
"Cheddar-Käse", 87L,
"Rindfleisch", 77L);
Map<Long, Integer> expected = Map.of(
19L, 1,
87L, 3);
Map<Long, Integer> actual = sut.idsAndCountFromInput(inputLine, keywordsToIds);
assertEquals(expected, actual);
}
expected: <{19=1, 87=3}> but was: <{19=null, 87=null}>
Expected :{19=1, 87=3}
Actual :{19=null, 87=null}
I have tried the loop above, where i say if the key of the one map contains the key of the other map, put the value of keywordsToIds as key and value of firstCounter as value.
I'm assuming you're trying to have 2 maps that hold the same strings as the keys, while having an additional map corresponding to an Integer occurrence and a Long ID.
To make a Map which holds an Integer occurrence as the key and a Long ID as the value, I believe you have two options, either manually going through each value set, or creating a custom class to hold your two properties.
Option One: Creating a custom class
You could create a class representing the thing your trying to store, and create a Map of your Object. For the sake of simplicity, lets say you are trying to have two maps represent IDs. Each ID Object represents the ID itself, and how frequently its been used (the occurrence).
You could create your class like this:
public class ID {
long id = 0L;
int occurrence = 0;
public ID(long id, int uses) {
this.id = id;
occurrence = uses;
}
public long getID() {
return(id);
}
public int getOccurs() {
return(occurrence);
}
}
Then, make all the ID objects you need and add them into either a Map, if you still want the string representation, as Map<String, ID>, or just create a list, because the ID object stores all the properties you were previously storing in the map.
Option Two: Manually Getting Each Value
You could get each of the value Collections and loop through them at the same time by iterating through each list to add each Long and Integer to the same map.
Map<String, Long> keywordsToIds = new HashMap<String, Long>();
Map<String, Integer> firstCounter = new HashMap<String, Integer>();
String[] list = new String[] {"wordA", "wordB", "wordC"};
long[] longs = new long[] {12L, 14L, 15L};
int[] ints = new int[] {123, 456, 789};
for (int i=0; i<list.length; i++) {
keywordsToIds.put(list[i], longs[i]); // Adds each word pointing to a Long
firstCounter.put(list[i], ints[i]); // Adds each word pointing to an Integer
}
Map<Long, Integer> idsToOccurrence = new HashMap<Long, Integer>();
ArrayList<Long> ids = new ArrayList<Long>(keywordsToIds.values()); // Retrieve the values from the ID map
ArrayList<Integer> occurrence = new ArrayList<Integer>(firstCounter.values()); // Retrieve the values from the Occurrence map
for (int i=0; i<ids.size(); i++) { // Loop through each, value set
idsToOccurrence.put(ids.get(i), occurrence.get(i)); // Add the values to the ID/Occurrence map
}
for (String key : keywordsToIds.keySet()) { // Loops through each item in the map(s)
Long longID = keywordsToIds.get(key);
System.out.println(key+" --> "+longID+" --> "+idsToOccurrence.get(longID)); // Displays the keys pointing to each other
}
Hope this helps clarify things for you. If you're still a bit confused about Maps, take a look here.
I got it. I actually just had to use "getKey" on both, instead of get Value for the second map. Actually sat down hours for this simple change. Pretty new to maps so got confused with getKey and getValue
idAndCount.put(keywordsToIds.get(entry.getKey()), firstCounter.get(entry.getKey()));
i am having map1 as <k1,v1> and i have to create map2 with map1 as a value like map2=<k3,map1>.
But keys k1 ans k3 are having duplicate and we have to retain duplictes.
Example:
map1={(1,a),(1,b),(2,c)}
map2={5={1,a},5={1,b},6={2,c}}
How to achieve this using hashmaps or maps(without using guava's multimap concept)
As HashMap doesn't allow to store duplicate keys, you might want to consider changing your code a bit and create HashMap<Key,ArrayList<HashMap>>. Maps with the same key would be stored under the same Key in the ArrayList of your HashMaps. "ArrayList of HashMaps" would be the value of parent HashMap. There is a simple example how I see you could achieve something similar to containing more values to duplicate keys (I used hardcoded key values to make it a bit simpler to read, I also added some explanations in the comments to the code):
import java.util.*;
public class A {
public static void main(String[] args) {
Map<Integer, ArrayList<Character>> map1 = new HashMap<>(); // Map containing few Characters under one key
map1.put(1, new ArrayList<Character>());
map1.get(1).add('a');
map1.get(1).add('b');
map1.put(2, new ArrayList<Character>());
map1.get(2).add('c');
System.out.println("map1: " + map1); // prints: map1: {1=[a, b], 2=[c]}
Map<Integer, ArrayList<HashMap<Integer, Character>>> map2 = new HashMap<>(); // Map storing as keys 5 and 6, values are maps from map1
map2.put(5, new ArrayList<HashMap<Integer, Character>>());
map2.put(6, new ArrayList<HashMap<Integer, Character>>());
for(Map.Entry<Integer, ArrayList<Character>> entry : map1.entrySet()) { // For each Integer-ArrayList pair from map1...
if(entry.getKey().equals(1)) { // Check if key Integer is equal to 1, if it is...
for(Character character : entry.getValue()) { // Create Maps containg pairs of Integer as key and each Character as value...
HashMap<Integer, Character> innerMap = new HashMap<>();
innerMap.put(entry.getKey(), character);
map2.get(5).add((new HashMap<Integer,Character>(innerMap)));
}
}
if(entry.getKey().equals(2)) { // Check if key Integer is equal to 1, if it is...
for(Character character : entry.getValue()) { // Create Maps containg pairs of Integer as key and each Character as value...
HashMap<Integer, Character> innerMap = new HashMap<>();
innerMap.put(entry.getKey(), character);
map2.get(6).add((new HashMap<Integer,Character>(innerMap)));
}
}
}
System.out.println("map2: " + map2); // prints: map2: {5=[{1=a}, {1=b}], 6=[{2=c}]}
}
}
Output you get:
map1: {1=[a, b], 2=[c]}
map2: {5=[{1=a}, {1=b}], 6=[{2=c}]}
You can use result Map and combination of getKey() and getValue() methods to create Maps like 5={1,a} and 5=[{1=b} on-the-go (but be aware that you cannot store them in the same Map).
Maps don't allow more than one mapping for any given key.
What you can do, though, is to have a Map<K1, List<V1>> for map1, which maps keys to lists of values.
To add an entry, instead of using Map.put, use Map.computeIfAbsent:
map1.computeIfAbsent(1, k -> new ArrayList<>()).add("a");
map1.computeIfAbsent(1, k -> new ArrayList<>()).add("b");
map1.computeIfAbsent(2, k -> new ArrayList<>()).add("c");
This has the following structure:
map1: {1=[a, b], 2=[c]}
Now, for map2, it's not clear if you'd need to take the same approach or if you'd be done using Map.put.
public class MapCheck {
public static void main(String[] args) {
Map<String, String> data = new HashMap<String, String>();
data.put("John", "Taxi Driver");
data.put("Mark", "Professional Killer");
Map<String, String> data1 = new HashMap<String, String>();
data1.put("John", "Driver");
data1.put("Mark", "Murderer");
Map<String, String> data3 = new HashMap<String, String>();
data3.putAll(data);
data3.putAll(data1);
System.out.println(data3);
}
}
I have few maps which contains same key, their values are different. I want to merge them. But when I merge them with the usual putAll() it gives me only the value of the key which was inserted latest.
Output from above code is {John=Driver, Mark=Murderer}
Is there a method which will get me all the values associated with the key and give me as a array like
{John=[Taxi Driver, Driver], Mark=[Professional Killer, Murderer]}
You can produce a Map<String, List<String>> quite easily with Java 8 Streams:
Map<String, List<String>>
merged =
Stream.of(data,data1) // create a Stream<Map<String,String> of all Maps
.flatMap(map->map.entrySet().stream()) // map all the entries of all the
// Maps into a
// Stream<Map.Entry<String,String>>
.collect(Collectors.groupingBy(Map.Entry::getKey, // group entries by key
Collectors.mapping(Map.Entry::getValue,
Collectors.toList())));
The output Map:
{John=[Taxi Driver, Driver], Mark=[Professional Killer, Murderer]}
Something like this?
void add(String key, String value, Map<String,List<String>> map) {
List<String> list = map.get(key);
if (list == null) {
list = new ArrayList<>();
map.put(key, list);
}
list.add(value);
}
the data structure Multimap may be the best choice. The implementation in guava is highly recommended.
https://github.com/google/guava/wiki/NewCollectionTypesExplained#multimap
You may want to create Map<String, <ArrayList<String>>. That way you can store multiple values that are associated with one key.
You may consider creating method that provides storing multiple values associated to unique key in specified map:
public static void add(String key, String value, Map<String, List<String>> map) {
// Here you create a refeREnce to instance of the list storing values associated with given key:
List<String> listOfValues = map.get(key);
// If given key was already present in the map...
if(listOfValues != null) {
// Add given value to list of values associated with given key:
listOfValues.add(value);
// If given key wasn't present in the map...
} else {
// Initialize an ArrayList that will store given value in the map:
listOfValues = new ArrayList<>();
// Put new entry into the given map (given key and to-be-filled list of values:
map.put(key, listOfValues);
// Put given value to the list of values associated with given key
listOfValues.add(value);
}
}
Hi I am working with HashMap in java and i have a scenario where i have to compare 2 HashMaps
HashMap1:
Key: BOF Value: SAPF
Key: BOM Value: SAPM
Key: BOL Value: SAPL
HashMap2:
Key: BOF Value: Data1
Key: BOL Value: Data2
And after comparing these two hashmaps my resulting hashmap will contain the Key as a Value of First HashMap1 and Value as a Value of second HashMap2.
HashMap3:
Key: SAPF Value: Data1
Key: SAPL Value: Data2
Just iterate on the keys of HashMap1, and for each key, check if it's present in HashMap2.
If it's present, add the values to HashMap3 :
final Map<String, String> hm1 = new HashMap<String, String>();
hm1.put("BOF", "SAPF");
hm1.put("BOM", "SAPM");
hm1.put("BOL", "SAPL");
final Map<String, String> hm2 = new HashMap<String, String>();
hm2.put("BOF", "Data1");
hm2.put("BOL", "Data2");
final Map<String, String> hm3 = new HashMap<String, String>();
for (final String key : hm1.keySet()) {
if (hm2.containsKey(key)) {
hm3.put(hm1.get(key), hm2.get(key));
}
}
Iterate over the keys of the first map and put the values in your new map, if the second map has a value for the same key.
Map map3 = new HashMap();
for (Object key : map1.keySet()) {
Object value2 = map2.get(key);
if (value2 != null) {
Object value1 = map1.get(key);
map3.put(value1, value2);
}
}
HashMap has an method called entrySet() that returns an object that represents the content of the map as a set of key-value pairs.
public Set<Map.Entry<K,V>> entrySet()
You should iterate through that set using the keys to look up in the second map and then putting the results in the 'result set'.
I assume you have a established that the values in the first set will be unique OR you don't mind that entries might get overwritten in the output.
Notice that the iterator moves through the set in an unspecified order so if there are overwrites this method won't guarantee which values overwrite which other values.
You can use the keySets of both maps to intersect them using:
boolean retainAll(Collection<?> c)
and then iterate using that intersection over the tho maps building your solution.
I have created a map called result.
In the sortByKeys method as my keys are String with Numeric values, I have converted them to Integer key type Map then sorted them.
The sorting is working fine when I am looping and printing individually, but not when I am setting them in another Map.
public class TestDate {
public static void main (String args[]){
Map<String, String> result = new HashMap<String, String>();
result.put("error", "10");
result.put("1","hii");
result.put("Update","herii");
result.put("insert","insert");
result.put("10","hiiuu");
result.put("7","hii");
result.put("21","hii");
result.put("15","hii");
Map<String, String> sorted = sortByKeys(result);
//System.out.println(sorted);
}
private static Map<String, String> sortByKeys(Map<String, String> map) {
Map <Integer,String> unSorted = new HashMap<Integer, String>();
Map <String,String> sorted = new HashMap<String, String>();
for (Map.Entry<String, String> entry : map.entrySet())
{
try{
int foo = Integer.parseInt(entry.getKey());
unSorted.put(foo, entry.getValue());
}catch (Exception e){
}
}
Map<Integer, String> newMap = new TreeMap<Integer, String>(unSorted);
Set set = newMap.entrySet();
Iterator iterator = set.iterator();
while(iterator.hasNext()) {
Map.Entry me = (Map.Entry)iterator.next();
System.out.println(me.getKey());
System.out.println(me.getValue());
sorted.put(me.getKey().toString(), me.getValue().toString());
}
System.out.println(sorted);
return null;
}
}
Here is the o/p :
1
hii
7
hii
10
hiiuu
15
hii
21
hii
{21=hii, 10=hiiuu, 1=hii, 7=hii, 15=hii}
If you don't need the last inch of performance, you can solve this rather directly, without an extra step to sort the map, by using SortedMap:
Map<String,String> result = new TreeMap<>(Comparator.comparingInt(Integer::parseInt));
If you are among the unfortunate bunch who are still being denied access to Java 8, you'll have to implement the Comparator in long-hand:
new TreeMap<>(new Comparator<String,String> { public int compare(String a, String b) {
return Integer.compare(Integer.parseInt(a), Integer.parseInt(b));
}});
The above approach works only under the assumption that all keys are parseable integers. If that is not the case, then you won't be able to use the SortedMap directly, but transform your original map into it, filtering out the unparseable keys.
It's because the Map you're putting them into is a HashMap, which isn't sorted. There's no guarantee of the ordering of results you'll get out of the HashMap, even if you put them in in the right order.
(And calling it sorted doesn't change anything :) )
You print 2 different maps and not the same: you iterate over and print the entries of newMap map, and at the end you print sorted map.
You see the sorted entries printed because you iterate over your sorted newMap.
Then you print the sorted map which is unsorted (despite by its name). You print a different map instance.
Print this:
System.out.println(newMap); // This is the instance of the sorted "TreeMap"