Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to compare if object exists in arraylist and add if doesn't

 
Haani Naz
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,

i'm working on a library lending system..

the logic i'm using is:

1. get name of person you would like to lend the book to
2. if arraylist is empty, add person/set name
3. loop through peroson arraylist
3.1 compare person name
3.1.1 if person name matches then lend book
3.1.2 if person name doesn't match add person to arraylist
[go back to step 3]




Here's my incomplete code:



The problem with my code despite being able to add the first person and second person successfully is when i add the third person what happens is, it goes through each item in the arrayList and subsequently adds the new person 2 more times.

so for instance:
check if person name in the arrayList is the same
if not add person to the arrayList

[when the arrayList has 2 people, it will add the third person 3 times]





here's the output so that what i'm talking about makes sense:





i've made the assumption that names are unique.

can somebody pleas advise/help me in getting this to work? i'm new to java and i can't seem to think of a solution to implement. how can i do search of the entire arrayList by the person name and add the new person if it doesn't exist..?

this is me trying to learn java as a hobby and its doing my head in lol.


THANKS!


 
Anniruddh Rana
Greenhorn
Posts: 18
Chrome Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Haani,

The issue with your code is that



The above code snippet occurs inside the for loop

So effectively, each time you find a person with a different name you add another person with the entered name.

eg: You entered Harry 1st .. array list had size =0, after entering Harry it has size=1
Now you entered Sue .. array list size was 1, got incremented to 2.

Now next time around you enter Sue again, this is how the for loop will work

size of AL = 2, so the loop variable "i" will have the values 0 and 1

element at index 0, name = Harry ... does not match Sue ... go to else block, insert Sue at index 2
iterate again

element at index 1, name = Sue ... found Sue, book assigned.

So even though you had Sue in the ArrayList earlier you ended up adding her again, the arraylist would look something like this {Harry, Sue, Sue}

instead you can use a boolean flag:



Apart from your use case. the arrayList infact the Collection interface has a contains method, which checks if an object is contained inside the collection or not.

The implementation of an ArrayList's contains method uses the equals() method from the Object class to evaluate if the object is in the list or not.

This is what you could do

In class Person... override the equals and hashCode method.


Next as soon as you get the name input from keyboard. do the following.



As you may have found out I havent tested this code inside any IDE so there might be some syntactical errors in the code, kindly ignore

Hope this is helpful
Regards,
Anniruddh
 
Darryl Burke
Bartender
Posts: 5149
11
Java Netbeans IDE Opera
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What's wrong with using a java.util.Set implementation? You get that functionality built-in.
 
Winston Gutkowski
Bartender
Pie
Posts: 10571
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Haani Naz wrote:i'm working on a library lending system..
the logic i'm using is:

1. get name of person you would like to lend the book to
2. if arraylist is empty, add person/set name
3. loop through person arraylist
3.1 compare person name
3.1.1 if person name matches then lend book
3.1.2 if person name doesn't match add person to arraylist
[go back to step 3]

Seems reasonable, and I think Annirudh has probably covered your error.
However, the underlying issue is that an ArrayList is not really a great collection for this type of logic; a HashMap would be much better, viz:if you really must use a List, you could create a method of your own:however it may be quite slow if your library has lots of members.

Winston

 
Anniruddh Rana
Greenhorn
Posts: 18
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
Seems reasonable, and I think Annirudh has probably covered your error.
However, the underlying issue is that an ArrayList is not really a great collection for this type of logic; a HashMap would be much better, viz:

Winston



I believe you meant a HashSet as mentioned by Darryl (HashMap.put takes a key value pair as arguments), moreover HashSet is backed up by a HashMap

He/She would still have to override the equals and hashCode methods as mentioned above, else the HashSet will not be able to determine that two Person objects having the same name are actually equal(as their hashcodes would be different and hence would get stored into different buckets in a hash backed data structure)
 
Winston Gutkowski
Bartender
Pie
Posts: 10571
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anniruddh Rana wrote:I believe you meant a HashSet as mentioned by Darryl (HashMap.put takes a key value pair as arguments)...

Absolutely right. Which means it would be members.add(), not members.put().

Winston
 
Haani Naz
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guys! will test it out and report back when i get a chance.
 
Haani Naz
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, so i took the advise and am trying to break the problem into smaller chunks.

All i'm trying to do now is check if a person exists or not.

1. if person exists, do nothing
2. if person doesn't exist, add person


here's what i've got so far:





helper.checkIfPersonExists(p1, ml) makes a call to:




i've also tried changing the if staments above to:



i can't seem to figure out how to NOT add the person if they exist. it doesn't seem to work :s

ml is the MyLibrary object and the getPeople() returns the arrayList of people.
 
Haani Naz
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok just read Anniruddh Rana's post again.

think its slowly gelling together now, i realize that i need to override the equals method.

will give that a go!
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Haani Naz wrote:ok just read Anniruddh Rana's post again.

think its slowly gelling together now, i realize that i need to override the equals method.

will give that a go!


And if you override equals(), you should always override hashCode() too. Even if you don't necessarily need it for this particular use case, the two generally go together, and it's good practice to override them together.

http://java.sun.com/developer/Books/effectivejava/Chapter3.pdf
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic