简体   繁体   中英

how to store the data column wise in array list in java?

hi everyone , I have a Map<String, List> ie string store the header and list of object store their values respectively. I want to store the data in array list one by one as given below.

here is data format I want to store in array list:-

 [ Pojo(foo1=636580, foo2=CIVIL, foo3=29/06/2021),
     Pojo(foo1=636580, foo2=CIVIL, foo3=29/06/2021),
     Pojo(foo1=636580, foo2=CIVIL, foo3=29/06/2021), 
     Pojo(foo1=636580, foo2=CIVIL, foo3=29/06/2021), 
     Pojo(foo1=636580, foo2=CIVIL, foo3=29/06/2021), 
   ]

but the data is stored as :

[
     Pojo(foo1=636580, foo2=N/A, foo3=N/A),
     Pojo(foo1=636580, foo2=N/A, foo3=N/A),
     Pojo(foo1=636580, foo2=N/A, foo3=N/A), 
     Pojo(foo1=636580, foo2=N/A, foo3=N/A), 
     Pojo(foo1=636580, foo2=N/A, foo3=N/A), 
     
     Pojo(foo1=N/A, foo2=CIVIL  , foo3=N/A), 
     Pojo(foo1=N/A, foo2=CIVIL  , foo3=N/A), 
     Pojo(foo1=N/A, foo2=CIVIL  , foo3=N/A), 
     Pojo(foo1=N/A, foo2=CIVIL  , foo3=N/A), 
     Pojo(foo1=N/A, foo2=CIVIL  , foo3=N/A),


     Pojo(foo1=N/A, foo2=N/A, foo3=29/06/2021),
     Pojo(foo1=N/A, foo2=N/A, foo3=29/06/2021),
     Pojo(foo1=N/A, foo2=N/A, foo3=29/06/2021),
     Pojo(foo1=N/A, foo2=N/A, foo3=29/06/2021),
     Pojo(foo1=N/A, foo2=N/A, foo3=29/06/2021)
]

here is my pojo class:-

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Pojo {
    private String foo1= "N/A";
    private String foo2= "N/A";
    private String foo3= "N/A";
}

here is my code :-

Map<String, List<Object>> myMap = new HashMap<>();
        List<String> strings = new ArrayList<>();
        strings.add("foo1");
        strings.add("foo2");
        strings.add("foo3");

        List<Pojo> pojos = new ArrayList<>();
        for (String string : strings) {
            Pojo pojo = new Pojo();
            for (Map.Entry<String, List<Object>> entry : myMap.entrySet()) {
                int max = myMap.values().stream().map(List::size).max(Integer::compareTo).get() - 1;
                if (entry.getKey().equalsIgnoreCase(string)) {
                    while (max >= 0) {
                        pojo = getPropertyValue(entry.getKey(), entry.getValue().get(max), pojo);
                        pojos.add(pojo);

                        max--;
                    }
                }
            }
        }
        System.out.println(pojos);

here is my getPropertyValue method :-

Class<?> unreconciledTransactionClass = Class.forName("com.example.looping.rohit.Pojo"); // convert string classname to class
        Object data;
        if (pojo != null) {
            data = pojo;
        } else {
            data = unreconciledTransactionClass.getDeclaredConstructor().newInstance(); // invoke empty constructor

        }
        PropertyAccessor myAccessor = PropertyAccessorFactory.forBeanPropertyAccess(data);
        myAccessor.setPropertyValue(attributeName, value);
        return (Pojo) data;
    }

Below is the code that should solve your problem. I have added a try / catch block in order to handle the case when you have lists with different size in myMap.

int max = myMap.values().stream().map(List::size).max(Integer::compareTo).get();
List<Pojo> pojos = new ArrayList<>();
for(int i=0; i<max; i++) {
    Pojo pojo = new Pojo();
    for (String string : strings) {
        String value;
        try {
             value = myMap.get(string).get(i);
        }catch(IndexOutOfBoundsException e) {
             value =  "N/A";
        }
        pojo = getPropertyValue(string,value, pojo);
    }
    pojos.add(pojo);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM