logo

كيفية فرز HashMap في جافا

لا يحتفظ Java HashMap بأي ترتيب افتراضيًا. إذا كانت هناك حاجة لفرز HashMap، فإننا نقوم بفرزها بشكل صريح بناءً على المتطلبات. توفر Java خيارًا لفرز HashMap بناءً على المفاتيح والقيم. في هذا القسم، سوف نتعلم كيفية فرز HashMap وفقًا للمفاتيح والقيم.

  • فرز HashMap حسب المفاتيح
  • فرز HashMap حسب القيم

فرز HashMap حسب المفاتيح

هناك الطرق التالية لفرز HashMap حسب المفاتيح:

  • باستخدام خريطة الشجرة
  • باستخدام LinkedHashMap

عندما نستخدم LinkedHashMap، يجب علينا اتباع العملية:

عندما نستخدم LinkedHashMap، فإننا نحتاج إلى الحصول على مجموعة المفاتيح. قم بتحويل المجموعة إلى قائمة، وقم بفرز القائمة ثم قم بإضافة القائمة التي تم فرزها إلى LinkedHashMap بنفس الترتيب. نفس العملية التي قمنا بها في المثال فرز HashMap حسب القيمة .

مثال على فرز HashMap حسب المفاتيح

في المثال التالي، نستخدم مُنشئ TreeMap لفرز العناصر وتمرير كائن فئة HashMap كوسيطة. هذه هي أبسط طريقة لفرز HashMap حسب المفاتيح.

 import java.util.Map; import java.util.HashMap; import java.util.TreeMap; import java.util.Iterator; public class SortHashMapByKeys { public static void main(String args[]) { //implementation of HashMap HashMap hm=new HashMap(); //addding keys and values to HashMap hm.put(23, 'Yash'); hm.put(17, 'Arun'); hm.put(15, 'Swarit'); hm.put(9, 'Neelesh'); Iterator it = hm.keySet().iterator(); System.out.println('Before Sorting'); while(it.hasNext()) { int key=(int)it.next(); System.out.println('Roll no: '+key+' name: '+hm.get(key)); } System.out.println('
'); Map map=new HashMap(); System.out.println('After Sorting'); //using TreeMap constructor to sort the HashMap TreeMap tm=new TreeMap (hm); Iterator itr=tm.keySet().iterator(); while(itr.hasNext()) { int key=(int)itr.next(); System.out.println('Roll no: '+key+' name: '+hm.get(key)); } } } 

انتاج:

 Before Sorting Roll no: 17 name: Arun Roll no: 23 name: Yash Roll no: 9 name: Neelesh Roll no: 15 name: Swarit After Sorting Roll no: 9 name: Neelesh Roll no: 15 name: Swarit Roll no: 17 name: Arun Roll no: 23 name: Yash 

فرز HashMap حسب القيم باستخدام واجهة المقارنة

في Java، يعد فرز HashMap حسب القيم أمرًا معقدًا لأنه لا توجد طريقة مباشرة متاحة. لفرز HashMap حسب القيم، نحتاج إلى إنشاء ملف المقارنة . يقارن عنصرين على أساس القيم.

بعد ذلك، احصل على مجموعة العناصر من الخريطة وقم بتحويل المجموعة إلى القائمة. استخدم ال مجموعات.فرز(قائمة) طريقة لفرز قائمة العناصر حسب القيم عن طريق تمرير المقارنة المخصصة. الآن قم بإنشاء جديد LinkedHashMap ونسخ العناصر التي تم فرزها في ذلك. منذ LinkedHashMap يضمن ترتيب إدراج التعيينات. نحصل على HashMap بقيم مرتبة.

جافا الرياضيات العشوائية

هناك فرق بسيط بين فرز HashMap حسب المفاتيح والقيم وهو أنه يمكن أن يحتوي على قيم مكررة ولكن ليس مفاتيح مكررة. لا يمكننا استخدام TreeMap لفرز القيم لأن TreeMap يقوم بفرز العناصر حسب المفاتيح.

مثال لفرز HashMap حسب القيم

 import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.Iterator; import java.util.LinkedHashMap; import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.Set; public class SortHashMapValue { public static void main(String[] args) { //implementing HashMap HashMap hm = new HashMap(); hm.put(6, 'Tushar'); hm.put(12, 'Ashu'); hm.put(5, 'Zoya'); hm.put(78, 'Yash'); hm.put(10, 'Praveen'); hm.put(67, 'Boby'); hm.put(1, 'Ritesh'); System.out.println('Before Sorting:'); Set set = hm.entrySet(); Iterator iterator = set.iterator(); while(iterator.hasNext()) { Map.Entry map = (Map.Entry)iterator.next(); System.out.println('Roll no: '+map.getKey()+' Name: '+map.getValue()); } Map map = sortValues(hm); System.out.println('
'); System.out.println('After Sorting:'); Set set2 = map.entrySet(); Iterator iterator2 = set2.iterator(); while(iterator2.hasNext()) { Map.Entry me2 = (Map.Entry)iterator2.next(); System.out.println('Roll no: '+me2.getKey()+' Name: '+me2.getValue()); } } //method to sort values private static HashMap sortValues(HashMap map) { List list = new LinkedList(map.entrySet()); //Custom Comparator Collections.sort(list, new Comparator() { public int compare(Object o1, Object o2) { return ((Comparable) ((Map.Entry) (o1)).getValue()).compareTo(((Map.Entry) (o2)).getValue()); } }); //copying the sorted list in HashMap to preserve the iteration order HashMap sortedHashMap = new LinkedHashMap(); for (Iterator it = list.iterator(); it.hasNext();) { Map.Entry entry = (Map.Entry) it.next(); sortedHashMap.put(entry.getKey(), entry.getValue()); } return sortedHashMap; } } 

انتاج:

 Before Sorting: Roll no: 1 Name: Ritesh Roll no: 67 Name: Boby Roll no: 5 Name: Zoya Roll no: 6 Name: Tushar Roll no: 10 Name: Praveen Roll no: 12 Name: Ashu Roll no: 78 Name: Yash After Sorting: Roll no: 12 Name: Ashu Roll no: 67 Name: Boby Roll no: 10 Name: Praveen Roll no: 1 Name: Ritesh Roll no: 6 Name: Tushar Roll no: 78 Name: Yash Roll no: 5 Name: Zoya