97 look yarısı google olsa onun yarısıda guess olsa kaldı 24 kişi 1 kişi benim kaldı 23 kişi 1 i yorum attı 22 oldu bunların ceyregi yorum yazan insanlar olsa nerde bunlar nerdeee 
BUNU İYİ OKUYUN HASHMAP İÇİN GENEL BİLGİ KODLAR ALTTADIR..
Map anahtar => deger baglantilarini saklamak icin kullanilir. Mesela elimizde bir kelime listesi var ve her kelimeden kac tane oldugunu saymak istiyoruz.
Her kelimeden kac tane oldugunu tutmak icin bir Map<String, Integer> kulanabiliriz. Burada anahtar String tipinde, ve deger de Integer tipindedir. Boylece map'imiz ustunde su islemleri gerceklestirebilecegiz:
Yani ozetle map'in yapabilmesi gereken islemler:
HashMap nasil calisir?
Mesela 40 tane kucuk gozu olan bir dolabin var, ve de bir suru kitabin. Fakat aradigin kitabi bulmakta zorluk cekiyorsun. Hashing stratejisini kullanarak dolabini su sekilde duzenleyebilirsin:
(not: yukarida aslinda HashMap yerine HashSet'i (Hash'li kume) anlatmisim. Fakat kumenin elemanlarina bir deger ilistirirsek HashMap elde ederiz. Mesela HashMap<Kitap, Integer> elde etmek icin kitaplarimiz arasina uzerinde kocaman bir sayi yazan bir kagit koyariz
)
HashMap da yaklasik bu sekilde isler. Mesela bir HashMap<Anahtar, Deger> nesnesi olusturdugumuzda hashmap icinde N tane goz ayirir
[*]. HashMap'e bir anahtar nesnesi koyacagimiz zaman hashmap:
Burada onemli olan sey sudur: nesnenin hash degeri nasil hesaplanir?
Object.hashCode() metodu bir nesnenin hash degerini verir. Object'in alt siniflari hashCode metodunu yeniden tanimlayabilirler (ve gerektiginde tanimlamalidirlar da). Eger yeni bir sinif yaziyorsak ve bu siniftan olusturulan nesneleri HashMap veya HashSet ile kullanabilmek istiyorsak suna dikkat etmeliyiz:
BUNU İYİ OKUYUN HASHMAP İÇİN GENEL BİLGİ KODLAR ALTTADIR..
Map anahtar => deger baglantilarini saklamak icin kullanilir. Mesela elimizde bir kelime listesi var ve her kelimeden kac tane oldugunu saymak istiyoruz.
Her kelimeden kac tane oldugunu tutmak icin bir Map<String, Integer> kulanabiliriz. Burada anahtar String tipinde, ve deger de Integer tipindedir. Boylece map'imiz ustunde su islemleri gerceklestirebilecegiz:
- su kelimeden kac tane var?
- su kelime mevcut mu?
- su kelimeden su kadar olsun
- Bir Map<String, Integer> nesnesi olustur.
- Listemizdeki her kelime icin:
- kelime map'in icinde degilse map'a o kelimeden 1 tane oldugunu soyle
- yok icindeyse map'a o kelimeden kac tane oldugunu sor ve sayiyi bir artirip tekrar map'a koy
Yani ozetle map'in yapabilmesi gereken islemler:
- Su anahtar nesnesi icinde mevcut mu? Map.containsKey(anahtar)
- Su anahtar nesnesi ile baglantili nesne nedir? Map.get(anahtar)
- Su anahtar nesnesi ile su nesneyi bagla. Map.put(anahtar, deger)
HashMap nasil calisir?
Mesela 40 tane kucuk gozu olan bir dolabin var, ve de bir suru kitabin. Fakat aradigin kitabi bulmakta zorluk cekiyorsun. Hashing stratejisini kullanarak dolabini su sekilde duzenleyebilirsin:
- Bir kitabin hash degeri = (kitabin isminin ilk harfinin harf sirasi) + 29 * (kitabin isminin ikinci harfinin harf sirasi). Yani "Bu ulke" isimli bir kitabin hash degeri 2 + 29 * 25 = 729
- Bir kitap, sadece dolaptaki (kitabin hash degeri) mod (dolabin goz sayisi) numarali goze konabilir. Yani "Bu ulke" isimli bir kitabi 729 % 40 = 9 numarali goze koyabilirz.
(not: yukarida aslinda HashMap yerine HashSet'i (Hash'li kume) anlatmisim. Fakat kumenin elemanlarina bir deger ilistirirsek HashMap elde ederiz. Mesela HashMap<Kitap, Integer> elde etmek icin kitaplarimiz arasina uzerinde kocaman bir sayi yazan bir kagit koyariz
HashMap da yaklasik bu sekilde isler. Mesela bir HashMap<Anahtar, Deger> nesnesi olusturdugumuzda hashmap icinde N tane goz ayirir
[*]. HashMap'e bir anahtar nesnesi koyacagimiz zaman hashmap:
- anahtar nesnesinin hash degerini bulur
- hash degerinden nesnenin hangi goze koyulacagini hesaplar
- nesneyi o goze koyar
Burada onemli olan sey sudur: nesnenin hash degeri nasil hesaplanir?
Object.hashCode() metodu bir nesnenin hash degerini verir. Object'in alt siniflari hashCode metodunu yeniden tanimlayabilirler (ve gerektiginde tanimlamalidirlar da). Eger yeni bir sinif yaziyorsak ve bu siniftan olusturulan nesneleri HashMap veya HashSet ile kullanabilmek istiyorsak suna dikkat etmeliyiz:
- Ayni olan nesnelerin hash degeri ayni olmali (a.equals(b) true donduruyorsa a ile b aynidir)
- Farkli olan nesnelerin hash degeri farkli olmak zorunda degil. Fakat performans acisindan olabildigince farkli olmasi gerekir (farkli nesneleri dolabin farkli gozlerine dagitmak icin)
Kod:[B]Product.java[/B] package javamapexample; [COLOR=#008000]/**[/COLOR] [COLOR=#008000]*[/COLOR] [COLOR=#008000]* @author [SIZE=5]tunamert-tore[/SIZE][/COLOR] [COLOR=#008000]*/[/COLOR] public class [B]Product[/B] { private int id; private String name; private int price; public Product(int id,String name,int price) { this.id = id; this.name = name; this.price = price; } /** * @return the id */ public int getId() { return id; } /** * @param id the id to set */ public **** setId(int id) { this.id = id; } /** * @return the name */ public String getName() { return name; } /** * @param name the name to set */ public **** setName(String name) { this.name = name; } /** * @return the price */ public int getPrice() { return price; } /** * @param price the price to set */ public **** setPrice(int price) { this.price = price; } } [B]Main.java[/B] package javamapexample; import java.util.HashMap; import java.util.Iterator; import java.util.LinkedHashMap; import java.util.Map; [COLOR=#008000]/**[/COLOR] [COLOR=#008000]*[/COLOR] [COLOR=#008000]* @author [SIZE=5]tunamert-tore[/SIZE][/COLOR] [COLOR=#008000]*/[/COLOR] public class Main { /** * @param args the command line arguments */ public static **** main(String[] args) { [COLOR=#008000]//Lets create first products by using Product class[/COLOR] [COLOR=#0000ff]Product p1 = new Product(1,”car”,10000);[/COLOR] [COLOR=#0000ff]Product p2 = new Product(2,”computer”,1000);[/COLOR] [COLOR=#0000ff]Product p3 = new Product(3,”phone”,400);[/COLOR] [COLOR=#0000ff]Product p4 = new Product(4,”speakers”,100);[/COLOR] [COLOR=#0000ff]Product p5 = new Product(5,”speakers2″,125);[/COLOR] [COLOR=#0000ff]Map<String,Product> productMap = new HashMap<String, Product>();[/COLOR] [COLOR=#0000ff]productMap.put(“car”, p1);[/COLOR] [COLOR=#0000ff]productMap.put(“computer”, p2);[/COLOR] [COLOR=#0000ff]productMap.put(“phone”, p3);[/COLOR] [COLOR=#0000ff]productMap.put(“speakers”, p4);[/COLOR] [COLOR=#008000]//what will be happened if a same key is inserted again[/COLOR] [COLOR=#008000]//this will replace the old product with a new one[/COLOR] productMap.put(“speakers”, p4); [COLOR=#008000]//HashMap allows programmers to have only one Null key and many null values[/COLOR] [COLOR=#000080]productMap.put(null, p5);[/COLOR] [COLOR=#008000]//productMap.put(null, null); VALID[/COLOR] [COLOR=#008000]//productMap.put(“phone”, null); VALID[/COLOR] [COLOR=#ff0000]System.out.println(“getting the phone element (id for the phone) with key phone –> ” + productMap.get(“phone”).getId() );[/COLOR] [COLOR=#008000]// Iteration starts[/COLOR] [COLOR=#008000]//ordering of the elements in a HashMap is not sorted[/COLOR] [COLOR=#008000]//so that ordering will be unpredictable[/COLOR] [COLOR=#008000]//use TreeMap if you want ordering[/COLOR] Iterator it = productMap.keySet().iterator(); while (it.hasNext()) { Object key = it.next(); System.out.println(“key –> ” + key + ” value –> ” + ” id -> ” + productMap.get(key).getId() + ” name -> ” + [COLOR=#ff0000]productMap.get(key).getName())[/COLOR]; } System.out.println(“\nLinkedHashMap starts\n”); [COLOR=#0000ff]Map<String,Product> productMapLinked = new LinkedHashMap<String, Product>();[/COLOR] [COLOR=#0000ff]productMapLinked.put(“1″, p2);[/COLOR] [COLOR=#0000ff]productMapLinked.put(“2″, p3);[/COLOR] [COLOR=#0000ff]productMapLinked.put(“3″, p1);[/COLOR] Iterator it2 = productMapLinked.keySet().iterator(); while (it2.hasNext()) { Object key = it2.next(); System.out.println(“key –> ” + key + ” value –> ” + [COLOR=#ff0000]productMapLinked.get(key).getName()[/COLOR]); } [COLOR=#008000]//If you use TreeMap in your application[/COLOR] [COLOR=#008000]//You will need to override equals() and hashCode() [/COLOR] } } [COLOR=#800000]Sample output;[/COLOR] [COLOR=#0000ff]run:[/COLOR] [COLOR=#0000ff]getting the phone element (id for the phone) with key phone –> 3[/COLOR] [COLOR=#0000ff]HashMap starts[/COLOR] [COLOR=#0000ff]key –> null value –> id -> 5 name -> speakers2[/COLOR] [COLOR=#0000ff]key –> computer value –> id -> 2 name -> computer[/COLOR] [COLOR=#0000ff]key –> car value –> id -> 1 name -> car[/COLOR] [COLOR=#0000ff]key –> phone value –> id -> 3 name -> phone[/COLOR] [COLOR=#0000ff]key –> speakers value –> id -> 4 name -> speakers[/COLOR] [COLOR=#0000ff]LinkedHashMap starts[/COLOR] [COLOR=#0000ff]key –> 1 value –> computer[/COLOR] [COLOR=#0000ff]key –> 2 value –> phone[/COLOR] [COLOR=#0000ff]key –> 3 value –> car[/COLOR] [COLOR=#008000]BUILD SUCCESSFUL (total time: 1 seconds)[/COLOR]
Son düzenleme:


