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


Download 93.02 Kb.
Pdf ko'rish
bet6/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

Element borligini tekshirish
HashSet-da element mavjudligini tekshirish uchun contain() usulidan foydalaning:
cars.contains("Mazda");


Elementni o'chirish
Elementni olib tashlash uchun remove() usulidan foydalaning:
cars.remove("Volvo");
Barcha elementlarni olib tashlash uchun clear() usulidan foydalaning:
cars.clear();
HashSet Size
Qancha element borligini bilish uchun size methodidan foydalaning:
cars.size();
HashSet elementlari bo'ylab aylanish
HashSet elementlari bo'ylab aylanish uchun for-each loopdan foydalaning:
for (String i : cars) {
System.out.println(i);
}
Other Types(Boshqa data type lar)
Biz HashSet elementlarini faqatgina String typeda emas balki boshqa barcha non-primitive typlarda 
berishimiz mumkin:
public class Main {
public static void main(String[] args) {
// Create a HashSet object called numbers
HashSet numbers = new HashSet();
// Add values to the set
numbers.add(4);
numbers.add(7);
numbers.add(8);


// Show which numbers between 1 and 10 are in the set
for(int i = 1; i <= 10; i++) {
if(numbers.contains(i)) {
System.out.println(i + " was found in the set.");
} else {
System.out.println(i + " was not found in the set.");
}
}
}
}
Java Iterator
Iterator - bu ArrayList va HashSet kabi to'plamlar bo'ylab aylanish uchun ishlatilishi mumkin bo'lgan 
ob'ekt. U "iterator" deb ataladi, chunki "iteratsiya" tsikl uchun texnik atamadir.
Iteratordan foydalanish uchun uni java.util paketidan import qilishingiz kerak.
Getting an Iterator (Iteratorni olish)
Iterator() methodi har qanday to'plam uchun Iteratorni olish uchun ishlatilishi mumkin:
public class Main {
public static void main(String[] args) {
// Make a collection
ArrayList cars = new ArrayList();
cars.add("Volvo");
cars.add("BMW");
cars.add("Ford");
cars.add("Mazda");
// Get the iterator
Iterator it = cars.iterator();
// Print the first item
System.out.println(it.next());
}
}
To'plamni aylanib chiqish uchun iteratorning hasNext() va next() usullaridan foydalaning:


while(it.hasNext()) {
System.out.println(it.next());
}

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