Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8396855
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T20:38:31+00:00 2026-06-09T20:38:31+00:00

How do I fetch unique list of object arrays in below code: import java.util.*;

  • 0

How do I fetch unique list of object arrays in below code:

    import java.util.*;

    class t1 {
        public static void main(String[] args) {

            Object[] o1 = new Object[] { null, "abc@aaa.com", "ENG", "775778435", 1};
            Object[] o2 = new Object[] { null, "abc@aaa.com", "ENG", "775778435", 1};

            List<Object[]> result = new ArrayList<Object[]>(); 
            result.add(o1); 
            result.add(o2); 

            // The above result list is coming from some DB & I cannot change the structure of above script. 
            // Now, I need to remove the duplicates from this result list meaning I need to find duplicates from the objects within the list.
            // I tried below code but it still prints duplicates. Any help???

            Set<User> setResult = new HashSet<User>();
            User userInfo = null;

            for (Object[] userData : result) {
                userInfo = new User((String)userData[0], (String)userData[1], (String)userData[2], (String)userData[3], (Integer) userData[4]);
                setResult.add(userInfo);
            }

            Iterator it = setResult.iterator();
            while (it.hasNext()) {
                Object o = it.next();

                User u = (User) o;
                System.out.println("non-duplicate = " + u.getEmail());
            }

            // Expected result: non-duplicate = abc@aaa.com
            // Actual   result: non-duplicate = abc@aaa.com getting printed twice i.e. duplicate not getting removed!
        }
    }

    class User {
        public String firstName;
        public String email;
        public String language;
        public String productCode;
        public int status;

        public User() {         
        }

        public User(String fName, String userId, String lang, String productCode, int status) {
            this.firstName = fName;
            this.email = userId;
            this.language = lang;
            this.productCode = productCode;
            this.status = status;
        }   

        public String getFirstName() {
            return firstName;
        }

        public void setFirstName(String firstName) {
            this.firstName = firstName;
        }

        public String getEmail() {
            return email;
        }

        public void setEmail(String email) {
            this.email = email;
        }

        public String getLanguage() {
            return language;
        }

        public void setLanguage(String language) {
            this.language = language;
        }

        public String getProductCode() {
            return productCode;
        }

        public void setProductCode(String productCode) {
            this.productCode = productCode;
        }

        public int getStatus() {
            return status;
        }

        public void setStatus(int status) {
            this.status = status;
        }

        @Override
        public int hashCode() {
            int fNameHash = 0;
            int lNameHash = 0;
            int emailHash = 0;
            int langHash = 0;
            int productCodeHash = 0;

            if (this.firstName != null) {
                fNameHash = this.firstName.hashCode();
            }

            if (this.email != null) {
                emailHash = this.email.hashCode();
            }

            if (this.language != null) {
                langHash = this.language.hashCode();
            }

            if (this.productCode != null) {
                productCodeHash = this.productCode.hashCode();
            }

            return (fNameHash + lNameHash + emailHash + langHash + productCodeHash + this.status);
        }

        @Override
        public boolean equals(Object obj) {
            if(obj != null && obj instanceof User) {
                User temp = (User) obj;

                if (this.firstName != null && temp.firstName != null && this.firstName.equalsIgnoreCase(temp.firstName) 
                   && this.email != null && temp.email != null && this.email.equalsIgnoreCase(temp.email) 
                   && this.language != null && temp.language != null && this.language.equalsIgnoreCase(temp.language)
                   && this.productCode != null && temp.productCode != null && this.productCode.equalsIgnoreCase(temp.productCode)
                   && this.status == temp.status) {             
                    return true;
                }
            }
            return false;
        }   
    }

My expected result is to print abc@aaa.com only once but its getting printed twice!!

Can any one tell me how to correct this code?

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T20:38:33+00:00Added an answer on June 9, 2026 at 8:38 pm

    The problem is with the firstName value that is null for both objects. Because of this the equals() method return false and both your objects are considered distinct by the Set.

    Try changing your equals and hashcode with the following:

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof User)) return false;
    
        User user = (User) o;
    
        if (status != user.status) return false;
        if (email != null ? !email.equals(user.email) : user.email != null) return false;
        if (firstName != null ? !firstName.equals(user.firstName) : user.firstName != null) return false;
        if (language != null ? !language.equals(user.language) : user.language != null) return false;
        if (productCode != null ? !productCode.equals(user.productCode) : user.productCode != null) return false;
    
        return true;
    }
    
    @Override
    public int hashCode() {
        int result = firstName != null ? firstName.hashCode() : 0;
        result = 31 * result + (email != null ? email.hashCode() : 0);
        result = 31 * result + (language != null ? language.hashCode() : 0);
        result = 31 * result + (productCode != null ? productCode.hashCode() : 0);
        result = 31 * result + status;
        return result;
    }
    

    Best way is to have equals and hashcode generated from your IDE as suggested in another answer.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to fetch recent sold 5 unique items in magento, I have below
When i am using the NSPredicate while fetch the unique user data from Coredata
I want to write a Ruby program that will fetch all the unique http
I have the following table, from which i have to fetch non unique rows
table: uuid, version, datetime version is not unique, but the idea is to fetch
I have a ArrayList of object as below: Object: String country; String languages; String
I have @manytomany relation in hibernate Like : Table Employee public class Employee implements
I have a list of unique numbers in a file. Whenever user of my
i have this bean public class Advertisement{ @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name = pkid,
This is the model: class Rep(db.Model): author = db.UserProperty() replist = db.ListProperty(str) unique =

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.