Java nima Java dasturlash tili


Download 1.25 Mb.
bet46/49
Sana20.01.2023
Hajmi1.25 Mb.
#1103409
1   ...   41   42   43   44   45   46   47   48   49
Bog'liq
2 5420273372074344755

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[]) {


/* This is how to declare HashMap */


HashMap hmap = new HashMap();

/*Adding elements to HashMap*/


hmap.put(12, "Aziz");
hmap.put(2, "Zafar");
hmap.put(7, "Jalol");
hmap.put(49, "Fayzullo");
hmap.put(3, "Jamshid");

/* Display content using Iterator*/


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 orqalis elemetni 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 elementlarini olshining ikki xil usuli bor


  1. Download 1.25 Mb.

    Do'stlaringiz bilan baham:
1   ...   41   42   43   44   45   46   47   48   49




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling