设置< object>Map< string,设置< long>>java8

ncecgwcz  于 2021-07-11  发布在  Java
关注(0)|答案(4)|浏览(348)

假设我有下一个类结构:

@Getter
@Setter
public class Section {

    private Long id;

    private Set<Grid> grids;
}

@Getter
@Setter
public class Grid {

    private Long id;

    private Set<Row> rows;
}

@Getter
@Setter
public class Row {
    private Long id;

    private String email;
}

我有一组东西:

Set<Section> sections;

假设这一节在下一个结构中将值设置为json:

[
  {
    "id": 1,
    "grids": [
      {
        "id": 10,
        "rows" [
          {
            "id": 50,
            "email": "email1@test.com"
          },
          {
            "id": 51,
            "email": "email2@test.com"
          }
        ]
      }  
    ]
  },
  {
    "id": 2,
    "grids": [
      {
        "id": 11,
        "rows" [
          {
            "id": 60,
            "email": "email1@test.com"
          }
        ]
      }  
    ]
  }  
]

假设我有多个部分,这些部分有多个网格,每个网格有多行,每行的email属性可以存在于不同的网格中。
现在我需要将这个集合转换为java.util.map<string,set>,这个Map表示行对象email作为一个键,一组节id作为该Map键的值。所以我需要像map<email,set<section1id,section2id,等等…>>这样的结果,在json中(这只是为了澄清这个想法):

[
  {
    "key": "email1@test.com",
    "value": [1, 2] // Section ids
  },
  {
    "key": "email2@test.com",
    "value": [1] // Section ids
  }
]

OR like that (whatever)

[
  "email1@test.com": {1, 2},
  "email2@test.com": {1}
]

如何使用Java8流媒体实现这一点?

utugiqy6

utugiqy61#

尝试以下操作:

import static java.util.stream.Collectors.*;
import org.apache.commons.lang3.tuple.Pair;
// ...snip...
sections.stream()
        .flatMap(section->section.getGrids()
                                 .stream()
                                 .map(Grid::getRows)
                                 .flatMap(Set::stream)
                                 .map(row->new Pair(row.getEmail(), section.getId())))
        .collect(groupingBy(Pair::getKey, mapping(Pair::getValue, toSet())));
wljmcqd8

wljmcqd82#

如果您使用的是java 9或更高版本,您可以用一种非常简洁的方式完成,而不需要第三方库:

import java.util.Map;
import java.util.Set;

import static java.util.Map.Entry;
import static java.util.Map.entry;
import static java.util.stream.Collectors.groupingBy;
import static java.util.stream.Collectors.mapping;
import static java.util.stream.Collectors.toSet;

Map<String, Set<Long>> result = sections.stream()
        .flatMap(section -> section.getGrids().stream()
                .flatMap(grid -> grid.getRows().stream())
                .map(row -> entry(row.getEmail(), section.getId())))
        .collect(groupingBy(Entry::getKey, mapping(Entry::getValue, toSet())));
laik7k3q

laik7k3q3#

不是流媒体。它可以通过一个简单的foreach实现:

public static void main(String[] args) {
        Set<Section> sections = Set.of(....);
        Map<String, Set<Long>> result = new HashMap<>();
        sections.forEach(section -> 
                                 section.getGrids()
                                         .forEach(grid -> 
                                                          grid.getRows().forEach(row -> {
                                                            if (result.containsKey(row.getEmail()))
                                                                result.get(row.getEmail()).add(section.getId());
                                                                else result.put(row.getEmail(), Set.of(section.getId()));
                                                          })));
    }
ugmeyewa

ugmeyewa4#

这里有两个步骤来获得所需的输出格式。看看它是否适合你的用例

import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import javafx.util.Pair;

public class P5 {

  public static void main(String[] args) {

    Row r1 = new Row(50L, "email1@test.com");
    Row r2 = new Row(51L, "email2@test.com");
    Row r3 = new Row(60L, "email1@test.com");
    Row[] arr1 = {r1, r2};
    Row[] arr2 = {r3};

    Grid[] g1 = {new Grid(10L, new HashSet<>(Arrays.asList(arr1)))};
    Grid[] g2 = {new Grid(11L, new HashSet<>(Arrays.asList(arr2)))};

    Set<Section> sections = new HashSet<>();
    sections.add(new Section(1L, new HashSet<>(Arrays.asList(g1))));
    sections.add(new Section(2L, new HashSet<>(Arrays.asList(g2))));

    Map<Long, List<String>> minify = sections.stream()
        .collect(Collectors.toMap(section -> section.id,
            section -> section.grids.stream().flatMap(grid -> grid.rows.stream())
                .map(row -> row.email)
                .collect(Collectors.toList())));

    Map<String, Set<Long>> result = minify.keySet().stream()
        .flatMap(key -> minify.get(key).stream().map(email -> new Pair<>(
            key, email))).collect(Collectors.groupingBy(
            Pair::getValue, Collectors.mapping(Pair::getKey, Collectors.toSet())));

    System.out.println(result);
  }

  static class Section {
    private Long id;
    private Set<Grid> grids;
    public Section(Long id, Set<Grid> grids) {
      this.id = id;
      this.grids = grids;
    }
  }

  static class Grid {
    private Long id;
    private Set<Row> rows;
    public Grid(Long id, Set<Row> rows) {
      this.id = id;
      this.rows = rows;
    }
  }

  static class Row {
    private Long id;
    private String email;
    public Row(Long id, String email) {
      this.id = id;
      this.email = email;
    }
  }

}

结果

{email2@test.com=[1], email1@test.com=[1, 2]}

相关问题