HashMapを反復処理するJavaプログラム
この記事では、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;
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,があります。 -
各文字の出現回数をカウントするJavaプログラム
以下が私たちの文字列だとしましょう- String myStr = "thisisit"; 発生をカウントするために、HashMapを使用しています。ループしてcontainsKey(0およびcharAt()メソッドを使用し、上記の文字列内の各文字の出現をカウントします- HashMap <Character, Integer> hashMap = new HashMap<>(); for (int i = myStr.length() - 1; i >= 0; i--) { if (hashMap.contains
-
回文をチェックするJavaプログラム
回文数は、逆にしたときに同じままの数です。たとえば、121、313、525などです。 例 回文をチェックする例を見てみましょう- public class Palindrome { public static void main(String[] args) { int a = 525, revVal = 0, remainder, val; val = a; System.out.println("Number to be che