GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — Java-Interview-Programs/HashCodeImpl.java at master · smanaqvi83/Java-Interview-Programs Expand file tree/ Copy pathHashCodeImpl.javaMore file actions 64 lines (54 loc) · 1.59 KB / Copy pathHashCodeImpl.javaFile metadata and controls64 lines (54 loc) · 1.59 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 package com.sky.pgm; import java.util.HashMap; public class HashCodeImpl { public static void main(String[] args) { HashMap<Price, String> hm = new HashMap<Price, String>(); hm.put(new Price("Banana", 20), "Banana"); hm.put(new Price("Apple", 40), "Apple"); hm.put(new Price("Orange", 30), "Orange"); //creating new object to use as key to get value Price key = new Price("Banana", 20); System.out.println("Hashcode of the key: "+key.hashCode()); System.out.println("Value from map: "+hm.get(key)); } } class Price{ private String item; private int price; public String getItem() { return item; } public void setItem(String item) { this.item = item; } public int getPrice() { return price; } public void setPrice(int price) { this.price = price; } public Price(String item, int price) { super(); this.item = item; this.price = price; } public String toString(){ return "item: "+item+" price: "+price; } public int hashCode(){ System.out.println("In hashcode"); int hashcode = 0; hashcode = price*20; hashcode += item.hashCode(); return hashcode; } public boolean equals(Object obj){ System.out.println("In equals"); if (obj instanceof Price) { Price pp = (Price) obj; return (pp.item.equals(this.item) && pp.price == this.price); } else { return false; } } } |
| You are visitor 01402775 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |