Why we can't put null key in linkedHashMap in java [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
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.

Related

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

How to convert a HashMap to a TreeMap using a comparator? [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
In my code I have the following HashMap, where the key is a Kennel object and the value is a list of Dog objects in that kennel.
When I have populated my HashMap, can I then convert it to a Treemap and pass in a comparator?
Map<Kennel, List<Dog>> mapOfKennelsAndDogs;
//populate the map
//convert it to a treemap?
Just create the treemap with the comparator, and then add all the entries.
Comparator<Kennel> ordering = ...;
TreeMap<Kennel, List<Dog>> treeMap = new TreeMap<>(ordering);
treeMap.putAll(mapOfKennelsAndDogs);

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