Increment object member in an Array List

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dlite922
    Recognized Expert Top Contributor
    • Dec 2007
    • 1586

    Increment object member in an Array List

    I have a list of objects, the object has a String and an Int in it. I need to check to see if an item is in the list, if so increment the integer and save it back to the list.

    I believe I've done everything OK up to the point of checking to see if an object is in the list by overloading the equals() and hashCode() functions.

    I don't have any errors and compiles except my list contains two entries of the same name (String) instead of having an entry and the counter (Int) being 2.

    When printed:
    testingTitle 1
    testingTitle 1

    It should be:
    testingTitle 2

    Here's the relevant code:
    Code:
                while(titleItr.hasNext()) {
                    rank = 1;
                    String title = (String) titleItr.next();
                    TitleEntity te = new TitleEntity(title, rank);
    
                    if(resultList.contains(te)) {
                        int idx = resultList.indexOf(te);
                        te = resultList.get(idx);
                        te.setRank((te.getRank()+1));
                        resultList.set(idx, te);
                    } else {
                        resultList.add(te);
                    }                
                }
    te is the object with the string (called title) and integer (called rank) members
    resultList is the array list of them.

    I"m assuming contains(te) gives me the correct entity since I've overloaded equals(). correct?

    EDIT: just found out my program never goes into that if, but always the else. what do I need for contains() to work? Here's the overloaded equals(), compareTo() and hashcode() in TitleEntity() class.

    Code:
        public int compareTo(TitleEntity te) {
            return this.rank - te.rank;
        }
    
        public boolean equals(TitleEntity te) {
            return (this.title.equals(te.title));
        }
    
        public int hashCode() {
            return (title.hashCode());
        }
    Any help would be appreciated as always!




    Dan
  • dlite922
    Recognized Expert Top Contributor
    • Dec 2007
    • 1586

    #2
    SOLVED:

    equals cannot be overloaded (same name different param) but can be overided(same name and same parameters). So I changed equals() to this:

    Code:
        public boolean equals(Object o) {
            if(!(o instanceof TitleEntity)) {
                return false;
            }
            TitleEntity te = (TitleEntity)o;
            return this.title.equals(te.title);
        }
    Now contains finds it and a few other minor changes, I get the desired result.

    Cheers,



    Dan

    Comment

    • r035198x
      MVP
      • Sep 2006
      • 13225

      #3
      Actually equals can be overloaded as you found out in the code. You may just get unexpected results (like you got), that's all.
      Also read overriding equals and hashCode and compare it with what you have done.

      Comment

      • dlite922
        Recognized Expert Top Contributor
        • Dec 2007
        • 1586

        #4
        Originally posted by r035198x
        Actually equals can be overloaded as you found out in the code. You may just get unexpected results (like you got), that's all.
        Also read overriding equals and hashCode and compare it with what you have done.
        At first glance, it looks the same, except the check to see if the object is in-fact itself first. This would never happen in my code, but I added it anyway :)

        Thanks!



        Dan

        Comment

        Working...