এই নিবন্ধে, আমরা মান অনুযায়ী একটি মানচিত্র বাছাই কিভাবে বুঝতে হবে. জাভা হ্যাশম্যাপ হল একটি হ্যাশ টেবিল ভিত্তিক যা জাভার ম্যাপ ইন্টারফেসের বাস্তবায়ন। এটি কী-মান জোড়ার একটি সংগ্রহ।
নীচে একই -
এর একটি প্রদর্শন রয়েছে৷ধরুন আমাদের ইনপুট হল −
Input HashMap: Key = Java, Value = 45 Key = Scala, Value = 20 Key = Mysql, Value = 11 Key = Python, Value = 75
কাঙ্খিত আউটপুট হবে −
The HashMap after sorting is: Key = Mysql, Value = 11 Key = Scala, Value = 20 Key = Java, Value = 45 Key = Python, Value = 75
অ্যালগরিদম
Step 1 - START Step 2 - Declare namely Step 3 - Define the values. Step 4 - Use the ‘sort’ method to sort the elements of the map. Step 5 - Use the ‘getValue’ method to fetch the values and use the ‘compareTo’ method to comare two values. Step 6 - Iterate through the hashmap, and use the ‘getKey’ method to fetch the values into ‘temp’. Use ‘temp’ as the return value. Step 7 - Display the result Step 8 - Stop
উদাহরণ 1
এখানে, আমরা 'প্রধান' ফাংশনের অধীনে সমস্ত ক্রিয়াকলাপ একসাথে আবদ্ধ করি।
import java.util.*;
import java.lang.*;
public class Demo {
public static HashMap<String, Integer> sort(HashMap<String, Integer> input_map){
List<Map.Entry<String, Integer> > list =
new LinkedList<Map.Entry<String, Integer> >(input_map.entrySet());
Collections.sort(list, new Comparator<Map.Entry<String, Integer> >() {
public int compare(Map.Entry<String, Integer> o1,
Map.Entry<String, Integer> o2){
return (o1.getValue()).compareTo(o2.getValue());
}
});
HashMap<String, Integer> temp = new LinkedHashMap<String, Integer>();
for (Map.Entry<String, Integer> aa : list) {
temp.put(aa.getKey(), aa.getValue());
}
return temp;
}
public static void main(String[] args){
HashMap<String, Integer> input_map = new HashMap<String, Integer>();
input_map.put("Java", 45);
input_map.put("Scala", 20);
input_map.put("Mysql", 11);
input_map.put("Python", 75);
System.out.println("The HashMap is defined as:");
for (Map.Entry<String, Integer> element : input_map.entrySet()) {
System.out.println("Key = " + element.getKey() + ", Value = " + element.getValue());
}
Map<String, Integer> result_map = sort(input_map);
System.out.println("\nThe HashMap after sorting is:");
for (Map.Entry<String, Integer> element : result_map.entrySet()) {
System.out.println("Key = " + element.getKey() + ", Value = " + element.getValue());
}
}
} আউটপুট
The HashMap is defined as: Key = Java, Value = 45 Key = Scala, Value = 20 Key = Mysql, Value = 11 Key = Python, Value = 75 The HashMap after sorting is: Key = Mysql, Value = 11 Key = Scala, Value = 20 Key = Java, Value = 45 Key = Python, Value = 75
উদাহরণ 2
এখানে, আমরা ক্রিয়াকলাপগুলিকে অবজেক্ট ওরিয়েন্টেড প্রোগ্রামিং প্রদর্শনকারী ফাংশনে অন্তর্ভুক্ত করি৷
import java.util.*;
import java.lang.*;
public class Demo {
public static void main(String[] args){
HashMap<String, Integer> input_map = new HashMap<String, Integer>();
input_map.put("Java", 45);
input_map.put("Scala", 20);
input_map.put("Mysql", 11);
input_map.put("Python", 75);
System.out.println("The HashMap is defined as:");
for (Map.Entry<String, Integer> element : input_map.entrySet()) {
System.out.println("Key = " + element.getKey() + ", Value = " + element.getValue());
}
List<Map.Entry<String, Integer> > list =
new LinkedList<Map.Entry<String, Integer> >(input_map.entrySet());
Collections.sort(list, new Comparator<Map.Entry<String, Integer> >() {
public int compare(Map.Entry<String, Integer> o1,
Map.Entry<String, Integer> o2){
return (o1.getValue()).compareTo(o2.getValue());
}
});
HashMap<String, Integer> result_map = new LinkedHashMap<String, Integer>();
for (Map.Entry<String, Integer> aa : list) {
result_map.put(aa.getKey(), aa.getValue());
}
System.out.println("\nThe HashMap after sorting is:");
for (Map.Entry<String, Integer> element : result_map.entrySet()) {
System.out.println("Key = " + element.getKey() + ", Value = " + element.getValue());
}
}
} আউটপুট
The HashMap is defined as: Key = Java, Value = 45 Key = Scala, Value = 20 Key = Mysql, Value = 11 Key = Python, Value = 75 The HashMap after sorting is: Key = Mysql, Value = 11 Key = Scala, Value = 20 Key = Java, Value = 45 Key = Python, Value = 75