从对象的特定属性中分组多个值

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

Group more than one value from specific attributes of an object

问题

这是我现在的代码片段:

  1. Map<String, Map<Integer, Integer>> map = PreencherDados.listarAlunos()
  2. .stream()
  3. .collect(Collectors.groupingBy(
  4. Aluno::getNome,
  5. Collectors.groupingBy(
  6. Aluno::getBimestre,
  7. Collectors.summingInt(aluno -> 1)
  8. )
  9. ));
  10. for (Map.Entry<String, Map<Integer, Integer>> entry : map.entrySet()) {
  11. System.out.print("NOME: " + entry.getKey() + " || BIMESTRE: ");
  12. entry.getValue().forEach((bimestre, count) -> {
  13. System.out.print(bimestre + "=" + count + ", ");
  14. });
  15. System.out.println();
  16. }

这是期望得到的结果:

  1. NOME: JOAO || BIMESTRE: 1=1, 2=2, 3=4, 4=2
  2. NOME: ANA || BIMESTRE: 1=1, 2=1, 3=3
  3. NOME: PEDRO || BIMESTRE: 1=2, 2=5, 3=2, 4=2
  4. NOME: MARIA || BIMESTRE: 3=4, 4=4

如果仔细观察,我希望将列表中的对象转换为它们自身的总和。如果有任何问题,请随时询问!非常感谢。

英文:

I have a little doubt here.

I have a list of a specific object and would like to group the similar data.

Currently my code snippet looks like this:

Important -> This is my populated list PreencherDados.listarAlunos()

  1. Map&lt;String, Map&lt;Integer, List&lt;Aluno&gt;&gt;&gt; map = PreencherDados.listarAlunos()
  2. .stream().collect(
  3. Collectors.groupingBy(Aluno::getNome,
  4. Collectors.groupingBy(Aluno::getBimestre))
  5. );
  6. for (Map.Entry&lt;String, Map&lt;Integer, List&lt;Aluno&gt;&gt;&gt; entry : map.entrySet()) {
  7. System.out.println(&quot; NOME: &quot; + entry.getKey() + &quot; || BIMESTRE: &quot; + entry.getValue());
  8. }

This is the current result:

NOME: JOAO || BIMESTRE: {1=[entidade.Aluno@50], 2=[entidade.Aluno@24, entidade.Aluno@44], 3=[entidade.Aluno@2f, entidade.Aluno@476, entidade.Aluno@476, entidade.Aluno@476], 4=[entidade.Aluno@22, entidade.Aluno@32]}

NOME: ANA || BIMESTRE: {1=[entidade.Aluno@9a], 2=[entidade.Aluno@c7], 3=[entidade.Aluno@45, entidade.Aluno@246, entidade.Aluno@4f]}

NOME: PEDRO || BIMESTRE: {1=[entidade.Aluno@21, entidade.Aluno@18d], 2=[entidade.Aluno@23, entidade.Aluno@5a, entidade.Aluno@68, entidade.Aluno@48, entidade.Aluno@ac], 3=[entidade.Aluno@26, entidade.Aluno@476], 4=[entidade.Aluno@5c, entidade.Aluno@df]}

NOME: MARIA || BIMESTRE: {3=[entidade.Aluno@20, entidade.Aluno@476, entidade.Aluno@476, entidade.Aluno@476], 4=[entidade.Aluno@25, entidade.Aluno@2b, entidade.Aluno@7a, entidade.Aluno@ca]}

The result I would like to get is:

NOME: JOAO || BIMESTRE: 1=1, 2=2, 3=4, 4=2

NOME: ANA || BIMESTRE: 1=1, 2=1, 3=3

NOME: PEDRO || BIMESTRE: 1=2, 2=5, 3=2, 4=2

NOME: MARIA || BIMESTRE: 3=4, 4=4

If you look closely, I would like to transform the objects on the list into a sum of themselves.

I don't know if I'm doing it right, so I would like your help! Thank you very much

答案1

得分: 0

  1. System.out.println(" NOME: " + entry.getKey() + " || BIMESTRE: " +
  2. entry.getValue().entrySet().stream().map( e -> e.getKey() + "=" + e.getValue().size() ).collect(
  3. Collectors.joining(",")));
英文:
  1. System.out.println(&quot; NOME: &quot; + entry.getKey() + &quot; || BIMESTRE: &quot; +
  2. entry.getValue().entrySet().stream().map( e -&gt; e.getKey() + &quot;=&quot; + e.getValue().size() ).collect(
  3. Collectors.joining(&quot;,&quot;)));

huangapple
  • 本文由 发表于 2020年8月31日 07:40:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/63663123.html
匿名

发表评论

匿名网友

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

确定