DTO Combination Processing Configured in the LIST

Asked 1 years ago, Updated 1 years ago, 61 views

I'm trying to combine the DTO configured in the LIST with the key in the for statement, but I can't think of a good way to do it...

What you want to do:
DTOs with hoge1, hoge2, and hoge3 in the nest are put in array1.I would like to combine records with key matches and set them to arrayEx.
If the key does not match, set it to arrayEx without doing anything.

Finally, there was one arrayEx with hoge1, hoge2, and hoge3.
Ideally, one key with only hoge3 configured is 2.

I was able to reproduce it using many for statements, but it didn't work because it was slow...

I would appreciate it if someone could tell me.

Here's the source.

public class Test{
    public static void main(String[]args) {
        Test1 test1 = new Test1();
        ArrayList<Test1>array1 = new ArrayList<Test1>();
        ArrayList<Test1>array2 = new ArrayList<Test1>();
        ArrayList<Test1>arrayEx=new ArrayList<Test1>();

        test1.setKey("1");
        test1.setHoge1("hoge1");
        test1.setHoge2(null);
        test1.setHoge3(null);
        array1.add(test1);
        test1 = new Test1();

        test1.setKey("1");
        test1.setHoge1(null);
        test1.setHoge2("hoge2");
        test1.setHoge3(null);
        array1.add(test1);
        test1 = new Test1();

        test1.setKey("1");
        test1.setHoge1(null);
        test1.setHoge2(null);
        test1.setHoge3("hoge3");
        array1.add(test1);
        test1 = new Test1();

        test1.setKey("2");
        test1.setHoge1(null);
        test1.setHoge2(null);
        test1.setHoge3("hoge3");
        array1.add(test1);
        test1 = new Test1();

        int count = array1.size();

        inti = 0;
        for(i=0;i<count-1;i++){
            Test1 A = new Test1();
            Test1 B = new Test1();
            A = array1.get(i);
            B = array1.get(i+1);

            if(A.getKey().equals(B.getKey())){

                if(!(A.getHoge1()==null)){
                    B.setHoge1(A.getHoge1());
                }

                if(!(A.getHoge2()==null)){
                    B.setHoge2(A.getHoge2());
                }

                if(!(A.getHoge3()==null)){
                    B.setHoge3(A.getHoge2());
                }

                arrayEx.add(B);

            } else{
                arrayEx.add(A);
            }
        }
    }

}

The following is the current situation...I've been trying various things, so I've made a lot of corrections...

public static void main(String[]args){
        Test1 test1 = new Test1();
        List<Test1>array1 = new ArrayList<Test1>();
        ArrayList<Test1>array2 = new ArrayList<Test1>();
        List <Test1>arrayEx = new ArrayList<Test1>();

        test1.setKey("1");
        test1.setHoge1("hoge1");
        test1.setHoge2(null);
        test1.setHoge3(null);
        test1.setHoge4(null);
        array1.add(test1);
        test1 = new Test1();

        test1.setKey("1");
        test1.setHoge1(null);
        test1.setHoge2("hoge2");
        test1.setHoge3(null);
        test1.setHoge4(null);
        array1.add(test1);
        test1 = new Test1();

        test1.setKey("1");
        test1.setHoge1(null);
        test1.setHoge2(null);
        test1.setHoge3("hoge3");
        test1.setHoge4(null);
        array1.add(test1);
        test1 = new Test1();

        test1.setKey("1");
        test1.setHoge1(null);
        test1.setHoge2(null);
        test1.setHoge3(null);
        test1.setHoge4("hoge4");
        array1.add(test1);
        test1 = new Test1();


        test1.setKey("2");
        test1.setHoge1("hoge1");
        test1.setHoge2(null);
        test1.setHoge3(null);
        test1.setHoge4(null);
        array1.add(test1);
        test1 = new Test1();

        test1.setKey("2");
        test1.setHoge1(null);
        test1.setHoge2(null);
        test1.setHoge3("hoge3");
        test1.setHoge4(null);
        array1.add(test1);
        test1 = new Test1();

        int count = array1.size();

        inti = 0;
        Iterator<Test1>it=array1.iterator();



        // Current record
        Test1 A = new Test1();
        // Next Record
        Test1 B = new Test1();
        // Temporary evacuation
        Test1 C = new Test1();
        while(it.hasNext()){

            B = array1.get(i+1);

            if(it.hasNext()){

            A = it.next();
            }
            if(!(C.getKey()==null)){
                if(B.getKey().equals(C.getKey())){
                    if(!(B.getHoge1()==null)){
                        C.setHoge1(B.getHoge1());
                    }

                    if(!(B.getHoge2()==null)){
                        C.setHoge2(B.getHoge2());
                    }

                    if(!(B.getHoge3()==null)){
                        C.setHoge3(B.getHoge3());
                    }
                    if(!(Objects.equals(B.getHoge4(), null))){
                        C.setHoge4(B.getHoge4());
                    }
                    i = i+1;
                } else {
                    arrayEx.add(C);
                    C = new Test 1();
                    i = i+1;
                }



            } 

            if(C.getKey()==null){
                if(A.getKey().equals(B.getKey())){

                    if(!(A.getHoge1()==null)){
                        C.setHoge1(A.getHoge1());
                    } else if(!(B.getHoge1()==null)) {
                        C.setHoge1(B.getHoge1());
                    }

                    if(!(A.getHoge2()==null)){
                        C.setHoge2(A.getHoge2());
                    } else if(!(B.getHoge2()==null)) {
                        C.setHoge2(B.getHoge2());
                    }

                    if(!(A.getHoge3()==null)){
                        C.setHoge3(A.getHoge3());
                    } else if(!(B.getHoge3()==null)) {
                        C.setHoge3(B.getHoge3());
                    }

                    if(!(A.getHoge4()==null)){
                        C.setHoge4(A.getHoge4());
                    } else if(!(B.getHoge4()==null)) {
                        C.setHoge4(B.getHoge4());
                    }

                    C.setKey(A.getKey());

                    i = i+1;

                } else{
                    arrayEx.add(B);
                    i = i+1;
                }
            }
        }
    }

java

2022-09-30 21:46

2 Answers

If you want to aggregate or aggregate multiple data with the same key, you will generally use the Map type.

import java.util.*;

class Test1 {
    private String key;
    private String hoge1;
    private String hoge2;
    private String hoge3;

    public void setKey(String newKey){
        This.key = newKey;
    }
    public void setHoge1 (String newHoge1) {
        this.hoge1 = newHoge1;
    }
    public void setHoge2 (String newHoge2) {
        this.hoge2 = newHoge2;
    }
    public void setHoge3(String newHoge3){
        this.hoge3 = newHoge3;
    }
    public String getKey() {
        return this.key;
    }
    public String getHoge1(){
        return this.hoge1;
    }
    public String getHoge2(){
        return this.hoge2;
    }
    public String getHoge3(){
        return this.hoge3;
    }

    public String to String() {
        return String.format("{key=>%s, hoge1=>%s, hoge2=>%s, hoge3=>%s", getKey(), getHoge1(), getHoge2(), getHoge3());
    }
}

public class test {
    static ArrayList<Test1>createExampleData(){
        ArrayList<Test1>exampleData=new ArrayList<Test1>();
        Test1 test1;

        test1 = new Test1();
        test1.setKey("1");
        test1.setHoge1("1-hoge1");
        test1.setHoge2(null);
        test1.setHoge3(null);
        exampleData.add(test1);

        test1 = new Test1();
        test1.setKey("1");
        test1.setHoge1(null);
        test1.setHoge2("1-hoge2");
        test1.setHoge3(null);
        exampleData.add(test1);

        test1 = new Test1();
        test1.setKey("1");
        test1.setHoge1(null);
        test1.setHoge2(null);
        test1.setHoge3("1-hoge3");
        exampleData.add(test1);

        test1 = new Test1();
        test1.setKey("2");
        test1.setHoge1(null);
        test1.setHoge2(null);
        test1.setHoge3("2-hoge3");
        exampleData.add(test1);

        return exampleData;
    }   

    public static void main(String[]args) {
        ArrayList<Test1>array1=createExampleData();
        ArrayList<Test1>arrayEx=new ArrayList<Test1>();
        TreeMap<String, Test1>tmpTable=new TreeMap<String, Test1>();;
        System.out.println("Input array");

        for (Test1 test1:array1) {
            System.out.println(test1);
            String key = test1.getKey();
            if(tmpTable.containsKey(key)){
                // If a key has already appeared, remove it from the tmpTable and bind it to it.
                Test1 data = tmpTable.get(key);
                if(test1.getHoge1()!=null){
                    data.setHoge1(test1.getHoge1());
                }

                if(test1.getHoge2()!=null){
                    data.setHoge2(test1.getHoge2());
                }

                if(test1.getHoge3()!=null){
                    data.setHoge3(test1.getHoge3());
                }
            }
            else{
                // Store the first key in the tmpTable
                tmpTable.put(key, test1);
            }
        }

        System.out.println("Output array");
        arrayEx.addAll(tmpTable.values());
        for (Test1 test1:arrayEx) {
            System.out.println(test1);
        }
    }
}

Here are the results.

Input array
{ key=>1,hoge1=>1-hoge1,hoge2=>null,hoge3=>null}
{ key=>1,hoge1=>null,hoge2=>1-hoge2,hoge3=>null}
{ key=>1,hoge1=>null,hoge2=>null,hoge3=>1-hoge3}
{ key=>2,hoge1=>null,hoge2=>null,hoge3=>2-hoge3}
Output array
{ key=>1,hoge1=>1-hoge1,hoge2=>1-hoge2,hoge3=>1-hoge3}
{ key=>2,hoge1=>null,hoge2=>null,hoge3=>2-hoge3}


2022-09-30 21:46

Stream API groupingBy provides collect to classify the listed objects by key:

Map<String, List<Test1>>group=array1.stream().collect(Collectors.groupingBy(Test1::getKey)));

Now that you have a list of Test1 objects classified by key, you can "combine" all the objects in this list to get the desired results.

"Binding" is also available in the Stream API reduce:

for(List<Test1>list:group.values()){
    // All the contents of the list are grouped with the same key.
    // Combine everything in turn
    Optional <Test1>merged=list.stream().reduce(t1,t2)->{
        String t1 Hoge1 = t1.getHoge1();
        if(t1Hoge1!=null){
            t2.setHoge1(t1Hoge1);
        }
        String t1 Hoge2 = t1.getHoge2();
        if(t1Hoge2!=null){
            t2.setHoge2(t1Hoge2);
        }
        String t1 Hoge3 = t1.getHoge3();
        if(t1Hoge3!=null){
            t2.setHoge3(t1Hoge3);
        }
        return t2;
    });

    // Store binding results
    merged.ifPresent(arrayEx::add);
}

In addition, this "combination" is a common process for handling DTOs, and several libraries have been developed to perform this process.
The (primary) purpose of these libraries is to reduce the so-called boilerplate code.

You can see some of them on the next page:

For your reference, link the implementation samples using MapStruct:


2022-09-30 21:46

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.