hashset error, pleas help

D

dimitri.ognibene

I'm tryng to build a set of int array...
but it doesn't works.. I don't know how to use the api.. please help..

this must output ok but it doesnt, only "azz"
public class Main {

/** Creates a new instance of Main */


static class pica{
int[] data;
public boolean equals(Object a){
return (a instanceof
pica)&&java.util.Arrays.equals(data,((pica)a).data);
}

}
public Main() {

}

/**
* @param args the command line arguments
*/

public static void main(String[] args) {
int a[]=new int[]{1,2};
int b[]=new int[]{1,2};

pica ap=new pica();
pica bp=new pica();
ap.data=a;
bp.data=b;

java.util.Set<pica> set =new java.util.HashSet<pica>();
set.add(ap);

// if
(set.contains((java.util.List)java.util.Arrays.asList(b)))System.out.println("ok");
if(set.contains(bp)) System.out.println("ok");
else System.out.println("azz");
System.out.println(a.hashCode());
System.out.println(b.hashCode());
System.out.println(java.util.Arrays.asList(a).hashCode());
System.out.println(java.util.Arrays.asList(b).hashCode());
 
Z

zero

(e-mail address removed) wrote in
I'm tryng to build a set of int array...
but it doesn't works.. I don't know how to use the api.. please help..

this must output ok but it doesnt, only "azz"

First of all, please try to follow coding conventions. Specifically:

1. always start class names with a capital (Pica, not pica)
2. give variables meaningful names - names like "ap" and "bp" make your
code harder to read.
3. Main is probably not a very good name for a class
static class pica
{
int[] data;
public boolean equals(Object a)
{
return (a instanceof pica) &&
java.util.Arrays.equals(data,((pica)a).data);
}
}

It is important when implementing equals that you also implement the
hashCode method. These are used in conjunction, and if you implement
one and not the other, your class won't work correctly in the
Collections API.

Specifically, you need to make sure that if a.equals(b) then a.hashCode
() *must* equal b.hashCode(). This is not the case in your pica class,
because the hashCode method is the one inherited from Object.

java.util.Set<pica> set =new java.util.HashSet<pica>();
set.add(ap);

// if
(set.contains((java.util.List)java.util.Arrays.asList(b)))
System.out.println("ok");

linewraps made this unreadable. Is this whole part commented out?
if(set.contains(bp)) System.out.println("ok");

Here, you are checking if bp is in the set. It is not, because you only
added ap. So it's normal that this code doesn't print "ok".

And, even if you do add bp, you still need to implement the hashCode
method (see above). If you do that, it should work fine.
 
D

dimitri.ognibene

thanks.
adding hashcode method the bp instance result contained in the set, as
i've seen also looking in hashmap source
sorry for the bad coding style
it is not my usual way.
Good work
Dimitri
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,152
Members
46,697
Latest member
AugustNabo

Latest Threads

Top