How to Get Values from Map in Java Example

Get Values from Map in Java

In this example, We will show simple program about, How to get values from map in Java. The example has been tested and output shared in the same post.

Example Program

package com.dineshkrish;

import java.util.Collection;
import java.util.Map;
import java.util.TreeMap;

 * @author Dinesh Krishnan

public class GetValues {

	public static void main(String[] args) {
		// defining map object
		Map<Integer, String> map = new TreeMap<Integer, String>();
		// adding elements to map
		map.put(1, "One");
		map.put(2, "Two");
		map.put(3, "Three");
		map.put(4, "Four");
		map.put(5, "Five");
		map.put(6, "Six");
		map.put(7, "Seven");
		map.put(8, "Eight");
		map.put(9, "Nine");
		map.put(10, "Ten");
		// getting only values form map
		Collection<String> list = map.values();
		// printing the list of values


[One, Two, Three, Four, Five, Six, Seven, Eight, Nine, Ten]


1. Java Map Interface
2. Java TreeMap Class
3. Java Collection Interface
4. Java Map values() method