Q: change list creation to json with lambda Java I need to change the response type.
create the response in that part of the code
public Map<String, Double> buscaNotavsTipo(Integer idusuario){
    List<NotasMateria> notasMaterias = repository.findAll();
    Map<String, Double> filtered = notasMaterias.stream().
            filter(t -> t.getIdUsuario()== idusuario)
            .collect(Collectors.groupingBy(NotasMateria::getTipoNota,
                     Collectors.mapping(NotasMateria::getNotaMateria,
                             averagingDouble(Double::doubleValue))));
     return  filtered;
}

he brings that answer
  {
tipo_nota: Trabalho
nota: 8.2
tipo_nota: Prova
nota: 5.7
}

I need this response format
    {
  {
    tipo_nota: Trabalho
    nota: 8.2
  },
  {
    tipo_nota: Prova
    nota: 5.7
  }
}


A: It seems that a specific DTO should be created to represent the output and a list of these DTOs should be returned instead of the Map<String, Double>.
Example implementation of the DTO (using Lombok).
Also, a special constructor is provided to facilitate conversion of Map.Entry<String, Double> to the DTO instance, which would allow to use the DTO constructor as a method reference.
@Data
@AllArgsConstructor
@JsonNaming(PropertyNamingStrategy.SnakeCaseStrategy.class)
class Output {
    private String tipoNota;
    private double nota;

    public Output(Map.Entry<String, Double> entry) {
        this(entry.getKey(), entry.getValue()); // call all-args constructor provided by Lombok
    }
}

Existing method should be refactored to return List<Output>:
public static List<Output> buscaNotavsTipoList(List<NotasMateria> notasMaterias, Integer idusuario) {
        return notasMaterias.stream()
                .filter(t -> t.getIdUsuario()== idusuario)
                .collect(Collectors.groupingBy(NotasMateria::getTipoNota,
                        Collectors.mapping(NotasMateria::getNotaMateria,
                                averagingDouble(Double::doubleValue))))
                .entrySet().stream()
                .map(Output::new)
                .collect(Collectors.toList());
}

Test (using Jackson ObjectMapper)
ObjectMapper mapper = new ObjectMapper();

String json = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(
        buscaNotavsTipoList(
                Arrays.asList(
                        new NotasMateria(1, "Trabalho", 8.0),
                        new NotasMateria(1, "Trabalho", 8.4),
                        new NotasMateria(1, "Prova", 5.9),
                        new NotasMateria(1, "Prova", 5.5)
                ),
                1
        ));

        System.out.println(json);

Output
[ {
  "tipo_nota" : "Trabalho",
  "nota" : 8.2
}, {
  "tipo_nota" : "Prova",
  "nota" : 5.7
} ]


