I have a method that returns a Tuple type object, with the values: category type Category and total type BigDecimal.
How do I implement a modelMapper method to map the Tuple to a Product type class with the two attributes (category - type Category and total - type BigDecimal)?
How to implement this method to convert a Tuple object of n values to any other object with n corresponding attributes?
How to implement this method in the case of List<Tuple>
to List <AnyClass>
?
In the case of List<Tuple>
to List<Product>
, I have the following piece of functional code. It is incomplete, it just maps the first attribute, in the case of category.
Note: productsTuple is a type of List<Tuple>
object returned from a repository.
List<Product> products = productsTuple
.stream()
.map(p -> modelMapper.map(p.get(0), Produto.class))
.collect(Collectors.toList());
You can try this. (Fixed up)
List<Product> products = productsTuple
.stream()
.map(p -> {
Map<String, Object> maps = new HashMap<>();
p.getElements().forEach(te -> {
maps.put(te.getAlias(), p.get(te.getAlias()));
});
return modelMapper.map(maps, Product.class));
})
.collect(Collectors.toList());