public class Main {
public static void main(String[] args) {
// Create a HashMap
object called capitalCities
HashMap
capitalCities = new HashMap();
// Add keys and values (Country, City)
capitalCities.put("England", "London");
capitalCities.put("Germany", "Berlin");
capitalCities.put("Norway", "Oslo");
capitalCities.put("USA", "Washington DC");
System.out.println(capitalCities);
}
}
Access an Item(Elementga murojaat qilish)
HashMap-dagi qiymatga kirish uchun get() usulidan foydalaning va uning kalitiga murojaat qiling:
capitalCities.get("England");
Remove an Item(Elementni o'chirish)
Elementni olib tashlash uchun remove() usulidan foydalaning va kalitga murojaat qiling:
capitalCities.remove("England");
Barcha elementlarni olib tashlash uchun clear() methodidan foydalaning:
capitalCities.clear();
Do'stlaringiz bilan baham: