7 Replies Latest reply: Jan 17, 2007 12:49 PM by 800329 RSS

    Collections list contains() question

    807607
      Hello

      I have a list which contains objects of class C.

      i am using lists' 'contain()' method and overwriting class C's equals method.

      the equals method is like this
      public boolean equals (Object test) {
      C test2 = (C) test;
      if (getColor().equals(test2.getColor())
      return true;
      else
      return false;
      }
      This all works fine. but i want ANOTHER method that works similar but has a different if statement which determines whether to return true or false.

      Can this be done?
        • 1. Re: Collections list contains() question
          807607
          1. Are two C objects really equal if and only if they have the same color, or is this an expedient definition?

          2. It's simple enough to write the code to iterate through the collection, looking for a match, which is how contains is implemented.
          • 2. Re: Collections list contains() question
            807607
            they can be different objects but if the attribute getColor is the same for two objects then the list should only contain one. So you think i should write a method that goes thru the collection?
            • 3. Re: Collections list contains() question
              807607
              So you think i should write
              a method that goes thru the collection?
              Creating a equals() methods that lies about equality can only get you into trouble in the long run.

              You could create a class like ColorView:
              final class ColorView {
                  private final C c;
              
                  public ColorView(C c) {
                      this.c = c;
                  }
              
                  public C getC() {
                      return c;
                  }
              
                  //equal if C's colors are equal
                  public boolean equals(Object object) {
                      if (object instanceof ColorView) {
                          ColorView that = (ColorView) object;
                          return this.getC().getColor().equals(that.getC().getColor());
                      } else
                          return false;
                  }
              
                  public int hashCode() {
                      ...
                  }
              }
              • 4. Re: Collections list contains() question
                807607
                Or use as hashmap, with Color as the key...
                • 5. Re: Collections list contains() question
                  807607
                  they can be different objects but if the attribute
                  getColor is the same for two objects then the list
                  should only contain one. So you think i should write
                  a method that goes thru the collection?
                  No, you should be using a Set instead of List.
                  • 6. Re: Collections list contains() question
                    807607
                    they can be different objects but if the attribute
                    getColor is the same for two objects then the list
                    should only contain one. So you think i should
                    write
                    a method that goes thru the collection?
                    No, you should be using a Set instead of List.
                    Actually, I think the OP should be clearer on the requirents. He wrote that two C objects with equal colors are necessarily equal as C objects, but what he's trying to do has never been properly stated.
                    • 7. Re: Collections list contains() question
                      800329
                      If the collection (set) selection is for a specific case, one could use a HashMap having the selection criterion as key and the object as value. containsKey() would provide a hint, if it's not sufficient to take the last matching entry for the criterion.