Operations on properties in Java Class

44
June 13, 2019, at 12:20 PM

I have a class:

class Test{
 private int a;
 private int b;
 //setters, getters
}

I create 2 instances of type Test. They are initialized and fields are populated via 2 sources (say a file and database).

Test testFileValues = new Test();
Test testDBValues = new Test();
//code to populate the above 2 objects goes here where i read from file and db and populate above 2 objects using some logic.

After that I need to add each property from one class with the other and produce a sum of the 2 classes in a third result class.

So basically,

Test resultTest = new Test();
resultTest.setA(testFileValues.getA() + testDBValues.getA());
resultTest.setB(testFileValues.getB() + testDBValues.getB());

Is there a cleaner way to do this via Java 8 like lambda or streams? I thought of using reflection and getting the class attributes in a for loop and perform operations but that looks more tedious than my current solution.

Thanks in advance!

Answer 1

Is there a cleaner way to do this via Java 8 like lambda or streams?

No.

Your current solution is as clean as it gets. Java cannot automatically infer that you want to treat all the variables the same and add them together or anything; nothing in Java 8 will help this.

READ ALSO
Display annotation markers in Eclipse compare editor

Display annotation markers in Eclipse compare editor

Unable to display annotation markers in Eclipse compare editor when I tried comparing two files

24
Failed startup of context o.e.j.w.WebAppContext@34cd072c

Failed startup of context o.e.j.w.WebAppContext@34cd072c

I am running Docker, the container got started but throws the below error

42
How to create an snmp trap receiver and sender that supports TSM based security model in SNMP4J?

How to create an snmp trap receiver and sender that supports TSM based security model in SNMP4J?

I'm trying to create a snmp trap receiver and sender to support snmp v3 traps using SNMP4j apiI have few resources and documentation to implement them using USM based security model but I want to use TSM based security model to support version 3 traps

48
Can I use SalesforceDX as a version control for an existing Eclipse project?

Can I use SalesforceDX as a version control for an existing Eclipse project?

I am looking to use Salesforce as a version control solution for my Eclipse Java projectSo far, I have been unsuccessful at finding any information pertaining to this specific scenario

31