Connecting Tech Pros Worldwide Help | Site Map

Increment object member in an Array List

dlite922's Avatar
Expert
 
Join Date: Dec 2007
Location: Moon, Dark Side
Posts: 1,094
#1: May 20 '09
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:
Expand|Select|Wrap|Line Numbers
  1.  
  2.             while(titleItr.hasNext()) {
  3.                 rank = 1;
  4.                 String title = (String) titleItr.next();
  5.                 TitleEntity te = new TitleEntity(title, rank);
  6.  
  7.                 if(resultList.contains(te)) {
  8.                     int idx = resultList.indexOf(te);
  9.                     te = resultList.get(idx);
  10.                     te.setRank((te.getRank()+1));
  11.                     resultList.set(idx, te);
  12.                 } else {
  13.                     resultList.add(te);
  14.                 }                
  15.             }
  16.  
  17.  
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.

Expand|Select|Wrap|Line Numbers
  1.  
  2.     public int compareTo(TitleEntity te) {
  3.         return this.rank - te.rank;
  4.     }
  5.  
  6.     public boolean equals(TitleEntity te) {
  7.         return (this.title.equals(te.title));
  8.     }
  9.  
  10.     public int hashCode() {
  11.         return (title.hashCode());
  12.     }
  13.  
  14.  
Any help would be appreciated as always!




Dan
dlite922's Avatar
Expert
 
Join Date: Dec 2007
Location: Moon, Dark Side
Posts: 1,094
#2: May 20 '09

re: Increment object member in an Array List


SOLVED:

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

Expand|Select|Wrap|Line Numbers
  1.     public boolean equals(Object o) {
  2.         if(!(o instanceof TitleEntity)) {
  3.             return false;
  4.         }
  5.         TitleEntity te = (TitleEntity)o;
  6.         return this.title.equals(te.title);
  7.     }
  8.  
Now contains finds it and a few other minor changes, I get the desired result.

Cheers,



Dan
Lives Here
 
Join Date: Sep 2006
Posts: 12,070
#3: May 21 '09

re: Increment object member in an Array List


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.
dlite922's Avatar
Expert
 
Join Date: Dec 2007
Location: Moon, Dark Side
Posts: 1,094
#4: May 21 '09

re: Increment object member in an Array List


Quote:

Originally Posted by r035198x View Post

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
Reply