Let's suppose i have a HashMap with some entries:
Map hm= new HashMap();
hm.put(1,"ss");
hm.put(2,"ss");
hm.put(3,"bb");
hm.put(4,"cc");
hm.put(5,"ss");
i want output like:
[{1,ss},{2,ss},{5,ss}]
Is it possible?
Let's suppose i have a HashMap with some entries:
Map hm= new HashMap();
hm.put(1,"ss");
hm.put(2,"ss");
hm.put(3,"bb");
hm.put(4,"cc");
hm.put(5,"ss");
i want output like:
[{1,ss},{2,ss},{5,ss}]
Is it possible?
Tomer Li-ran
On
Firstly, you declare your HashMap like this:
HashMap<Integer, String> hm = new HashMap<>();
Then after putting the key and the values you can print the whole HashMap it like this:
System.out.println("Mappings of HashMap hm1 are : " + hm);
If you want to print the value where the key is equal to 1 then:
if (hm.containsKey(1)) {
String s = hm.get(1);
System.out.println("value for key 1 is: " + s);
}
Copyright © 2021 Jogjafile Inc.
Of course it is:
You should change the definition of your
Mapto:P.S. You didn't specify whether you want all the entries in the output
List, or just some of them. In the sample output you only included entries having "ss" value. This can be achieved by adding a filter:Output:
EDIT: You can print that
Listin the desired format as follows:Output: