Java Program to retrieve the set of all values in HashMap


First, create a HashMap and add elements −

HashMap hm = new HashMap();
hm.put("Wallet", new Integer(700));
hm.put("Belt", new Integer(600));
hm.put("Backpack", new Integer(1200));

Now, retrieve all the values −

Collection getValues = hm.values();
System.out.println("
Values..."); Iterator i = getValues.iterator(); while (i.hasNext()) { System.out.println(i.next()); }

The following is an example to get the set of all the values in the HashMap −

Example

 Live Demo

import java.util.*;
public class Demo {
   public static void main(String args[]) {
      // Create hash map
      HashMap hm = new HashMap();
      hm.put("Wallet", new Integer(700));
      hm.put("Belt", new Integer(600));
      hm.put("Backpack", new Integer(1200));
      System.out.println("Map = "+hm);
      Collection getValues = hm.values();
      System.out.println("
Values..."); Iterator i = getValues.iterator(); while (i.hasNext()) { System.out.println(i.next()); } } }

Output

Map = {Backpack=1200, Belt=600, Wallet=700}

Values...
1200
600
700

karthikeya Boyini
karthikeya Boyini

I love programming (: That's all I know

Updated on: 30-Jul-2019

165 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements