Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Java Arrays
  • Java Strings
  • Java OOPs
  • Java Collection
  • Java 8 Tutorial
  • Java Multithreading
  • Java Exception Handling
  • Java Programs
  • Java Project
  • Java Collections Interview
  • Java Interview Questions
  • Java MCQs
  • Spring
  • Spring MVC
  • Spring Boot
  • Hibernate
Open In App
Next Article:
Java HashMap put() Method
Next article icon

HashMap values() Method in Java

Last Updated : 26 Nov, 2018
Comments
Improve
Suggest changes
Like Article
Like
Report

The java.util.HashMap.values() method of HashMap class in Java is used to create a collection out of the values of the map. It basically returns a Collection view of the values in the HashMap.

Syntax:

Hash_Map.values()

Parameters: The method does not accept any parameters.

Return Value: The method is used to return a collection view containing all the values of the map.

Below programs are used to illustrate the working of java.util.HashMap.values() Method:
Program 1: Mapping String Values to Integer Keys.




// Java code to illustrate the values() method
import java.util.*;
  
public class Hash_Map_Demo {
    public static void main(String[] args)
    {
  
        // Creating an empty HashMap
        HashMap<Integer, String> hash_map = new HashMap<Integer, String>();
  
        // Mapping string values to int keys
        hash_map.put(10, "Geeks");
        hash_map.put(15, "4");
        hash_map.put(20, "Geeks");
        hash_map.put(25, "Welcomes");
        hash_map.put(30, "You");
  
        // Displaying the HashMap
        System.out.println("Initial Mappings are: " + hash_map);
  
        // Using values() to get the set view of values
        System.out.println("The collection is: " + hash_map.values());
    }
}
 
 
Output:
  Initial Mappings are: {20=Geeks, 25=Welcomes, 10=Geeks, 30=You, 15=4}  The collection is: [Geeks, Welcomes, Geeks, You, 4]  

Program 2: Mapping Integer Values to String Keys.




// Java code to illustrate the values() method
import java.util.*;
  
public class Hash_Map_Demo {
    public static void main(String[] args)
    {
  
        // Creating an empty HashMap
        HashMap<String, Integer> hash_map = new HashMap<String, Integer>();
  
        // Mapping int values to string keys
        hash_map.put("Geeks", 10);
        hash_map.put("4", 15);
        hash_map.put("Geeks", 20);
        hash_map.put("Welcomes", 25);
        hash_map.put("You", 30);
  
        // Displaying the HashMap
        System.out.println("Initial Mappings are: " + hash_map);
  
        // Using values() to get the set view of values
        System.out.println("The collection is: " + hash_map.values());
    }
}
 
 
Output:
  Initial Mappings are: {4=15, Geeks=20, You=30, Welcomes=25}  The collection is: [15, 20, 30, 25]  

Note: The same operation can be performed with any type of Mappings with variation and combination of different data types.



Next Article
Java HashMap put() Method
author
chinmoy lenka
Improve
Article Tags :
  • Java
  • Java - util package
  • Java-Collections
  • Java-Functions
  • Java-HashMap
Practice Tags :
  • Java
  • Java-Collections

Similar Reads

  • IdentityHashMap values() Method in Java
    The java.util.IdentityHashMap.values() method of IdentityHashMap class in Java is used to create a collection out of the values of the map. It basically returns a Collection view of the values in the Map. Syntax: Identity_Hash_Map.values() Parameters: The method does not accept any parameters. Retur
    2 min read
  • HashMap size() Method in Java
    The size() method of the Java HashMap class is used to retrieve the number of key-value pairs currently stored in the HashMap. Example 1: This example demonstrates the use of the size() method to get the number of elements in the HashMap. [GFGTABS] Java // Java program to demonstrates the working of
    2 min read
  • EnumMap values() Method in Java
    The Java.util.EnumMap.values() method in Java is used to create a collection out of the values of the map. It basically returns a Collection view of the values in the EnumMap. Syntax: EnumMap.values() Parameters: The method does not take any argument. Return Values: The method returns the collection
    2 min read
  • Java HashMap put() Method
    The put() method of the Java HashMap class is used to add or update the key-value pairs in the map. If the key already exists in the map, the previous value associated with the key is replaced by the new value and If the key does not exist, the new key-value pair is added to the map. Syntax of HashM
    2 min read
  • Java HashMap putAll() Method
    The putAll() method of the Java HashMap class is used to copy all key-value mappings from another map to the existing map. If a key exists in both maps, its value is updated with the value from the source map. Otherwise, new key-value pairs are added. Example 1: This example demonstrates copying all
    2 min read
  • Java HashMap remove() Method
    The remove() method of the Java HashMap class is used to remove a key-value pair from the map based on the given key. If the key is found, the mapping associated with the key is returned, otherwise, it returns null. Example 1: This example demonstrates how to use remove(Object key) to remove a key-v
    3 min read
  • Java HashMap replace() Method
    The replace() method of the HashMap class in Java is used to replace the value associated with a specific key if the key is already present in the map. Note: If the key does not exist, the method does nothing and the map remains unchanged. Example 1: This example demonstrates replacing the value of
    3 min read
  • HashMap clear() Method in Java
    The clear() method of the HashMap class in Java is used to remove all of the elements or mappings (key-value pairs) from a specified HashMap. Example 2: Here, we will use the clear() method to clear a HashMap of Integer keys and String values. [GFGTABS] Java // Clearing HashMap of Integer keys // an
    2 min read
  • Hashtable put() Method in Java
    The java.util.Hashtable.put() method of Hashtable is used to insert a mapping into a table. This means we can insert a specific key and the value it is mapping to into a particular table. If an existing key is passed then the previous value gets replaced by the new value. If a new pair is passed, th
    3 min read
  • IdentityHashMap put() Method in Java
    The java.util.IdentityHashMap.put() method of IdentityHashMap is used to insert a mapping into a map. This means we can insert a specific key and the value it is mapping, into a particular map. If an existing key is passed then the previous value gets replaced by the new value. If a new pair is pass
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences