Easiest way would be to create a method toHashMap()
in your Student
class:
public LinkedHashMap<String,Object> toHashMap(){
LinkedHashMap<String, Object> h = new LinkedHashMap<>();
h.put("Id",id);
h.put("Name", name);
h.put("Age",age);
return h;
}
I use a LinkedHashMap
to preserve the insertion order. Once that is done, try this:
List<Student> ls = new ArrayList<>();
ls.add(new Student(12123,"Tom",12));
ls.add(new Student(12354,"George",21));
ls.add(new Student(12245,"Sara",16));
ls.add(new Student(17642,"Caitlyn",11));
List<LinkedHashMap<String,Object>> names = ls.stream().map(Student::toHashMap).collect( Collectors.toList() );
System.out.println(names);
Output: [{Id=12123, Name=Tom, Age=12}, {Id=12354, Name=George, Age=21}, {Id=12245, Name=Sara, Age=16}, {Id=17642, Name=Caitlyn, Age=11}]