Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the values from the Hashmap without using Iterator?

Iterator iterator = map.entrySet().iterator();
while (iterator.hasNext()) 
{
    Map.Entry mapEntry = (Map.Entry) iterator.next();
System.out.println("The key is: " + mapEntry.getKey() + ",value is :" + mapEntry.getValue());

}

This is my code. Now i don't want to use Iterator to get the values. Please help me to find best solution.

like image 959
B Narasimha Avatar asked May 30 '26 19:05

B Narasimha


1 Answers

Map<String, Object> map = .....;//Initialization here
for (String key : map.keySet()) {
    // write your code here
}

//If you are just using keys of the Map

for (Object value : map.values()) {
     // write your code here
}

//If you are just using values from your Map

for (Map.Entry<String, Object> entry : map.entrySet()) {
    String key = entry.getKey();
    Object value = entry.getValue();
    // you code here
}

//If you want both Keys and values

//All are without using Iterator of the Map

like image 129
Rishi Avatar answered Jun 02 '26 08:06

Rishi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!