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 7668857
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T15:27:49+00:00 2026-05-31T15:27:49+00:00

If I have a bean such as public class SimpleBean { private String name;

  • 0

If I have a bean such as

public class SimpleBean
{
    private String name;
    private int int1;
    private int int2;
    private double double1;
    private double double2;

    // getters/setters snipped
}

what is the best way to merge two instances of the bean into one, where we only want to add the numeric values from the second bean instance to the first (i.e. we don’t care about the String ‘name’ above)

I have come up with the following solution using PropertyUtils, but am thinking it is probably not the best

public void mergeStats(Object stat)
{
    try
    {
        Map<String, Object> beanMap = PropertyUtils.describe(stat);

        // Remove ones we don't want to merge
        beanMap.remove("name");
        beanMap.remove("class");

        for (Map.Entry<String, Object> entry:beanMap.entrySet())
        {
            String key = entry.getKey();
            Number value = (Number) entry.getValue();

            Number existingValue = (Number) PropertyUtils.getProperty(this, key);
            BeanUtils.setProperty(this, key, existingValue.doubleValue() + value.doubleValue());
        }
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }
}

Edit: I will also add that the real bean for our project contains about 100 ints & doubles, and those can change over time as business requirements change. Maintaining a function that handles each one by name is not too appealing.

  • 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-05-31T15:27:51+00:00Added an answer on May 31, 2026 at 3:27 pm

    Unless you’re building a framework, I’d discourage using BeanUtils, or reflection in general, for this sort of tasks. It doesn’t give you any flexibility (you still have to name yourself the fields you don’t want to include, whenever something is added to SimpleBean).

    Adding properties explicitly has the advantage of being predictable and — if they become numerous — to rethink the design: perhaps arrays should be used?

    Otherwise, to improve your current solution, you could check for the type of the property with PropertyUtils.getPropertyDescriptor(...).getPropertyType(), and thus discover if it can safely be cast to java.lang.Number, or other primitive wrapper class.

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

Sidebar

Related Questions

I have a view-scoped bean ManageFoo.java: @ManagedBean(name = ManageFoo) @ViewScoped public class ManageFoo {
(Java Spring WebApp) I have: public class PersonValidator implements Validator { private PersonDAO d;
I have an entity class User that contains information such as username, first name,
I have a class which retrieves the data from DB. [Table(Name = Ilanlar)] public
I have such code: <h:inputText id=input value=#{bean.input}> <f:convertNumber /> <rich:ajaxValidator event=onblur /> </h:inputText> I
I have a bean of type string[] which has two or more values. I
I have the following classes defined: public interface Thingy { ... } public class
So I have a class of overloaded methods like this: class Foo { public
We have a Hibernate/Spring application that have the following Spring beans: <bean id=transactionManager class=org.springframework.orm.hibernate3.HibernateTransactionManager
@Entity @Inheritance(strategy = InheritanceType.SINGLE_TABLE) public class Problem { @ManyToOne private Person person; } @Entity

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.