Results 1 to 5 of 5
  1. #1
    santhoskumara is offline Member
    Join Date
    Aug 2007
    Posts
    5
    Rep Power
    0

    Default How to Remove Duplicate object from collection

    I have a bean class-"CollectionBean" implements Comparable and i had a Service Class "CookBookCollection" which creates instances of bean class and add to the list. The list contains duplicate values at position 1 and last.
    I had done sorting using comparator. But I don't how to remove duplicates from List by overriding Equals() and hashcode method.

    Kindly any one advice to me to remove duplicates from list using equals and hashcode method.

    ###### Bean Class#######

    package in.CookBook.collection;

    public class CollectionBean implements Comparable<CollectionBean> {

    private String id;
    private String name;

    public CollectionBean() {

    }

    public CollectionBean(String id, String name) {
    this.id = id;
    this.name = name;
    }

    public int compareTo(CollectionBean bean) {
    // TODO Auto-generated method stub
    if (!(bean instanceof CollectionBean)) {
    throw new ClassCastException(
    "Bean object is not a instance of CollecionBean");
    }
    return bean.getName().compareToIgnoreCase(this.name);
    }

    public boolean equals(CollectionBean bean) {
    System.out.println("idd--->" + bean.getId());
    return(this.getId().equals(bean.getId())
    && this.getName().equalsIgnoreCase(bean.getName())) ;

    }

    public int hashCode(CollectionBean bean) {

    int temp = (31) * 1
    + (bean.getName() == null ? 0 : bean.getName().hashCode())
    + (bean.getId() == null ? 0 : bean.getId().hashCode());
    return temp;
    }

    public String getId() {
    return id;
    }

    public void setId(String id) {
    this.id = id;
    }

    public String getName() {
    return name;
    }

    public void setName(String name) {
    this.name = name;
    }

    }


    ##########End of Bean Class#############



    ### Start of Service class#################

    package in.CookBook.collection;

    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.Comparator;
    import java.util.HashSet;
    import java.util.LinkedHashSet;
    import java.util.List;
    import java.util.Set;
    import java.util.SortedSet;

    public class CookBookCollection {

    public static void main(String args[]) {
    List<CollectionBean> result = new ArrayList<CollectionBean>();
    CollectionBean cb1 = new CollectionBean("1", "abc");
    CollectionBean cb2 = new CollectionBean("2", "dsd");
    CollectionBean cb3 = new CollectionBean("3", "gffg");
    CollectionBean cb4 = new CollectionBean("4", "sfsd");
    CollectionBean cb5 = new CollectionBean("5", "3343");
    ##duplicate values#
    CollectionBean cb6 = new CollectionBean("1", "abc");

    if (!result.contains(cb1)) {
    result.add(cb1);
    }
    if (!result.contains(cb2)) {
    result.add(cb2);
    }
    if (!result.contains(cb3)) {
    result.add(cb3);
    }
    if (!result.contains(cb4)) {
    result.add(cb4);
    }
    if (!result.contains(cb5)) {
    result.add(cb5);
    }
    if (!result.contains(cb6)) {
    result.add(cb6);
    }

    System.out.println("List Value before sorting");

    for (CollectionBean cb : result) {
    System.out.println("idd-->" + cb.getId() + "'&& name---->"
    + cb.getName());
    }

    Collections.sort(result);

    System.out.println("List Value after sorting && Remove duplicates");
    for (CollectionBean cb : result) {
    System.out.println("idd-->" + cb.getId() + "'&& name---->"
    + cb.getName());
    }

    }

    }

    ###########End of Service Class##############

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,544
    Rep Power
    23

    Default

    Where in the code are the duplicate objects to be removed? What method is supposed to to that?

    Please use code tags when posting code to preserve formatting.

    Add @Override before ALL the methods you have overridden.
    Last edited by Norm; 07-22-2010 at 10:27 PM.

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,439
    Rep Power
    18

    Default

    I realise this is probably a cheat, but you could simply make this some form of Set.
    A Set doesn't allow duplicates.
    :)

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,544
    Rep Power
    23

    Default

    if (!result.contains(cb6)) {
    result.add(cb6);
    }
    This should keep dups away.
    And will if the code is corrected.
    The problems are shown by adding the @Overrides

  5. #5
    Ramanjaneyulu is offline Member
    Join Date
    Aug 2010
    Posts
    11
    Rep Power
    0

    Default

    Hi santhoskumara,

    I observed that there is a fault in your CollectionBean class which is avoiding the list to find duplicates. Actually You overloaded the equals() and hashcode() methods,which are needs to override to avoid duplicate entries.

    As per Java contract if two objects are said equal then the hashcode returned by the both object should be same.

    I would recommend you to replace your equals() and hashCode() methods code with the following code then your application will execute.

    public boolean equals(Object bean) {
    if((bean instanceof CollectionBean)&&
    (this.getId().equals(((CollectionBean)bean).getId( ))&
    this.getName().equals(((CollectionBean)bean).getNa me())))
    return true;
    else
    return false;
    }

    public int hashCode() {

    int temp = (31) * 1
    + (this.getName() == null ? 0 : this.getName().hashCode())
    + (this.getId() == null ? 0 : this.getId().hashCode());
    return temp;
    }

Similar Threads

  1. Cannot insert duplicate key row in object
    By losintikfos in forum New To Java
    Replies: 3
    Last Post: 05-07-2009, 09:43 AM
  2. Replies: 4
    Last Post: 03-28-2009, 04:58 PM
  3. How setting an Object to null help Garbage Collection?
    By piyu.sha in forum Advanced Java
    Replies: 3
    Last Post: 10-06-2008, 07:35 AM
  4. Remove duplicate lines from a text file
    By Dirt.Diver in forum New To Java
    Replies: 15
    Last Post: 06-25-2008, 02:08 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •