• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1236
  • Last Modified:

remove duplicate entries in ArrayList containg bean objects

I have an ArrayList alBeans

I want to remove all duplicate entries in alBeans.  So if 2 beans contain the same informations in their fields I want to remove one of them.

I've tried this an it doesn't work:
ArrayList noDupes = new ArrayList ( new HashSet ( alBeans ));
0
qbawler311
Asked:
qbawler311
  • 4
  • 3
  • 3
  • +2
4 Solutions
 
CEHJCommented:
You need to implement equals correctly in the bean class
0
 
el_diosCommented:
I think you also need to implement both hashcode and equals methods correctly in your bean if you are using a HashSet. These methods will be used by HashSet to find the duplicates.
0
 
CEHJCommented:
Yes - i forgot the hashCode
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Mayank SAssociate Director - Product EngineeringCommented:
Yes, equals () and hashCode () have a contract ;-) you need to implement both:

http://www-128.ibm.com/developerworks/java/library/j-jtp05273.html
0
 
el_diosCommented:
Not just bcos of the contract, specially bcos a HashSet is being used, maybe some other set implementation could have been used without hashmap implementation(bad coding) but would have worked, not HashSet.
0
 
qbawler311Author Commented:
How many more points to provide code if I give you the bean class?
0
 
Mayank SAssociate Director - Product EngineeringCommented:
>> Not just bcos of the contract

Yes, the contract effectively exists because of how they are linked together.

>> How many more points to provide code if I give you the bean class?

We can't give you full source-code just like that, though we can explain the algo, you can come up with some initial code and we can try to fix it. How big is the bean class? See the example on the link I posted and try to do something similar in your bean class.
0
 
el_diosCommented:
I would prefer guiding you than writing your code. These are very basic implementations and very simple also.
0
 
qbawler311Author Commented:
Ok give me some time...I'll be back
0
 
qbawler311Author Commented:
heres the bean

String str1;
float flt1;
float flt2;
String str2;
String str3;
float flt3;
float flt4;
String str4;

.
.
.
getters/.../setters

0
 
Jim CakalicSenior Developer/ArchitectCommented:
Which properties make the object unique? All of them? You might try the jakarta commons lang EqualsBuilder and HashCodeBuilder classes. Your methods would end up looking something like this:

 public class Person {
    private String name;
    private int age;
    private boolean isSmoker;
    ...
   
    // see http://jakarta.apache.org/commons/lang/api-release/org/apache/commons/lang/builder/EqualsBuilder.html
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj instanceof Person == false) {
            return false;
        }
        Person rhs = (Person) obj;
        return new EqualsBuilder()
                     .appendSuper(super.equals(obj))
                     .append(name, rhs.name)
                     .append(age, rhs.age)
                     .append(smoker, rhs.smoker)
                     .isEquals();
    }
 
    // see http://jakarta.apache.org/commons/lang/api-release/org/apache/commons/lang/builder/HashCodeBuilder.html
    public int hashCode() {
        // you pick a hard-coded, randomly chosen, non-zero, odd number
        // ideally different for each class
        return new HashCodeBuilder(17, 37)
            .append(name).
            .append(age)
            .append(smoker)
            .toHashCode();
    }
}

Obviously you'll have to change the implementations to reference the necessary properties in your class. The equals and hashCode methods should both use the same properties.

You can download commons-lang here: http://jakarta.apache.org/site/downloads/downloads_commons-lang.cgi. Extract the jar from the zip, put it in a convenient place, and add the jar to your CLASSPATH.

Regards,
Jim
0
 
Mayank SAssociate Director - Product EngineeringCommented:
Maybe you can simply try equals () as:

public boolean equals ( Object obj )
{
  return ( str1.equals ( obj.str1 ) && flt1 == obj.flt1 && /* so on */ .... ) ;
}

Try implementing hashCode ()
0
 
qbawler311Author Commented:
Thanks guys, sorry i took so long, usually I can be right on top this.  I was afk because of work for a while.
0
 
CEHJCommented:
:-)
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
  • 3
  • 3
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now