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