hey Guys,
im creating a clas that is to be used in a HashMap to map phone numbers to name.
for example : map.put( new PhoneNumber(207, 8269600, 260), “Paul”);

public final class PhoneNumber {
private final int areaCode;
private final int number;
private final int extension;

public PhoneNumber(int areaCode, int number, int extension){
this.areaCode = areaCode;
this.number = number;
this.extension = extension;
}

public int getAreaCode(){
return areaCode;
}
public int getNumber(){
return number;
}
public int getExtension(){
return extension;
}
}

what i need is that when i later want to retrieve the corresponding name like :

map.get(new PhoneNumber(207, 8269600, 260));

it should return the String “Paul”.

What i need is a methods that would enable the name to be correctly and efficiently retrieved from the map when a newly constructed PhoneNumber object is used?


please help i need this urgently and i have no experience with hash maps and not enough time to learn them
any help will hep

thank you

I guess you may have to use an overloaded hashcode method for this to work. I'm not quite sure though. May be the more experienced people can help you out.

I have a few questions out of curiosity :

1. Can it not be the other way around ? You give the name, you get the object and then the phone number. Because that seems more ideal to me.
2. Do you keep these objects or do you know the values of all these objects that you are putting into the map ? Cos retrieval according to me will be impossible without knowing value or keeping the object.

to answer your first question , is no, because there can be many same names however the number is unique.
the values could be kept in a database but for know i am just using values that i know of for testing purposes

thanks for you help

I guess you can write your own hashcode method to use your variables to obtain a unique number. This may work.

public int hashCode() {
                String unique = Integer.toString(areaCode)+Integer.toString(number)+Integer.toString(extension);
		return Integer.parseInt(unique);
	}

The requirement is that each object should have a unique hashcode for the value that it has. So think of a way to generate a big enough unique number for your objects to be added in the map. It has to be in a way such that the same unique number is generated when you give the same values again. The above method may be one of the many ways to generate your unique number. Do let me know if this helps your cause.

Edited 5 Years Ago by adarshcu: Forgot the return value

Yup, I think you will have to override the hashCode and equals methods for your phone number class, but that's not too hard. Eg: you could convert the number to a String in a standard format, and then use that String's hashCode
(as per previous post, but return
unique.hashCode();

Edited 5 Years Ago by JamesCherrill: Justy saw previous post

ok now i am more confused than i was before lol,
how does this all fit in the code above ? i am very unfamiliar with the way hashmap works

thanks to everyone for the help

Each object has a hashCode method. When you add an object to a map, it uses the hashcode of the object during retrieval. Overriding hashcode method ensures the way to recognize unique objects.

Hashmap uses the key object's hash (an int as returned by the hashCode() method) to create the map between key and value. The default hash method that you inherit from Object returns unique values for every object, but you want to "equal" phone numbers to have the same hash so they will correspond to the same entry in the hashmap. In your phone number class you just need to override the default hashCode method with one of your own, as shown in the previous 2 posts. You may also need to override the default equals method similarly

thanks everyone for the help, i have managed to work this out

This question has already been answered. Start a new discussion instead.