Share on Google+Share on Google+

Example of values() method of HashMap.

In the tutorial you will learn about the values() method of HashMap class.

Example of values() method of HashMap.

The values() is the method of java HashMap. It returns a collection object of HashMap values. 



import java.util.Collection;

import java.util.HashMap;

import java.util.Iterator;

public class HashMapValues {

public static void main(String[] arr) {

/* Create object of HashMap */

HashMap<Integer, String> obHashMap = new HashMap<Integer, String>();

/* Strore value in HashMap */

obHashMap.put(new Integer(1), "AAA");

obHashMap.put(new Integer(2), "BBB");

obHashMap.put(new Integer(3), "CCC");

obHashMap.put(new Integer(4), "DDD");

obHashMap.put(new Integer(5), "EEE");

/* Create a set of keys of hashmap */

Collection<String> collection = obHashMap.values();

Iterator obIter = collection.iterator();

while (obIter.hasNext()) {

String str = (String);

System.out.println("Values : " + str); } } }

Output :

Values : AAA

Values : BBB

Values : CCC

Values : DDD

Values : EEE


Posted on: April 6, 2011 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.