Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Java Generic Class

Status
Not open for further replies.

brent123

MIS
Mar 21, 2006
29
0
0
US
I have a Generic class that uses a list it gets from a parameter. When I test to see if two items in the list are equal I get false even if they are equal. I have specified the equals method in the class that the list is made up of, but I can't get it to work. The generic class looks like this:

public static <E> List<E> removeRepeats (List<E> list){
System.out.println(list.get(0).equals(list.get(1));
}

I know the item at index 0 and 1 are equal based on the equality I specified in the classes equal method. But the method returns false every time.

What am i missing?

Thanks,
 
Whats class type is "E" (am I missing something) ?

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
<E> just means the class is generic. It can take a list made up of any class type and return a list of the same class type.

Thanks,
 
Well I did not know that :) Thankyou.

If the datatype is of String (ie List<String>), then you can do .equals() - but if it is of some other type (ie List<Integer>) then .equals() will not work as you expect it to (unless you override the .equals() method in your object in the list which performs some correct comparison algorithm.

You don't say which actual class is contained within the List ...

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
Ok. Lets say I'mm calling this generic method with a list of ScreenPoint -- removeRepeats(spList). spList is a list of ScreenPoint. I have already defined my class ScreenPoint and I have overridden the equals method to define equality they way I want. I have also tested the ScreenPoint equals method and it works fine. But when the generic method remove repeats executes the line -- list.get(0).equals(list.get(1))-- it returns false when I know its true.
 
big errors:
a) missing a closing bracket.
b) doesn't return anything
Code:
public static <E> List <E> removeRepeats (List<E> list)
{
	System.out.println (list.get (0).equals (list.get (1)));
}
c) small error:
Your equal-Method isn't defined:
Code:
 public boolean equals (Object o)
but
Code:
 public boolean equals (Foo o)
?

don't visit my homepage:
 
I'd need to test, but Foo is an Object, I think it will compile and run.

Cheers,
Dian
 
works:
Code:
import java.util.*;

/**
        GenericEquals

        @author Stefan Wagner
        @date Do 22 Feb 02:01:08 CET 2007

*/
public class GenericEquals
{
        class Foo
        {
                int bar;
                Foo (int x)
                {
                        bar = x;
                }

                public boolean equals (Object o)
                {
                        return (((Foo) o).bar == bar);
                }
        }

        public GenericEquals ()
        {
                Foo foo = new Foo (0);
                Foo bar = new Foo (1);
                Foo fob = new Foo (1);

                System.out.println (foo.equals (bar));
                System.out.println (fob.equals (bar));

                List <Foo> list = new ArrayList <Foo> ();
                list.add (fob);
                list.add (bar);
                list.add (foo);

                list = removeRepeats (list);
                for (Foo f : list)
                {
                        System.out.println (f.bar);
                }
        }

        public static <E> List <E> removeRepeats (List<E> list)
        {
                boolean hit = list.get (0).equals (list.get (1));
                System.out.println (hit);
                if (hit)
                        list.remove (0);
                return list;
        }

        public static void main (String args[])
        {
                new GenericEquals ();
        }
}

don't visit my homepage:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top