• Web sitemizin içeriğine ve tüm hizmetlerimize erişim sağlamak için Web sitemize kayıt olmalı ya da giriş yapmalısınız. Web sitemize üye olmak tamamen ücretsizdir.
  • Sohbetokey.com ile canlı okey oynamaya ne dersin? Hem sohbet et, hem mobil okey oyna!
  • Soru mu? Sorun mu? ''Bir Sorum Var?'' sistemimiz aktiftir. Paylaşın beraber çözüm üretelim.

Nasıl java içinde harita kullanırım gir içeri (HashMap,LinkedHashMap)

Üyelik Tarihi
7 Ocak 2015
Konular
4,091
Mesajlar
4,274
MFC Puanı
40
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:
su kelimeden kac tane var?
su kelime mevcut mu?
su kelimeden su kadar olsun
Ve kelime sayma islemini soyle yapabiliriz:
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
Bu sekilde map'imiz her kelimeden kac tane oldugunu tutmus olacak.




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)
Map sadece bir interface (arayuz) ve yukarida bir Map'in ne yapabilmesi gerektigini anlattik. HashMap da bir Map'tir ve bu islemleri (dogru sekilde kullanildiginda) oldukca hizli bir sekilde gerceklestirir.

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.
Peki dolabi kitap koymak yerine mesela araba parcasi koymak icin kullaniyorsak? Yine ayni yontemi kullaniriz, ama araba parcasinin hash degerini baska bir sekilde hesaplamamiz gerekiyor. Mesela araba parcasinin seri numarasi olabilir.

(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
Bir anahtar degerini arayacakken yine ayni stratejiyi kullanir.


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:
Product.java
package javamapexample;
/**
*
* @author tunamert-tore
*/
public class Product {
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;
}
}
Main.java
package javamapexample;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
/**
*
* @author tunamert-tore
*/
public class Main {
/**
* @param args the command line arguments
*/
public static **** main(String[] args) {
//Lets create first products by using Product class
Product p1 = new Product(1,”car”,10000);
Product p2 = new Product(2,”computer”,1000);
Product p3 = new Product(3,”phone”,400);
Product p4 = new Product(4,”speakers”,100);
Product p5 = new Product(5,”speakers2″,125);
Map<String,Product> productMap = new HashMap<String, Product>();
productMap.put(“car”, p1);
productMap.put(“computer”, p2);
productMap.put(“phone”, p3);
productMap.put(“speakers”, p4);
//what will be happened if a same key is inserted again
//this will replace the old product with a new one
productMap.put(“speakers”, p4);
//HashMap allows programmers to have only one Null key and many null values
productMap.put(null, p5);
//productMap.put(null, null); VALID
//productMap.put(“phone”, null); VALID
System.out.println(“getting the phone element (id for the phone) with key phone –> ” + productMap.get(“phone”).getId() );
// Iteration starts
//ordering of the elements in a HashMap is not sorted
//so that ordering will be unpredictable
//use TreeMap if you want ordering
Iterator it = productMap.keySet().iterator();
while (it.hasNext()) {
Object key = it.next();
System.out.println(“key –> ” + key + ” value –> ” + ” id -> ” + productMap.get(key).getId() + ” name -> ” + productMap.get(key).getName());
}
System.out.println(“\nLinkedHashMap starts\n”);
Map<String,Product> productMapLinked = new LinkedHashMap<String, Product>();
productMapLinked.put(“1″, p2);
productMapLinked.put(“2″, p3);
productMapLinked.put(“3″, p1);
Iterator it2 = productMapLinked.keySet().iterator();
while (it2.hasNext()) {
Object key = it2.next();
System.out.println(“key –> ” + key + ” value –> ” + productMapLinked.get(key).getName());
}
//If you use TreeMap in your application
//You will need to override equals() and hashCode()
}
}
Sample output;
run:
getting the phone element (id for the phone) with key phone –> 3
HashMap starts
key –> null value –> id -> 5 name -> speakers2
key –> computer value –> id -> 2 name -> computer
key –> car value –> id -> 1 name -> car
key –> phone value –> id -> 3 name -> phone
key –> speakers value –> id -> 4 name -> speakers
LinkedHashMap starts
key –> 1 value –> computer
key –> 2 value –> phone
key –> 3 value –> car
BUILD SUCCESSFUL (total time: 1 seconds)
 
Üst