How to add List element to map by key? - java

I use the following approach for sorting items by key:
final Map<Integer, UUID> map = new TreeMap<>();
while (rowIterator.hasNext()) {
// Add Items to the TreeMap
mapppp.put(1, UUID.fromString("610e9040-840c-48c5-aa64-f193ed896133"));
mapppp.put(2, UUID.fromString("4ff0055d-49a9-4e93-b960-ff70ae1007ce"));
mapppp.put(3, UUID.fromString("751c79ce-445c-406a-9787-a754857a259c"));
mapppp.put(3, UUID.fromString("dc721d77-509d-4bd3-8004-43a8daab5840"));
mapppp.put(3, UUID.fromString("4ca16aff-5a30-4638-8b38-3a93eddb0fc5"));
mapppp.put(3, UUID.fromString("e4be360b-cccb-42a2-b416-eb4bb623b49f"));
}
However, I cannot add another element for the repeated key value (for example 3) and I think I should implement something like Map<Integer, List<UUID>> map = new TreeMap<>(); rather than Map<Integer, UUID> map = new TreeMap<>();. However, I could not find a proper solution for that. So, how can I manage the repeated key values to be added to the map?

You could indeed use a Map<Integer, List<UUID>> as you suggested. You can then use computeIfAbsent to create an empty map if the key isn't there, and add the value to the key:
final Map<Integer, List<UUID>> map = new TreeMap<>();
addToMap(int key, String value) {
mapppp.computeIfAbsent(key, k -> new ArrayList<>()).add(value);
}

Related

How to map HSCAN response to Map<key, value> using Vert.x Redis

I am using io.quarkus.redis.client.RedisClient which inside use Vert.x for a Redis Client in Java. When I use HSCAN method it return a list of key and values in differents rows like this:
The keys are 0,2,4... and the values are the JSONs. Is there a way to obtain a Map<key,value> instead of a list with a key and values mix in a elegant/clean way?
You can do it with a simple for
Map<String, String> result = new HashMap<>();
for (int i = 0; i < source.length; i+=2) {
result.put(source[i], source[i + 1]);
}
In Kotlin you can use a more elegant solution but I think this one works
You will need to iterate over the Response in order to get the map.
Here is an example for Map<String, String> with for loop:
Map<String, String> map = new HashMap<>();
for (String key : results.getKeys()) {
map.put(key, results.get(key).toString());
}
Here is the same example but using java lambdas:
Map<String, String> map = result.getKeys().stream()
.collect(Collectors.toMap(key -> key, key -> result.get(key).toString()));
For your case with json you can just change the transformation function from .toString() to something that suits your need.
Edit 1:
As HSCAN returns array as defined:
return a two elements multi-bulk reply, where the first element is a
string representing an unsigned 64 bit number (the cursor), and the
second element is a multi-bulk with an array of elements.
There is not a simple solution to create a map but this is what I recommend:
Iterator<Response> iterator = response.get(1).stream().iterator();
Map<String, String> map = new HashMap<>();
while (iterator.hasNext()) {
map.put(iterator.next().toString(), iterator.next().toString());
}

How do I put in the values (of different data types) from two HashMaps with the same keys into a new third HashMap?

I need to make a third HashMap based off the values from the PeopleAndNumbers and PeopleAndGroups hashmaps. But the third HashMap would only have the 3 groups as keys and the total amounts from the people in that group as values.
(Also worth noting that the keys in the first both maps are the same.)
Here are the contents of the first two maps:
PeopleAndNumbers: {p1=1, p2=3, p3=2, p4=3, p5=1, p6=2}
PeopleAndGroups: {p1=GroupA, p2=GroupB, p3=GroupC, p4=GroupB, p5=GroupC, p6=GroupA}
I need to make a third HashMap that'd print out like this:
CombineMap: {GroupA=3, GroupB=6, GroupC=3}
Here is what the code looks like so far:
import java.util.HashMap;
public class HashmapTest {
public static void main(String[] args) {
HashMap<String, Integer> PeopleAndNumbers = new HashMap<String, Integer>();
HashMap<String, String> PeopleAndGroups = new HashMap<String, String>();
PeopleAndNumbers.put("p1", 1);
PeopleAndNumbers.put("p2", 3);
PeopleAndNumbers.put("p3", 2);
PeopleAndNumbers.put("p4", 3);
PeopleAndNumbers.put("p5", 1);
PeopleAndNumbers.put("p6", 2);
PeopleAndGroups.put("p1","GroupA");
PeopleAndGroups.put("p2","GroupB");
PeopleAndGroups.put("p3","GroupC");
PeopleAndGroups.put("p4","GroupB");
PeopleAndGroups.put("p5","GroupC");
PeopleAndGroups.put("p6","GroupA");
System.out.println(PeopleAndNumbers);
System.out.println(PeopleAndGroups);
HashMap<String, Integer> CombineMap = new HashMap<String, Integer>();
//Insert method to do this here, How would I go about this?
System.out.println("Expected Output for CombineMap should be");
System.out.println("{GroupA=3, GroupB=6, GroupC=3}");
System.out.println(CombineMap);
}
}
If I understand you correctly, you want to sum Numbers by Group, using the common keys to join them. If so, you can do it pretty easily with streams:
Map<String, Integer> combined = PeopleAndGroups.entrySet()
.stream()
.collect(Collectors.groupingBy(e -> e.getValue(),
Collectors.summingInt(e -> PeopleAndNumbers.get(e.getKey()))));
Or you can iterate and merge entries into your destination map:
Map<String, Integer> combined = new HashMap<>();
PeopleAndGroups.forEach((k, v) ->
combined.merge(v, PeopleAndNumbers.get(k), Integer::sum));
To achieve that you need to iterate over the entries of the PeopleAndGroups map and do the following for each entry:
check if the combinedMap has a key equal to the value of the current entry
If the key doesn't exist put the key with value 0: combinedMap.put(entry.getValue(), 0)
Get the value of the entry's key from the PeopleAndNumbers and let's call it N: int N = PeopleAndNumbers.get(entry.getKey())
add N to the old value of your result map:
combinedMap.put(entry.getValue(), combinedMap.get(entry.getValue()) + N)

How can i convert Map of Strings and Object to a Map of Strings and List

I have method that should return Map<Strings, List<String>> but in the mean time my method gives me a Map<Strings, Object>, I want to transfer the values of object into a List of Strings.
Here is the current code:
#SuppressWarnings("unchecked")
static Map<String, List<String>> getQueryParameters(JsonObject inputJsonObject) {
JsonArray parameters = inputJsonObject.getJsonArray("parameters");
Optional<JsonObject> queryParameters = parameters.stream().
filter(JsonObject.class::isInstance).
map(JsonObject.class::cast).
filter(jsonObject -> jsonObject.getJsonObject("queryParameters") != null).
map(item -> item.getJsonObject("queryParameters")).findFirst();
Map<String, Object> paramMap = queryParameters.get().getMap();
paramMap contains key and value , values could be an arrays of integers
so I want to put them into the map below:
Map<String, List<String>> mystore = new LinkedHashMap<String, List<String>>();
My solution is this which did not work correctly
Map<String, List<String>> mystore = new LinkedHashMap<String, List<String>>();
Map<String, Object> paramMap = queryParameters.get().getMap();
Iterator it = paramMap.entrySet().iterator();
while (it.hasNext()) {
String key = it.next().toString();
if (!mystore.containsKey(key)) ;
mystore.put(key, new LinkedList<String>());
mystore.get(key).add(it.next().toString());
}
I was a key holding another key as value and is just a mix up , any suggestions?
After debuging what happens i see that mystore holds both "key and value" together as a key and value it hold the next "key and value as value
Should be something like this:
while (it.hasNext()) {
Map.Entry<String, Object> next = iterator.next();
String key = next.getKey();
Object value = next.getValue();
if (!mystore.containsKey(key)) mystore.put(key, new LinkedList<String>());
mystore.get(key).add(value.toString());
}
I'm not writing a program for you, but instead help you in finding a problem. You are confused with Entry. If you are using IDE, you should solve it easier. Look for this line :
String key = it.next().toString();
Entry has a K,V pair. The iterator returns an EntrySet and thus usage to get key is it.next().getKey() and it.next().getValue()
Now that you have a correct key, please go on debugging. Instead of putting and getting and manipulating in below lines of your code. Put with correct value instead?
Yours:
mystore.put(key, new LinkedList<String>());
mystore.get(key).add(it.next().toString());
What about?:
Entry entry = it.next();
//Get key and value here. DO coding using Entry's methods
List<String> ll = new LinkedList<String>();
ll.add(value)
mystore.put(key, ll);
Tip: Always have the Javadoc or reference documentation handy for knowing more. That's how you learn the language. Refer:https://docs.oracle.com/javase/7/docs/api/java/util/Map.Entry.html

How do I reverse a map with a Set<Integer> included?

For instance if I have a map with integer and strings:
Map<Integer, String> myMap = new HashMap<Integer, String>();
This map would contain key values of Integers and values of names.
What I am trying to do is make a new map, that copies all the values (names) from theMap and makes them the keys for the new map. Now the tricky part I can't get, is that I want the values of the new map to be the numbers, but if there are multiple numbers that correspond to the same name I want them to be held in an Set.
Example of new map:
Map<String, Set<Integer>> returnMap = new TreeMap<String, Set<Integer>>();
So if "John" corresponds to 1,2,3,4. I would like the new map to contain a key of "John" with a Set containing 1,2,3,4
Google's Guava library has a nice Multimap class which maps keys to multiple values. If you use it, you can take advantage of a host of helper methods:
SetMultimap<String, Integer> returnMap =
Multimaps.invertFrom(Multimaps.forMap(myMap), TreeMultimap.create());
It's not that tricky :)
Map<Integer, String> map = ... //Your map
Map<String, Set<Integer>> reverseMap = new TreeMap<String, Set<Integer>>();
for(Map.Entry<Integer, String> entry : map.entrySet()) {
Integer key = entry.getKey();
String value = entry.getValue();
Set<Integer> set;
if(reverseMap.containsKey(value)) {
set = reverseMap.get(value);
set.add(key);
} else {
set = new HashSet<Integer>();
set.add(key);
reverseMap.put(value, set);
}
}

Finding Matching Keys and Values in a Map

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

Categories

Resources