0

I have the below code block which I'm trying to convert using java stream.

for(Emp emp:empList){
            if(combinedData.getOrg()==null){
                combinedData.setOrg(emp.getCompany());
            }
            combinedData.addToList(emp.getWorkLog());
            if(combinedData.getRawData()!=null){
                combinedData.setRawData(combinedData.getRawData()+emp.getData());
            }
            else{
                combinedData.setRawData(emp.getData());
            }
        }

The challenge I have is that I need to set Org only one and also I need to append to the existing rawdata. I know it can be easily done with reduced number of lines using streams.

The complete code is below

public class Main {
    public static void main(String[] args) {
        List<Emp> empList = Arrays.asList(
                new Emp("x1","as2112","a"),
                new Emp("x1","as2122","b"),
                new Emp("x1","as2422","c"),
                new Emp("x1","as2423","d"));
        CombinedData combinedData = new CombinedData();
        for(Emp emp:empList){
            if(combinedData.getOrg()==null){
                combinedData.setOrg(emp.getCompany());
            }
            combinedData.addToList(emp.getWorkLog());
            if(combinedData.getRawData()!=null){
                combinedData.setRawData(combinedData.getRawData()+emp.getData());
            }
            else{
                combinedData.setRawData(emp.getData());
            }
        }

}

}
class Emp{
    private String company;
    private String workLog;
    private String data;

    public Emp(
            String company,
            String workLog,
            String data) {
        this.company = company;
        this.workLog = workLog;
        this.data = data;
    }

    public String getCompany() {
        return company;
    }

    public void setCompany(String company) {
        this.company = company;
    }

    public String getWorkLog() {
        return workLog;
    }

    public void setWorkLog(String workLog) {
        this.workLog = workLog;
    }

    public String getData() {
        return data;
    }

    public void setData(String data) {
        this.data = data;
    }
}

class CombinedData{
    private String org;
    private List<String> worklogList;
    private String rawData;

    public String getOrg() {
        return org;
    }

    public void setOrg(String org) {
        this.org = org;
    }

    public List<String> getWorklogList() {
        return worklogList;
    }

    public void setWorklogList(List<String> worklogList) {
        this.worklogList = worklogList;
    }

    public String getRawData() {
        return rawData;
    }

    public void setRawData(String rawData) {
        this.rawData = rawData;
    }
    public void addToList(String worklog){
        this.worklogList.add(worklog);
    }
}
1
  • 1
    Your code causes a NullPointerException (CombinedData.addToList())! Commented Apr 22, 2019 at 18:28

2 Answers 2

2

I know it can be easily done with reduced number of lines using streams.

Well, it is not true. I only can advise you to add method to CombinedData

void add(Emp emp) {
    if(getOrg()==null){
        setOrg(emp.getCompany());
    }
    addToList(emp.getWorkLog());
    if(getRawData()!=null){
        setRawData(getRawData()+emp.getData());
    }
    else{
        setRawData(emp.getData());
    }
}

and then apply it to the stream

CombinedData combinedData = new CombinedData();
empList.forEach(combinedData::add);
Sign up to request clarification or add additional context in comments.

Comments

0

As an aside, this particular step is better as an operation of 'combinedData':

if ( combinedData.getRawData() != null ) {
    combinedData.setRawData( combinedData.getRawData() + emp.getData() );
} else{
    combinedData.setRawData( emp.getData() );
}

combinedData.addRawData( emp.getData() );

What is telling is that the code has several calls to 'combinedData'. Does the caller really need to know the state of 'combinedData', or how to combine existing raw data with new raw data (implemented here by concatenation)?

(Also, placing the step in this fashion works much much better if ever you were to add synchronization / had to make this code thread safe.)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.