I query the DB for two columns where the first one is the key to the second one. How can I convert the resulting list to a single map? Is it even possible? I have just seen examples with beans.
List<Map<String, Object>> steps = jdbcTemplate.queryForList(
"SELECT key, value FROM table");
// well this doesn't work
Map<String, String> result = steps.stream()
.collect(Collectors.toMap(s -> s.get("key"), s -> s.get("value")));
We can merge Map collections using the merge() method. This returns a copy of the collection with the remaining collections merged in. We get the same results as the previous example when we run the above code. The merge() method can also be used to merge Map collections.
You can get the name of the Sobject using the method getSObjectType(); Try the below code where I have used Map to store the SobjectType and the List of all records of that Sobject. Map<Schema. SObjectType, List<SObject>> objMap = new Map<Schema. SObjectType, List<SObject>>(); for(Sobject obj : sObj){ Schema.
With Java 8, you can convert a List to Map in one line using the stream() and Collectors. toMap() utility methods. The Collectors. toMap() method collects a stream as a Map and uses its arguments to decide what key/value to use.
We can use the reduce() of java streams to convert a list of maps to a single map in java.
Please check the following code on how it is used.
For example:
@Data
@AllArgsConstructor
public class Employee {
private String employeeId;
private String employeeName;
private Map<String,Object> employeeMap;
}
public class Test{
public static void main(String[] args) {
Map<String, Object> map1 = new HashMap<>();
Map<String, Object> map2 = new HashMap<>();
Map<String, Object> map3 = new HashMap<>();
map1.put("salary", 1000);
Employee e1 = new Employee("e1", "employee1", map1);
map2.put("department", "HR");
Employee e2 = new Employee("e2", "employee2", map2);
map3.put("leave balance", 14);
Employee e3 = new Employee("e3", "employee3", map3);
//now we create a employees list and add the employees e1,e2 and e3.
List<Employee> employeeList = Arrays.asList(e1,e2,e3);
//now we retreive employeeMap from all employee objects and therefore have a List of employee maps.
List<Map<String, Object>> employeeMaps = employeeList
.stream()
.map(Employee::getEmployeeMap)
.collect(Collectors.toList());
System.out.println("List of employee maps: " + employeeMaps);
// to reduce a list of maps to a single map, we use the reduce function of stream.
Map<String, Object> finalMap = employeeMaps
.stream()
.reduce((firstMap, secondMap) -> {
firstMap.putAll(secondMap);
return firstMap;
}).orElse(null);
System.out.println("final Map: "+ finalMap);
}
}
Output: List of employee maps: [{salary=1000}, {department=HR}, {leave balance=14}].
final Map: {salary=1000, department=HR, leave balance=14}
PS: Apologies for the extended answer, this is my first time in stackoverflow. Thank you :-)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With