25-dars — Javada Map.
Map ham to’plam bo’lib, boshqalaridan farqi key(kaliti) ham obyekt saqlay olishidadir. Map’da key hech qachon dublikat(takror) bo’lishi mumkin emas. Map interface bo’lib undan
HashMap, TreeMap LinkedHashMap’lar implement oladi.
HashMap klassi
HashMap asosi Map interface bo’lgan to’plam(collection) bo’lib, u o’zida juft qiymatlardan foydalanadi ular key(kalit) va value(qiymat)dir. Bu klass elementlarni saqlanish tartibida saqlamaydi. Huddi HashTable klassiga o’xshab ketadi. U sinxron emas, o’zida key(kalit)ini ham value(qiymat)ini ham null qiymat saqlashi mumkin. HashMap’da faqat bitta null key(kalit) saqlash mumkin.
E’lon qilinishi:
HashMap
HahshMap’ga misol:
import java.util.HashMap; import java.util.Map; import java.util.Iterator; import java.util.Set; public class Details { public static void main(String args[]) { /* Bu misol HashMap'ning qanday e'lon qilishi */ HashMaphmap = new HashMap (); /*HashMap'ga elementlar qo'shish*/ hmap.put(12, "Aziz"); hmap.put(2, "Zafar"); hmap.put(7, "Jalol"); hmap.put(49, "Fayzullo"); hmap.put(3, "Jamshid"); /* Iterator kontentini ko'rsatish*/ Set set = hmap.entrySet(); Iterator iterator = set.iterator(); while(iterator.hasNext()) { Map.Entry mentry = (Map.Entry)iterator.next(); System.out.print("key is: "+ mentry.getKey() + " & Value is: "); System.out.println(mentry.getValue()); } /* key orqali elementni olish String var= hmap.get(2); System.out.println("Value at index 2 is: "+var); /* key orqali elementni o'chirish*/ hmap.remove(3); System.out.println("Map key and values after removal:"); Set set2 = hmap.entrySet(); Iterator iterator2 = set2.iterator(); while(iterator2.hasNext()) { Map.Entry mentry2 = (Map.Entry)iterator2.next(); System.out.print("Key is: "+mentry2.getKey() + " & Value is: "); System.out.println(mentry2.getValue()); } } }
ekranda:
key is: 49 & Value is: Fayzullo key is: 2 & Value is: Zafar key is: 3 & Value is: Jamshid key is: 7 & Value is: Jalol key is: 12 & Value is: Aziz Map key and values after removal: Key is: 49 & Value is: Fayzullo Key is: 2 & Value is: Zafar Key is: 7 & Value is: Jalol Key is: 12 & Value is: Aziz
HashMap’ni elementlarni olishining ikki xil usuli bor
- For loop orqali
- While loop + iterator
misol:
import java.util.HashMap; import java.util.Map; import java.util.Iterator; public class Details { public static void main(String [] args) { HashMaphmap = new HashMap (); // HashMap'ga elementlarni qo'shish hmap.put(11, "AB"); hmap.put(2, "CD"); hmap.put(33, "EF"); hmap.put(9, "GH"); hmap.put(3, "IJ"); // FOR sikli System.out.println("For Loop:"); for (Map.Entry me : hmap.entrySet()) { System.out.println("Key: "+me.getKey() + " & Value: " + me.getValue()); } //WHILE sikli & ITERATOR System.out.println("While Loop:"); Iterator iterator = hmap.entrySet().iterator(); while (iterator.hasNext()) { Map.Entry me2 = (Map.Entry) iterator.next(); System.out.println("Key: "+me2.getKey() + " & Value: " + me2.getValue()); } } }
ekranda:
For sikli: Key: 2 & Value: CD Key: 3 & Value: IJ Key: 33 & Value: EF Key: 9 & Value: GH Key: 11 & Value: AB While sikli: Key: 2 & Value: CD Key: 3 & Value: IJ Key: 33 & Value: EF Key: 9 & Value: GH Key: 11 & Value: AB
HashMap’ni key orqali saralash
Bu misolda HashMap’ning key’ini TreeMap’dan foydalanib saralash usulini ko’ramiz.
import java.util.HashMap; import java.util.Map; import java.util.TreeMap; import java.util.Set; import java.util.Iterator; public class Details { public static void main(String[] args) { HashMaphmap = new HashMap (); hmap.put(5, "A"); hmap.put(11, "C"); hmap.put(4, "Z"); hmap.put(77, "Y"); hmap.put(9, "P"); hmap.put(66, "Q"); hmap.put(0, "R"); System.out.println("Saralashdan oldin:"); Set set = hmap.entrySet(); Iterator iterator = set.iterator(); while(iterator.hasNext()) { Map.Entry me = (Map.Entry)iterator.next(); System.out.print(me.getKey() + ": "); System.out.println(me.getValue()); } Map map = new TreeMap (hmap); System.out.println("Saralashdan keyin:"); Set set2 = map.entrySet(); Iterator iterator2 = set2.iterator(); while(iterator2.hasNext()) { Map.Entry me2 = (Map.Entry)iterator2.next(); System.out.print(me2.getKey() + ": "); System.out.println(me2.getValue()); } } }
ekranda:
Saralashdan oldin: 0: R 4: Z 5: A 66: Q 9: P 77: Y 11: C Saralashdan keyin: 0: R 4: Z 5: A 9: P 11: C 66: Q 77: Y
HashMap ni Comerator dan foydalanib, value(qiymat)ini saralashga misol:
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 HMapSortingByvalues { public static void main(String[] args) { HashMaphmap = new HashMap (); hmap.put(5, "A"); hmap.put(11, "C"); hmap.put(4, "Z"); hmap.put(77, "Y"); hmap.put(9, "P"); hmap.put(66, "Q"); hmap.put(0, "R"); System.out.println("Saralashdan oldin:"); Set set = hmap.entrySet(); Iterator iterator = set.iterator(); while(iterator.hasNext()) { Map.Entry me = (Map.Entry)iterator.next(); System.out.print(me.getKey() + ": "); System.out.println(me.getValue()); } Map map = sortByValues(hmap); System.out.println("Saralashdan keyin:"); Set set2 = map.entrySet(); Iterator iterator2 = set2.iterator(); while(iterator2.hasNext()) { Map.Entry me2 = (Map.Entry)iterator2.next(); System.out.print(me2.getKey() + ": "); System.out.println(me2.getValue()); } } private static HashMap sortByValues(HashMap map) { List list = new LinkedList(map.entrySet()); // Bu yerda custom komparator aniqlandi Collections.sort(list, new Comparator() { public int compare(Object o1, Object o2) { return ((Comparable) ((Map.Entry) (o1)).getValue()) .compareTo(((Map.Entry) (o2)).getValue()); } }); // Bu yerda men Hashmap'da tartiblangan ro'yhatni ko'chiryapman // LinkedHashMap'dan foydalanib, kiritish tartibini saqlash uchun 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; } }
ekranda:
Saralashdan oldin: 0: R 4: Z 5: A 66: Q 9: P 77: Y 11: C Saralashdan keyin: 5: A 11: C 9: P 66: Q 0: R 77: Y 4: Z
TreeMap
TreeMap ham huddi TreeSetga o’xshab ma’lumotlarni saralaydi, farqi TreeMap
key bo’yicha saralaydi.
TreeMap ham huddi HashMap’ga o’xshab Map’dan implement oladi. Farqi HashMap ma’lumotlarni saralab chiqarmaydi.
Key bo’yicha saralashga misol:
import java.util.TreeMap; import java.util.Set; import java.util.Iterator; import java.util.Map; public class Details { public static void main(String args[]) { /* Bu yerda TreeMap'ni qanday e'lon qilish */ TreeMaptmap = new TreeMap (); /*Adding elements to TreeMap*/ tmap.put(1, "Data1"); tmap.put(23, "Data2"); tmap.put(70, "Data3"); tmap.put(4, "Data4"); tmap.put(2, "Data5"); /* Iterator'dan foydalangan holda kontentni ko'rsatish */ Set set = tmap.entrySet(); Iterator iterator = set.iterator(); while(iterator.hasNext()) { Map.Entry mentry = (Map.Entry)iterator.next(); System.out.print("key is: "+ mentry.getKey() + " & Value is: "); System.out.println(mentry.getValue()); } } }
ekranda:
key is: 1 & Value is: Data1 key is: 2 & Value is: Data5 key is: 4 & Value is: Data4 key is: 23 & Value is: Data2 key is: 70 & Value is: Data3
ko’rib turganingizdek ma’lumotlar key bo’yicha saralanib chiqarildi.
TreeMap’da ham elementlarni olish, qo’shish jarayonlari huddi HashMapga o’xshash bo’ladi.
Manba:
Umumiy Dasturlash
25-dars — Javada Map.