Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: ArrayList.contains method does not work on user-defined data types

  1. #1
    Junior Member
    Join Date
    Sep 2014
    Location
    Mumbai
    Posts
    10
    My Mood
    Cheerful
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default ArrayList.contains method does not work on user-defined data types

    I am trying to remove the duplicate elements from ArrayList using .contains() if elements are primitive datatype it works but user-defined datatype does not work.

    public class UserBean {
        String name;
        String address;
     
        public String getName() {
            return name;
        }
     
        public void setName(String name) {
            this.name = name;
        }
     
        public String getAddress() {
            return address;
        }
     
        public void setAddress(String address) {
            this.address = address;
        }
     
    }
     
     
     
     
    import java.util.ArrayList;
    import java.util.List;
     
    public class Main {
        public static void main(String args[]){
            UserBean bean=new UserBean();
            List<UserBean> userInfo=new ArrayList();
            bean.setName("rob");
            bean.setAddress("mumbai");
            userInfo.add(bean);
            bean=new UserBean();
            bean.setName("bob");
            bean.setAddress("pune");
            userInfo.add(bean);
            bean=new UserBean();
            bean.setName("rob");
            bean.setAddress("mumbai");
            userInfo.add(bean);
            bean=new UserBean();
            bean.setName("bob");
            bean.setAddress("pune");
            userInfo.add(bean);
            List<UserBean> finalList=new ArrayList();
            finalList.add(userInfo.get(0));
            for(UserBean temp:userInfo){
            if(!finalList.contains(temp)){
                finalList.add(temp);
            }
        }
            for(UserBean temp:finalList){
                System.out.println(temp.getName()+" "+temp.getAddress());
            }
        }
    }


    I get following output:
    rob mumbai
    bob pune
    rob mumbai
    bob pune


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    6,285
    My Mood
    Mellow
    Thanks
    252
    Thanked 777 Times in 762 Posts

    Default Re: ArrayList.contains method does not work on user-defined data types

    From the ArrayList API, you can see that the contains() method uses the equals() method to determine if one object is the same as another. As is often the case for user-defined classes, the default equals() method (inherited from ?) is insufficient to accurately determine equality, so you should override equals(), writing your own code to determine equality.

  3. #3
    Junior Member
    Join Date
    Sep 2014
    Location
    Mumbai
    Posts
    10
    My Mood
    Cheerful
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: ArrayList.contains method does not work on user-defined data types

    I will also have to override hashcode method along with equals but the bean is in the jar if there is any other way I will be very thankful as editing the jar is my last resort.

  4. #4
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    894
    Thanks
    0
    Thanked 166 Times in 148 Posts

    Default Re: ArrayList.contains method does not work on user-defined data types

    You could wrap it up in a class adapter or object adapter.
    Read up on these, perhaps on wikipedia:
    Adapter pattern - Wikipedia, the free encyclopedia

  5. The Following User Says Thank You to Cornix For This Useful Post:

    semaphore (September 1st, 2014)

  6. #5
    Junior Member
    Join Date
    Sep 2014
    Location
    Mumbai
    Posts
    10
    My Mood
    Cheerful
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: ArrayList.contains method does not work on user-defined data types

    Thank you very much it worked I just extended the bean class and override the equals and hashCode methods.

Similar Threads

  1. LinkedList with user defined types
    By muhammadabrar78 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 22nd, 2013, 10:15 PM
  2. User Defined Criteria
    By SimonTemplar in forum Java Theory & Questions
    Replies: 0
    Last Post: February 20th, 2013, 07:02 PM
  3. [SOLVED] Printing an ArrayList of user-defined Objects
    By er1111 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 2nd, 2012, 11:06 AM
  4. User-Defined Methods
    By ZippyShannon in forum What's Wrong With My Code?
    Replies: 4
    Last Post: July 28th, 2011, 11:23 PM