How to retrieve key, value pair in one shot? [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 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();
}

Related

can we use ArrayList as value(datatype) in key-value pair of HashMap? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
can we use ArrayList as value(datatype) in key-value pair of HashMap?.
will there be any issues, if it is possible?
provide an alternative.
Little test with jshell (comes with OpenJDK 13):
var map = new HashMap<String, List<String>>();
map.put("first", new ArrayList<String>());
map.get("first").add("one");
Map<Integer, List<Integer>> m_Map1 = new
HashMap<Integer, List<Integer>>();
// Add values
m_Map1.put(1, new ArrayList<Integer>(list1));
m_Map1.put(2, new ArrayList<Integer>(list2));

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.

Create a new Map using two different maps [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 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);
}

Search without iterator for Map [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 8 years ago.
Improve this question
Is it possible to search without iterator without using map.
Map<String,Short> map = new HashMap<String,Short>();
map.put("String2", (short)2);
map.put("String1", (short)1);
map.put("String3", (short)4);
I am looking for a way to get the value based on the key (Return 2 for value String2). Is Map the right one to use in this scenario.
Thanks !
Returning values based on a key is precisely what maps are for.
To retrieve your value you can simply do:
short returnVal = map.get("String2");

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