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:
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.
Any help would be appreciated as always!
Dan
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);
}
}
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());
}
Dan
Comment