17-dars. Java ArrayList, linkedList, HashMap, HashSet, Iterator Java ArrayList


Download 93.02 Kb.
Pdf ko'rish
bet5/7
Sana21.04.2023
Hajmi93.02 Kb.
#1372885
1   2   3   4   5   6   7
Bog'liq
17 dars java arraylist linkedlist hashmap hashset iterator XjkypEGJ

HashMap Size
Qancha element borligini bilish uchun size() usulidan foydalaning:
capitalCities.size();
Loop Through a HashMap()


HashMap elementlarini for-each tsikl operatridan foydalanib ishlatamiz
Eslatma: Agar siz faqat kalitlarni xohlasangiz keySet() usulidan foydalaning va faqat qiymatlarni 
istasangiz values() usulidan foydalaning:
// Print keys
for (String i : capitalCities.keySet()) {
System.out.println(i);
}
// Print values
for (String i : capitalCities.values()) {
System.out.println(i);
}
// Print keys and values
for (String i : capitalCities.keySet()) {
System.out.println("key: " + i + " value: " + capitalCities.get(i));
}
Other Types(Boshqa typelar)
Example
String kalitlari va Integer qiymatlarini saqlaydigan people deb nomlangan HashMap ob'ektini yarating:
public class Main {
public static void main(String[] args) {
// Create a HashMap object called people
HashMap people = new HashMap();
// Add keys and values (Name, Age)
people.put("John", 32);
people.put("Steve", 30);
people.put("Angie", 33);
for (String i : people.keySet()) {
System.out.println("key: " + i + " value: " + people.get(i));


}
}
}
Java HashSet
HashSet - bu har bir elementi yagona bo'lgan va java.util paketida joylashgan elementlar to'plami:
Example
String tipidagi elementlarni saqlaydigan cars deb nomlangan HashSet obyektini yarating:
HashSet cars = new HashSet();
Add Items(Element qo'shish)
HashSet sinfida juda ko'p foydali methodlar mavjud. Masalan, unga elementlar qo'shish uchun add() 
usulidan foydalaning:
public class Main {
public static void main(String[] args) {
HashSet cars = new HashSet();
cars.add("Volvo");
cars.add("BMW");
cars.add("Ford");
cars.add("BMW");
cars.add("Mazda");
System.out.println(cars);
}
}
Eslatma: Yuqoridagi misolda BMW ikki marta qo'shilgan bo'lsa ham, u to'plamda faqat bir marta 
paydo bo'ladi, chunki to'plamdagi har bir element yagona bo'lishi kerak.

Download 93.02 Kb.

Do'stlaringiz bilan baham:
1   2   3   4   5   6   7




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