Java 程式迭代一個 HashMap


在本文中,我們將瞭解如何迭代一個 HashMap。Java HashMap 是基於雜湊表的 Java Map 介面的實現。它是一個鍵值對集合。

以下是它的演示示例 −

假設我們的輸入是

Input Hashmap: {Java=Enterprise, JavaScript=Frontend, Mysql=Backend, Python=ML/AI}

期望的輸出是

The keys of the Hashmap are: Java, JavaScript, Mysql, Python,
The Values of the Hashmap are: Enterprise, Frontend, Backend, ML/AI,

演算法

Step 1 - START
Step 2 - Declare namely
Step 3 - Define the values.
Step 4 - Create a hashmap of strings and initialize elements in it using the ‘put’ method.
Step 5 - Display the hashmap on the console.
Step 6 - Iterate over the elements of the hashmap, and fetch each key using ‘keySet’ method.
Step 7 - Display this on the console.
Step 6 - Stop

示例 1

在此,我們在“main”函式下將所有操作繫結在一起。

import java.util.HashMap;
import java.util.Map.Entry;
public class Demo {
   public static void main(String[] args) {
      System.out.println("The required packages have been imported");
      HashMap<String, String> input_map = new HashMap<>();
      input_map.put("Java", "Enterprise");
      input_map.put("Python", "ML/AI");
      input_map.put("JavaScript", "Frontend");
      input_map.put("Mysql", "Backend");
      System.out.println("The HashMap is defined as: " + input_map);
      System.out.print("\nThe keys of the Hashmap are: ");
      for(String key: input_map.keySet()) {
         System.out.print(key);
         System.out.print(", ");
      }
      System.out.print("\nThe Values of the Hashmap are: ");
      for(String value: input_map.values()) {
         System.out.print(value);
         System.out.print(", ");
      }
   }
}

輸出

The required packages have been imported
The HashMap is defined as: {Java=Enterprise, JavaScript=Frontend, Mysql=Backend, Python=ML/AI}

The keys of the Hashmap are: Java, JavaScript, Mysql, Python,
The Values of the Hashmap are: Enterprise, Frontend, Backend, ML/AI,

示例 2

在此,我們將操作封裝到函式中,展示面向物件程式設計。

import java.util.HashMap;
public class Demo {
   static void print_keys(HashMap<String, String> input_map){
      System.out.print("\nThe keys of the Hashmap are: ");
      for(String key: input_map.keySet()) {
         System.out.print(key);
         System.out.print(", ");
      }
   }
   static void print_values( HashMap<String, String> input_map){
      System.out.print("\nThe Values of the Hashmap are: ");
      for(String value: input_map.values()) {
         System.out.print(value);
         System.out.print(", ");
      }
   }
   public static void main(String[] args) {
      System.out.println("The required packages have been imported");
      HashMap<String, String> input_map = new HashMap<>();
      input_map.put("Java", "Enterprise");
      input_map.put("Python", "ML/AI");
      input_map.put("JavaScript", "Frontend");
      input_map.put("Mysql", "Backend");
      System.out.println("The HashMap is defined as: " + input_map);
      print_keys(input_map);
      print_values(input_map);
   }
}

輸出

The required packages have been imported
The HashMap is defined as: {Java=Enterprise, JavaScript=Frontend, Mysql=Backend, Python=ML/AI}

The keys of the Hashmap are: Java, JavaScript, Mysql, Python,
The Values of the Hashmap are: Enterprise, Frontend, Backend, ML/AI,

更新於: 2022-3-30

365 次瀏覽

開啟您的 職業生涯

完成課程並獲得認證

開始
廣告