【问题标题】:How get child elements with java lambda to put in a Map<Long, List<Long>>?如何使用 java lambda 获取子元素以放入 Map<Long, List<Long>>?
【发布时间】:2018-11-16 07:14:18
【问题描述】:

我有一个具有这种结构的对象:

@JsonProperty("id")
private Long codigoCategoria;

@JsonProperty("parentId")
private Long codigoCategoriaPai;

@JsonProperty("name")
private String nomeCategoria;

@JsonInclude(JsonInclude.Include.NON_EMPTY)
private ComissaoPadraoEntity comissao;

@JsonProperty("categories")
private List<CategoriaDTO> subCategorias;

你怎么看,它有一个他自己类型的列表,我需要用 Map &lt;Long,List&lt;Long&gt;&gt; 映射这个类别。其中key是codigoCategoria,value必须是一个Long List,子Categorias里面有codigoCategoria。

这是有效载荷结构:

  {
  "categories": [
    {
      "id": "1813",
      "parentId": null,
      "name": "Malas e Mochilas",
      "items": 12,
      "categories": [
        {
          "id": "1827",
          "parentId": "1813",
          "name": "Conjuntos de Malas",
          "items": 0,
          "categories": [

          ],
          "attributes": null
        },
        {
          "id": "1830",
          "parentId": "1813",
          "name": "Mochilas",
          "items": 4,
          "categories": [
            {
              "id": "1831",
              "parentId": "1830",
              "name": "Mochila Esportiva",
              "items": 0,
              "categories": [

              ],

到目前为止,我已经尝试了许多不同的方法,这是我完成的代码,但甚至无法编译:

private Map<Long, List<Long>> mapATreeofCategories() {
        List<CategoriaDTO> categories = getAll();
        Map<Long, List<Long>> treeCategories = categories.forEach(categoriaDTO -> {
            categories.stream()
                    .collect(Collectors.toMap(categoriaDTO.getCodigoCategoria(),
                            categoriaDTO.getSubCategorias().forEach(categoriaDTO1 -> categoriaDTO1.getCodigoCategoria())));
        });
        return treeCategories;
    }

感谢大家的帮助。

【问题讨论】:

  • 你得到什么错误?
  • 我得到这个错误:Collectors.toMap 不能被应用(长,空)。我试图映射属性,平面地图但也不起作用。
  • 请随时edit 将您的问题与错误消息联系起来。问题是forEach() 返回void,因此它不能以您尝试使用它的方式使用。我不熟悉 Stream API,因此无法提供更多帮助。
  • Map&lt;Long, List&lt;Long&gt;&gt; treeCategories = categories.forEach... 你知道 forEach 是 Consumer 并且有 void 类型。
  • @GustavoSimõesdeMoraes 你可能只想要categories.stream() .collect(Collectors.toMap(c -&gt; c.getCodigoCategoria(), v -&gt; v.getSubCategorias() .stream() .map(e -&gt; e.getCodigoCategoria()).collect(Collectors.toList()) ); ?如果没有,那么您需要编辑您的帖子以明确您的目标。

标签: java dictionary lambda


【解决方案1】:

forEach 方法具有void 返回类型,因此不能用作valueMapper 函数中的返回值。

相反,您似乎想从subCategories 集合中提取CodigoCategoria,在这种情况下,您需要执行以下操作:

categories.stream() 
          .collect(Collectors.toMap(k -> k.getCodigoCategoria(), 
                      v -> v.getSubCategorias().stream() 
                             .map(e -> e.getCodigoCategoria())
                             .collect(Collectors.toList()) 
          );

【讨论】:

  • 我明白。我只有一个问题,我可以用 lambda 使这个方法递归吗,例如,我有一个具有三个或更多级别的结构,假设我有一个对象 CategoriaDTO 并有一个列表,在这个列表中我有很多 CategoriaDTO 但在那 lis 可以有一个列表。如果我只有两个级别可以正常工作,但有更多则不包含在列表中。
  • @GustavoSimõesdeMoraes 你不能在Long 内嵌套任何东西,所以你的结构(Map &lt;Long,List&lt;Long&gt;&gt;)的最大深度只有两个级别(根和子根)
  • @Nikolay Shevchenko 但甚至让这个方法递归?我在另一种方法中做了类似的事情,但不是只在 forEach 中使用这个级别的 lambda。我正在尝试收集值,递归调用此方法但不起作用:(
猜你喜欢
  • 1970-01-01
  • 2019-07-31
  • 1970-01-01
  • 2018-04-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-25
  • 1970-01-01
相关资源
最近更新 更多