英文:
How can I be able to perform a filter on a nested List within another java list
问题
免责声明: 我对Java还很陌生,我正在尝试使用Spring Boot中的流来转换此JSON以获得如下所示的输出。但我的所有尝试都失败了,我在做什么错?
以下是您提供的代码部分的翻译:
[
{
"id": 1,
"year": 2020,
"description": "Two thousand twenty",
"brands": [
{
"id": 1,
"name": "Tesla",
"description": "This is a car provider from USA",
"countryId": 16,
"brandNo": "TE001",
"categoryId": 23
},
{
"id": 2,
"name": "Toyota",
"description": "This is a car provider from Japan",
"countryId": 16,
"brandNo": "TE003",
"categoryId": 23
}
]
},
{
"id": 1,
"year": 2022,
"description": "Two thousand twenty",
"brands": [
{
"id": 1,
"name": "Tesla",
"description": "This is a car provider from USA",
"countryId": 16,
"brandNo": "TE001",
"categoryId": 23
},
{
"id": 2,
"name": "Toyota",
"description": "This is a car provider from Japan",
"countryId": 16,
"brandNo": "TE003",
"categoryId": 23
}
]
}
]
我看到您期望的输出是,如果用户传递了品牌名称 Toyota
,则输出应为:
[
{
"id": 1,
"year": 2020,
"description": "Two thousand twenty",
"brands": [
{
"id": 2,
"name": "Toyota",
"description": "This is a car provider from Japan",
"countryId": 16,
"brandNo": "TE003",
"categoryId": 23
}
]
},
{
"id": 1,
"year": 2022,
"description": "Two thousand twenty",
"brands": [
{
"id": 2,
"name": "Toyota",
"description": "This is a car provider from Japan",
"countryId": 16,
"brandNo": "TE003",
"categoryId": 23
}
]
}
]
以下是您提供的Java代码的翻译:
List<YearBrandDTO> years = yearRepository.findAll().stream()
.map(year -> year.getBrands().stream()
.filter(brand -> brand.getName().equals("Toyota")))
.map(year -> modelMapper.map(year, YearBrandDTO.class))
.collect(Collectors.toList());
您的DTO类 YearBrandDTO
如下所示:
@Data
public class YearBrandDTO {
private Long id;
private Integer year;
private String description;
private Object brands;
}
但您得到的输出是空值:
[
{
"id": null,
"year": null,
"description": null,
"brands": null
}
]
请注意,您的问题可能涉及到模型映射器(modelMapper)的配置或用法。要获得更详细的帮助,您可能需要检查您的模型映射器配置以确保正确映射数据。此外,确保您的数据源(yearRepository)中包含正确的数据,以便进行正确的筛选和映射。
英文:
Disclaimer: I am really new to Java and i am trying to transform this json in springboot using streams to obtain an output as indicated below. But all my trials are failing, what am i doing wrong with this.
[
{
"id": 1,
"year": 2020,
"description": "Two thousand twenty",
"brands": [
{
"id": 1,
"name": "Tesla",
"description": "This is a car provider from USA",
"countryId": 16,
"brandNo": "TE001",
"categoryId": 23
},
{
"id": 2,
"name": "Toyota",
"description": "This is a car provider from Japan",
"countryId": 16,
"brandNo": "TE003",
"categoryId": 23
}
]
},
{
"id": 1,
"year": 2022,
"description": "Two thousand twenty",
"brands": [
{
"id": 1,
"name": "Tesla",
"description": "This is a car provider from USA",
"countryId": 16,
"brandNo": "TE001",
"categoryId": 23
},
{
"id": 2,
"name": "Toyota",
"description": "This is a car provider from Japan",
"countryId": 16,
"brandNo": "TE003",
"categoryId": 23
}
]
}
]
The output i am look to get is based on if the user passes a name for the brand that "Toyota"
then the output should be.
[
{
"id": 1,
"year": 2020,
"description": "Two thousand twenty",
"brands": [
{
"id": 2,
"name": "Toyota",
"description": "This is a car provider from Japan",
"countryId": 16,
"brandNo": "TE003",
"categoryId": 23
}
]
},
{
"id": 1,
"year": 2022,
"description": "Two thousand twenty",
"brands": [
{
"id": 2,
"name": "Toyota",
"description": "This is a car provider from Japan",
"countryId": 16,
"brandNo": "TE003",
"categoryId": 23
}
]
}
]
My current code snippet I am using looks like the one below. Any help is highly appreciated.
List<YearBrandDTO> years = yearRepository.findAll().stream()
.map(year -> year.getBrands().stream()
.filter(brand -> brand.getName().equals("Toyota")))
.map(year -> modelMapper.map(year, YearBrandDTO.class))
.collect(Collectors.toList());
And my DTO looks like this
@Data
public class YearBrandDTO {
private Long id;
private Integer year;
private String description;
private Object brands;
}
But the output i get is null values
[
{
"id": null,
"year": null,
"description": null,
"brands": null
}
]
答案1
得分: 0
你试图将品牌过滤的结果直接映射到 YearBrandDTO
。但你应该先筛选品牌,然后创建一个新的 YearBrandDTO
对象。
就像这样:
List<YearBrandDTO> years = yearRepository.findAll().stream()
.map(year -> {
List<Brand> filteredBrands = year.getBrands().stream()
.filter(brand -> brand.getName().equals("Toyota"))
.collect(Collectors.toList());
YearBrandDTO yearBrandDTO = modelMapper.map(year, YearBrandDTO.class);
yearBrandDTO.setBrands(filteredBrands);
return yearBrandDTO;
})
.collect(Collectors.toList());
这样你应该能够获得所需的输出。只需更新你的 YearBrandDTO
类,使其具有 List<BrandDTO>
类型的品牌字段。
@Data
public class YearBrandDTO {
private Long id;
private Integer year;
private String description;
private List<BrandDTO> brands;
}
在你的情况下,我需要创建一个类似于你的 YearBrandDTO
类的 BrandDTO
类,以映射品牌字段。
英文:
You're trying to map the result of filtering brands directly to the YearBrandDTO
. But you should filter the brands first and then create a new YearBrandDTO
object.
Like this:
List<YearBrandDTO> years = yearRepository.findAll().stream()
.map(year -> {
List<Brand> filteredBrands = year.getBrands().stream()
.filter(brand -> brand.getName().equals("Toyota"))
.collect(Collectors.toList());
YearBrandDTO yearBrandDTO = modelMapper.map(year, YearBrandDTO.class);
yearBrandDTO.setBrands(filteredBrands);
return yearBrandDTO;
})
.collect(Collectors.toList());
This way you should be able to get the desired output. Just update your YearBrandDTO
class to have a List<BrandDTO>
instead of an Object for the brand's field.
@Data
public class YearBrandDTO {
private Long id;
private Integer year;
private String description;
private List<BrandDTO> brands;
}
In your case, I would need to create a BrandDTO
class similar to your YearBrandDTO
class to map the brand fields.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论