如何能够对一个嵌套在另一个Java列表中的列表执行筛选操作?

huangapple go评论73阅读模式
英文:

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.

[
    {
        &quot;id&quot;: 1,
        &quot;year&quot;: 2020,
        &quot;description&quot;: &quot;Two thousand twenty&quot;,
        &quot;brands&quot;: [
            {
                &quot;id&quot;: 1,
                &quot;name&quot;: &quot;Tesla&quot;,
                &quot;description&quot;: &quot;This is a car provider from USA&quot;,
                &quot;countryId&quot;: 16,
                &quot;brandNo&quot;: &quot;TE001&quot;,
                &quot;categoryId&quot;: 23
            },
            {
                &quot;id&quot;: 2,
                &quot;name&quot;: &quot;Toyota&quot;,
                &quot;description&quot;: &quot;This is a car provider from Japan&quot;,
                &quot;countryId&quot;: 16,
                &quot;brandNo&quot;: &quot;TE003&quot;,
                &quot;categoryId&quot;: 23
            }
        ]
    },
    {
        &quot;id&quot;: 1,
        &quot;year&quot;: 2022,
        &quot;description&quot;: &quot;Two thousand twenty&quot;,
        &quot;brands&quot;: [
            {
                &quot;id&quot;: 1,
                &quot;name&quot;: &quot;Tesla&quot;,
                &quot;description&quot;: &quot;This is a car provider from USA&quot;,
                &quot;countryId&quot;: 16,
                &quot;brandNo&quot;: &quot;TE001&quot;,
                &quot;categoryId&quot;: 23
            },
            {
                &quot;id&quot;: 2,
                &quot;name&quot;: &quot;Toyota&quot;,
                &quot;description&quot;: &quot;This is a car provider from Japan&quot;,
                &quot;countryId&quot;: 16,
                &quot;brandNo&quot;: &quot;TE003&quot;,
                &quot;categoryId&quot;: 23
            }
        ]
    }
]

The output i am look to get is based on if the user passes a name for the brand that &quot;Toyota&quot; then the output should be.

[
    {
        &quot;id&quot;: 1,
        &quot;year&quot;: 2020,
        &quot;description&quot;: &quot;Two thousand twenty&quot;,
        &quot;brands&quot;: [
            {
                &quot;id&quot;: 2,
                &quot;name&quot;: &quot;Toyota&quot;,
                &quot;description&quot;: &quot;This is a car provider from Japan&quot;,
                &quot;countryId&quot;: 16,
                &quot;brandNo&quot;: &quot;TE003&quot;,
                &quot;categoryId&quot;: 23
            }
        ]
    },
    {
        &quot;id&quot;: 1,
        &quot;year&quot;: 2022,
        &quot;description&quot;: &quot;Two thousand twenty&quot;,
        &quot;brands&quot;: [
            {
                &quot;id&quot;: 2,
                &quot;name&quot;: &quot;Toyota&quot;,
                &quot;description&quot;: &quot;This is a car provider from Japan&quot;,
                &quot;countryId&quot;: 16,
                &quot;brandNo&quot;: &quot;TE003&quot;,
                &quot;categoryId&quot;: 23
            }
        ]
    }
]

My current code snippet I am using looks like the one below. Any help is highly appreciated.

  List&lt;YearBrandDTO&gt; years = yearRepository.findAll().stream()
                    .map(year -&gt; year.getBrands().stream()
                            .filter(brand -&gt; brand.getName().equals(&quot;Toyota&quot;)))
                    .map(year -&gt; 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

   [
        {
            &quot;id&quot;: null,
            &quot;year&quot;: null,
            &quot;description&quot;: null,
            &quot;brands&quot;: 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&lt;YearBrandDTO&gt; years = yearRepository.findAll().stream()
    .map(year -&gt; {
        List&lt;Brand&gt; filteredBrands = year.getBrands().stream()
            .filter(brand -&gt; brand.getName().equals(&quot;Toyota&quot;))
            .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&lt;BrandDTO&gt; instead of an Object for the brand's field.

@Data
public class YearBrandDTO {
    private Long id;
    private Integer year;
    private String description;
    private List&lt;BrandDTO&gt; brands;
}

In your case, I would need to create a BrandDTO class similar to your YearBrandDTO class to map the brand fields.

huangapple
  • 本文由 发表于 2023年3月31日 17:38:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/75896970.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定