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
- 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.
Expand|Select|Wrap|Line Numbers
- 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