Create a new Map using two different maps [closed] - java

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
How do I create a new Map using the values from two existing maps.
First Map
Map <ID, name>
Second Map
Map <ID, email>
IDs are the same object.
How do I iterate over these two maps, to get name and email to create a new Map like
below ? Assuming The String values are unique.
Map <name, email>

Assuming that name is unique then you could do something like this.
Ive havent compiled this but it should give you the rough idea.
Map<Object, Object> res = new Hashmap();
Set<Object> keys= firstMap.keySet();
for (Object key : keys) {
res.put(firstMap.get(key), secondmap.get(key);
}

Related

Target Variable based on String in Java [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I need to allow the user to target a variable using a string name. Example,
int number = 5;
String variableTarget = "number";
I need to target the int, number, and then change its value in later code. Any way?
I cannot think of any way to do it. There seems to be alot of class targeting, but I already have that.
I don't think that there is a way to do this in java. But, to have such a functionality, you can use a map to assign the value to the number key in the Map :
HashMap <String, Integer> myMap = new HashMap<>();
myMap.put("number", 5);
and you can alter the value later using the same put statment :
myMap.put("number", myMap.get("number")+1);
You can use Java Reflections, or create another structure like a HashMap answered by #DodgyCodeException

How to retrieve key, value pair in one shot? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
Is there any way to get key, value pair in one shot ?
please suggest any logic for
if you want to iterate over a Map.Entry of a Map you can directly refer to the entry
Map<String, Object> map = ...
for (Map.Entry entry: map.entrySet()){
String key = entry.getKey();
Object value = entry.getValue();
}

Why we can't put null key in linkedHashMap in java [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
We can put single null key in hashMap. but in linkedHashMap we can't pull null key. what is the reason behind this.
You can put a null key in a LinkedHashMap. Example:
Map<String, String> m = new LinkedHashMap<> ();
m.put(null, "a");
System.out.println(m.size());
System.out.println(m.get(null));
prints 1 and a.
Following documentation
This class provides all of the optional Map operations, and permits
null elements.

Map<String, Integer> foo - how do I get the value of the integer - JAVA [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I have a method:
public void updateBoard (Map<String, Integer> foo)
How do I find out the value of the Integer? I'm trying foo.get() but it just gives me the key.
Thanks!
Here is how map works
Map <String,Integer> myMap = new HashMap<String,Integer>();
myMap.put("manikant",123);
// Many more put..
System.out.println(myMap.get("manikant"));
// In case you are using java 8. you can also try this.
myMap.forEach( (k,v) -> System.out.println("Key: " + k + ": Value: " + v));
output
123
in your case you can use foo.get(/*enter your key*/);
For more Information see how map works in java
You get all Integer values of the Map with:
foo.values();
Because a Map is a key->value construct.
With
foo.keySet();
you get all keys.
And with
foo.get("key1");
you get the appropriate value for the key key1.
If you are trying to get all the values in the map, use foo.values().
If you are trying to get the value to a specific key in the map, use foo.get(<key>).
Hope it helps!

Looping through the keys of a hashMap [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have a HashMap called AfricanPeople
private HashMap<Integer, Object> AfricanPeople = new HashMap<Integer, Object>();
the key is the age and the value is a person object.
I want to loop through the hashmaps keys and get all people that are between the age of 30 and 45.
Is this possible?
It is possible, using
for (Integer key : map.keySet())
But a HashMap is not the appropriate structure for this. You should use a TreeMap instead, which can directly return a submap containing the keys between 30 and 45:

Categories