i wrote the below code to form custom objects. but its returning only single object. like below
{"fathername":null,"name":"ks"}
but i want like below(array object)
[{"name":"test","fathername":"test123"},{"name":"ks","fathername":null}]
but it is returning single object. what i am missing here, please pour your suggestions
public HashMap<String, String> applicantlistv2() {
List<Applicant> users = repository.findAll();
HashMap<String, String> myhash = new HashMap<String, String>();
for (Applicant applicant : users) {
myhash.put("fathername", applicant.getFather_name());
myhash.put("name", applicant.getFirst_name());
System.out.println(applicant.getFirst_name());
}
//return repository.findAll();
return myhash;
}
For each iteration you are replacing the same key. So only the data in the last iteration would be available.
Replace the code as follows:
List<Map<String, String>> myhash = new ArrayList<>();
for (Applicant applicant : users) {
Map<String, String> map = new HashMap<>();
map.put("fathername", applicant.getFather_name());
map.put("name", applicant.getFirst_name());
myhash.add(map);
System.out.println(applicant.getFirst_name());
}
return myhash;