java

How to get values and keys from HashMap in Java

In this tutorial, we are going to see how to get values and keys from HashMap in Java. In Java, we can get the keys and values via the map.entrySet() method.
 

Example:
import java.util.*;

public class Main {

    public static void main(String[] args) {

        Map<String, String> lang = new HashMap<>();
        lang.put("1", "Java");
        lang.put("2", "PHP");
        lang.put("3", "Python");

        // Get values and keys
        for (Map.Entry<String, String> entry : lang.entrySet()) {
            String key = entry.getKey();
            String value = entry.getValue();
            System.out.println("Key: " + key + ", Value: " + value);
        }
    }
}

Output:

Key: 1, Value: Java
Key: 2, Value: PHP
Key: 3, Value: Python
mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

Your email address will not be published. Required fields are marked *