Oluşturulan hashmap'te daha önce kullanılmış olan anahtarı kontrol etmek için kullanılan metoddur.
2 dönüş değeri vardır;
package KodSozluk; import java.util.HashMap; public class KodSozluk{ public static void main(String[] args) { HashMap<Integer, String> okulOgrenci = new HashMap<Integer,String>(); okulOgrenci.put(146, "mehmet"); // hashMap veri ekliyoruz. okulOgrenci.put(43, "sedef"); okulOgrenci.put(72, "ayşe"); System.out.println(okulOgrenci.toString()); System.out.println("72 anahtarı kullanılmışmı = "+okulOgrenci.containsKey(72)); System.out.println("12 anahtarı kullanılmışmı = "+okulOgrenci.containsKey(12)); if(!okulOgrenci.containsKey(12)){ // 12. anahtar boş ise yeni ögrenci ekliyorum. okulOgrenci.put(12, "serhat"); } System.out.println(okulOgrenci.toString()); } }
{146=mehmet, 72=ayşe, 43=sedef} 72 anahtarı kullanılmışmı = true 12 anahtarı kullanılmışmı = false {146=mehmet, 72=ayşe, 43=sedef, 12=serhat}