Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / January 2006

Tip: Looking for answers? Try searching our database.

hashset error, pleas help

Thread view: 
dimitri.ognibene@gmail.com - 30 Jan 2006 18:36 GMT
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());
zero - 30 Jan 2006 19:38 GMT
> 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
> {
[quoted text clipped - 5 lines]
>    }
> }

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);

<snip>

> //        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.
dimitri.ognibene@gmail.com - 30 Jan 2006 20:12 GMT
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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.