এই আর্টিকেলে আমরা বুঝব কীভাবে একটি 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 - ভেরিয়েবল ডিক্লেয়ার Step 3 - মান ডিফাইন Step 4 - স্ট্রিং এর HashMap তৈরি করুন এবং 'put' মেথড দিয়ে এলিমেন্ট ইনিশিয়ালাইজ Step 5 - কনসোলে HashMap প্রদর্শন Step 6 - HashMap-এর এলিমেন্টগুলো ইটারেট করুন এবং 'keySet' মেথড দিয়ে প্রতিটি কী বের করুন Step 7 - কনসোলে প্রদর্শন Step 8 - STOP
উদাহরণ ১
এখানে সকল অপারেশন '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 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,
উদাহরণ ২
এখানে অপারেশনগুলো ফাংশনে এনক্যাপসুলেট করা হয়েছে অবজেক্ট-ওরিয়েন্টেড প্রোগ্রামিং প্রদর্শন করতে।
import java.util.HashMap;
class Demo {
static void print_keys(HashMap 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 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 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,