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

  • Home
  • SEARCH
  • 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 8087931
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T18:56:09+00:00 2026-06-05T18:56:09+00:00

I need your help in JAVA (with some sample code if possible) regarding to

  • 0

I need your help in JAVA (with some sample code if possible) regarding to the following scenario:
I have a list with a classes object and want to check if one object property has duplicates then
keep one of them and add others amounts with the kept one’s amount. For example:

I have this class:

class Salary {
    String names;
    Double amount;
}

and the list say salary_list contains the following elements in it(for example):

[jony,john   300.96]
[fuse,norvi,newby  1000.55]
[john,jony  22.6]
[richard,ravi,navin  55.6]
[fuse,norvi,newby  200.6]
... ... ...

So what is my expected output is the same input list with the following revised result:

[jony,john  323.56]
[fuse,norvi,newby  1201.15]
[richard,ravi,navin  55.6]

N.B: order in names is not important so not the order of the elements after the duplicate elimination.
I am not good at english as well as in Java. So forgive me if any mistakes there.

Thanks in advance.

  • 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-05T18:56:11+00:00Added an answer on June 5, 2026 at 6:56 pm

    Enhance your Salary class as follows:

    class Salary {
        String names;
        Double amount;
        private String sortedNames = null;
    
        @Override
        public boolean equals(Object o)
        {
            if (o == null || ! (o instanceof Salary)) return false;
            Salary othr = (Salary) o;
            String thisNames = this.getSortedNames();
            String othrNames = othr.getSortedNames();
            return thisNames.equals(othrNames);
        }
    
        @Override 
        public int hashCode()
        {
            return getSortedNames().hashCode();
        }
    
        public String getSortedNames()
        {
            if (this.sortedNames == null)
            {
                String[] nameArr = this.names.split(",");
                Arrays.sort(nameArr);
                StringBuilder buf = new StringBuilder();
                for (String n : nameArr)
                    buf.append(",").append(n);
                this.sortedNames = buf.substring(buf.length()==0?0:1);
            }
    
            return this.sortedNames; 
        }
    }
    

    This assumes that Salary is immutable (that is, after it’s created the values of names and amount won’t change. You could then use this with a hash map to add up all the amounts having the same names.

        Map<String,Salary>  map  = new HashMap<String,Salary>();
        for (Salary s : list)
        {
            Salary e = map.get(s.getSortedNames());
            if (e == null)
                map.put(s.getSortedNames(), s);
            else
                e.amount += s.amount;
        }
    

    At this point the map contains all unique Salary objects with the total amount for each.

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

Sidebar

Related Questions

I have recently started learning Java EE 6 and I could need some help.
I need your help to solve a silly problem. I have 2 tables in
I need your help. I have a C executable called generator.out that is a
I need your help with the textarea of my form fields. I have the
I need your help to add some insight into JNI on Android. I've been
I need your help in finding the appropriate code for getting the stored users
I don't have much experience in frameworks or languages, so i need your help.
Please i need some help in adding items to a JComboBox in Java from
I'm new to Java and really need your help. I am presently using a
I need your help. I have one Activity with two fragments: one fragment with

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.