🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.jbuilder.java.language
Article #15062

HashTable with complex keys

#15062
From: Stephan Grimm
Date: Thu, 15 Jul 1999 00:00
80 lines
2673 bytes
--------------C85562869C8BE663E3BC952E
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit

How can I use a HashTable with custom Objects as keys ?
In which way can I enforce the HashTable to use my own equals()-method
for the key's comparison ?

For an example:
class Key
{
    public int a;
    public int b;
    public Key(x,y){a=x;b=y;}
    public boolean equals(Key key)
    {
        return (a==key.a && b==key.b);
    }
}
...
hashTable.put(new Key(a0,b0),value);

Also when there exists a key with (a0,b0) this call to put() creates a
new key in the HashTable using the keys' object reference (?) for
comparison.
I want the custom method Key.equals() to be used.
How can I achieve this?


--



-Stephan Grimm-

<Stephan.Grimm@mgm-edv.de>
<grst0018@fh-karlsruhe.de>

GERMANY


--------------C85562869C8BE663E3BC952E
Content-Type: text/html; charset=us-ascii
Content-Transfer-Encoding: 7bit

<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
<html>
How can I use a HashTable with custom Objects as keys ?
<br>In which way can I enforce the HashTable to use my own equals()-method
for the key's comparison ?
<p>For an example:
<br><tt>class Key</tt>
<br><tt>{</tt>
<br><tt>&nbsp;&nbsp;&nbsp; public int a;</tt>
<br><tt>&nbsp;&nbsp;&nbsp; public int b;</tt>
<br><tt>&nbsp;&nbsp;&nbsp; public Key(x,y){a=x;b=y;}</tt>
<br><tt>&nbsp;&nbsp;&nbsp; public boolean equals(Key key)</tt>
<br><tt>&nbsp;&nbsp;&nbsp; {</tt>
<br><tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return (a==key.a &amp;&amp;
b==key.b);</tt>
<br><tt>&nbsp;&nbsp;&nbsp; }</tt>
<br><tt>}</tt>
<br><tt>...</tt>
<br><tt>hashTable.put(new Key(a0,b0),value);</tt>
<p>Also when there exists a key with (a0,b0) this call to put() creates
a new key in the HashTable using the keys' object reference (?) for comparison.
<br>I want the custom method Key.equals() to be used.
<br>How can I achieve this?
<br>&nbsp;
<p>--
<br>&nbsp;
<br>&nbsp;
<p>-Stephan Grimm-
<p>&lt;Stephan.Grimm@mgm-edv.de>
<br>&lt;grst0018@fh-karlsruhe.de>
<p>GERMANY
<br>&nbsp;</html>

--------------C85562869C8BE663E3BC952E--
Technical Details
Message-ID: <378D902A.E19E5D57@mgm-edv.de>
Path: arc1-ca.pugleaf.net!unknown!not-for-mail

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: f74b3,1b5114a3c0f3ff8f,start
X-Google-Attributes: gidf74b3,public
From: Stephan Grimm <Stephan.Grimm@mgm-edv.de>
Subject: HashTable with complex keys
Date: 1999/07/15
Message-ID: <378D902A.E19E5D57@mgm-edv.de>
X-Deja-AN: 501244462
X-Accept-Language: de,en
Content-Type: multipart/alternative; boundary="------------C85562869C8BE663E3BC952E"
Organization: mgm
Mime-Version: 1.0
Newsgroups: borland.public.jbuilder.java.language