LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-14-2007, 06:15 PM   #1
enemorales
Member
 
Registered: Jul 2004
Location: Santiago, Chile
Distribution: Ubuntu
Posts: 410

Rep: Reputation: 31
Custom java Map keys


Hi there,

I'm trying to use a map in Java where the keys are a class of my own:

Code:
public class IntPair {
	private final int x;
	private final int y;
		
	public IntPair(final int x, final int y) {
		this.x = x;
		this.y = y;
	}
		
	public IntPair() {
		x = y = 0;
	}
		
	public int getx() { return x; }
		
	public int gety() { return y; }

	public String toString() {
		return String.format("(%d,%d)",x,y);
	}
				
} // class IntPair
Here is an example of use:

Code:
		
Map<IntPair,Integer> h = new HashMap<IntPair,Integer>();

IntPair r1 = new IntPair(10,2);
h.put(r1, 5);
h.put(new IntPair(10,2), 8);
h.put(new IntPair(10,2), 543);

for (IntPair r : h.keySet()) {
	System.out.println(r + ":" + h.get(r));
}
So, you see, I've used three times the IntPair(10,2), hence after executing this I would like the Map to have only one element, which key the IntPair(10,2) and with value 543, but I instead of that I get three different instances, each one pointing to one different value: 5, 8 and 543.

I've tried overriding the equals method and also the hashCode() method in IntPair(), but I have had no success: I still get three elements in the Map instead of one.

Is there a way to solve this? How?

I think java is using the references/addresses for the objects to calculate the actual keys that it uses internally in the Map. The problem is that I wouldn't want to keep the references to the keys in the Map. I would like, for instance, to add new values using h.put(new IntPair(x,y)) and then being able, in another place in the program, whether new IntPair(x,y) is a key in the Map...

Thank you a lot in advance.
 
Old 05-15-2007, 04:43 AM   #2
Seagull
Member
 
Registered: Oct 2003
Location: Holland
Distribution: SuSE 10.0 SuSE 10.2
Posts: 70

Rep: Reputation: 15
What happens if you use :

Code:
h.put(r1, 5);
h.put(r1, 8);
h.put(r1, 543);
Thereby using the same object as the key.
 
Old 05-15-2007, 06:31 AM   #3
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,453

Rep: Reputation: 448Reputation: 448Reputation: 448Reputation: 448Reputation: 448
Hi

I'm not very good in Java, but I guess you need to make an "equals" method in the IntPair class. I think the default "equals" method is the same as "==" so there will only be a match when the references are the same. How else can the HashMap know if two object are "equal"? And I think if you make an equals method, you should make an hashCode method as well. Two equal objects should make the same hashCode, I don't know if they will do that using the default hashCode.
 
Old 05-15-2007, 07:45 AM   #4
theYinYeti
Senior Member
 
Registered: Jul 2004
Location: France
Distribution: Arch Linux
Posts: 1,897

Rep: Reputation: 66
Absolutely. You must override equals and hashcode as explained in Sun's Javadoc. If you tried this already, then sorry, you must have done something wrong.

Yves.
 
Old 05-15-2007, 08:20 AM   #5
enemorales
Member
 
Registered: Jul 2004
Location: Santiago, Chile
Distribution: Ubuntu
Posts: 410

Original Poster
Rep: Reputation: 31
Hi,

Certainly if I use the code proposed by Seagull that works, but I do not want to do that. I want to drop the reference and then be able to create new objects --with the same content-- to retrieve the values in the Map.

theYinYeti: Could you give me some reference to look at (I mean in the javadoc)? I looked at the reference for Map and for that reason I tried overriding the equals and hashCode method. The equals is easy. I simply check that x and y are the same. For the hashCode I tried returning x*1000+y as a hashCode, but it did not work:

Code:
  // ... within class IntPair:
  public boolean equals(IntPair p) {
    return this.x == p.x && this.y == p.y;
  }

  public int hashCode() {
    return x*1000+y;
  }
Thank you again...
 
Old 05-15-2007, 09:28 AM   #6
enemorales
Member
 
Registered: Jul 2004
Location: Santiago, Chile
Distribution: Ubuntu
Posts: 410

Original Poster
Rep: Reputation: 31
Ok, got it. I had to define the equals method as equals(Object o) and not as equals(IntPair p).

Thank you all for the answers.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to map mouse to keys in console curaga Linux - Hardware 0 05-06-2007 05:59 AM
How to map my extra keys to my applications? gregorian Linux - Newbie 2 02-25-2007 08:16 PM
Map Keys Under 3dDesktop michaeljh2000 Slackware 0 01-07-2005 02:38 PM
how to map extra keys on k/b need some help here! jollyjoice Slackware 1 07-24-2004 09:51 AM
Map two keys to same thing Wynd Linux - General 1 05-07-2003 08:47 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:41 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration