0

I have a custom object array list, the object must be in an array list however i have some duplicates in the list and i want to preform a check before i do an add to the list. How can this be achieved. The victimSocialSecurityNumber is unique. Under is my code:

CODE

while (rs.next()){

       Citizens victims = new Citizens();
       victims.setSocialSecurityNumber(rs.getInt("victimSocialSecurityNumber"));

       victims.setfName(rs.getString("victimFName"));

       victims.setlName(rs.getString("victimLName"));

       victims.setPhoto(rs.getString("victimPhoto"));

       victims.setName(rs.getString("victimFName") +" "+ rs.getString("victimLName"));


       crime.getVictims().add(victims);         
0

4 Answers 4

3

you can convert arraylist to set and back to get rid of the duplicates or use directly structure which allows only sorted unique elements : LinkedHashSet

Sign up to request clarification or add additional context in comments.

2 Comments

will this also set the object attribute list as well i.e crime.victims list?
@Martin, also should override equals and hashcode methods of Citizens.
2

Assuming Citizens overrides equals, you can do it like this

 if (!crime.getVictims().contains(victims)) {
       crime.getVictims().add(victims);  
 }

though generally when duplicates are not allowed the solution is Set

If you have doubts how to override equals / hashCode read http://javarevisited.blogspot.com/2011/10/override-hashcode-in-java-example.html

4 Comments

i was trying this however i wasn't successful. How do you override equals?
Thanks alot this worked however i am still a little bit confused how it did. i did some reading on these methods how they are used and why override them hopefully it will come to me soon. Also why doesn't equals method work like this by default?
default equals (Object.equals) returns true only for the same object i.e. obj1.equals(obj1).
1

You can use a hash set to add the objects and convert it to an Arraylist. This can help you to check whether the victim is unique.

CODE

Set hashset = new HashSet();

while (rs.next()){

   Citizens victims = new Citizens();
   victims.setSocialSecurityNumber(rs.getInt("victimSocialSecurityNumber"));

   victims.setfName(rs.getString("victimFName"));

   victims.setlName(rs.getString("victimLName"));

   victims.setPhoto(rs.getString("victimPhoto"));

   victims.setName(rs.getString("victimFName") +" "+ rs.getString("victimLName"));

   hashset.add(victims);       

}

List list = new ArrayList(hashset);

Comments

0

I could be completely wrong here, but wouldn't a for loop solve your problem? You could just compare what you are about to add to all the elements in the arraylist, and if there are no matches add it, and if there is don't?

2 Comments

Asuming you want to stop the user from creating more then one Citizen with the same state?
Your approach is also fine. But, choice of right data-structures is important. This helps us to understand their use better.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.