I have a JSON with a list of students as below - for each Student, I want to print the hobbies, if no hobby is present show NA using Java 8. I have written the below code which is working but I am having to use 2 maps. Please suggest if this code can be optimized.
JSON
{
"students":[
{
"fullName":"AAAAA",
"standard":10,
"hobbies":[
"programming",
"reading"
]
},
{
}
]
}
JAVA 8
Map<String, List<Student>> studentMap = reportCard.getStudents().stream().collect(Collectors.groupingBy(Student::getFullName));
Map<String, String> studentMap2 = new TreeMap<>();
studentMap.keySet().forEach(key -> Optional.ofNullable(studentMap.get(key).get(0).getHobbies())
.ifPresentOrElse(hobbies -> studentMap2.put(key, hobbies.stream().map(String::trim).collect(Collectors.joining(";"))), () -> studentMap2.put(key, "NA")));
Using Java 8
You can use the collectors.toMap
from java 8 to have the desired output as shown in the code below:
Here,
collectors.toMap
in which we will have the key as student Name and for the value i have checked if the hobbies list is not null and should not be empty and then perform the downstream operation of joining all the hobbies of the student otherwise it will have "NA" and i have used merger (a,b) -> a + "," + b
, it will combine the hobbies of same student with same name if exists and TreeMap::new
will sort the hobbies in natural order.Code:
public class Test {
public static void main(String[] args) throws JsonProcessingException {
String input = """
{
"students": [
{
"fullName": "AAAAA",
"standard": 10,
"hobbies": [
"programming",
"reading"
]
},
{
"fullName": "BBBB",
"standard": 12,
"hobbies": [
"music",
"reading"
]
},
{
"fullName": "CCCC",
"standard": 12,
"hobbies": [
]
},
{
}
]
}
""";
ObjectMapper mapper = new ObjectMapper();
StudentData data = mapper.readValue(input,StudentData.class);
Map<String,String> output =
data.getStudentList().stream().filter(a -> a.getName()!=null)
.collect(Collectors.toMap(Student::getName,
x -> (x.getHobbies()!=null && !x.getHobbies().isEmpty() ? String.join(",",x.getHobbies()) : "NA")
,(a,b) -> a + "," + b,TreeMap::new));
System.out.println(output);
}
}
Output::
{AAAAA=programming,reading, BBBB=music,reading, CCCC=NA}