【问题标题】:Grouping a list of main objects by their nested object's id按嵌套对象的 id 对主要对象列表进行分组
【发布时间】:2020-07-13 02:55:09
【问题描述】:

给定以下课程A

public class A {
    private int id; //this field is unique
    private int a_1; //this field ain't unique
    private String a_2;
    private String a_3;
    
    //setters + getters
}

我们可以将list 类型为A 的随机对象分组为它们各自的a_1

Map<String, List<A>> sortedmap = list.stream()
                .collect(Collectors.groupingBy(A::getA_1, Collectors.toList()));

现在给定两个类BC,例如:

public class B{
    private int id; //this field is unique for all entities of type B
    private String b_1;
    private C b_C; //many objects of type B may have a reference to the same object of type C
    
    //setters + getters
}

public class C{
    private int id; //this field is unique for all entities of type C
    private String c_1;
    private D c_D; //many objects of type C may have a reference to the same object of type D
    
    //setters + getters
}

public class D{
    private int id; //this field is unique for all entities of type D
    private String d_1;
    private String d_2;
    
    //setters + getters
}

我们如何通过它们各自的b_C.getC_D().getId() 字段对B 类型的随机对象列表进行排序?

【问题讨论】:

  • 你可以使用Comparator
  • 您的意思是“排序”(与问题标题不匹配)还是“组”?如果分组,答案真的是一样的,但你不能使用方法参考:Collectors.groupingBy(b -&gt; b.getB_C().getC_D().getId(), Collectors.toList())
  • @sprinter 是的排序,我编辑了线程。
  • @saka1029 返回类型是什么?我需要一个 Map> 类型的地图,其中关键是 b.getB_C().getC_D().getId()
  • 您的意思是按b_C.getC_D().getId() 分组并按b_C.getC_D().getId() 对分组列表进行排序?

标签: java collections java-stream


【解决方案1】:

您可以将结果分组到默认排序的 TreeMap 中:

public static void main(String[] args) {
    D d1 = new D();d1.setId(1);
    D d2 = new D();d2.setId(2);
    
    C c1 = new C();c1.setC_D(d1);
    C c2 = new C(); c2.setC_D(d2);
    
    B b1 = new B(); b1.setB_C(c1); b1.setId(1);
    B b2 = new B(); b2.setB_C(c2); b2.setId(2);
    B b3 = new B(); b3.setB_C(c2); b3.setId(3);
    
    Map<String, List<B>> result = Arrays.asList(b1,b2,b3)
            .stream()
            .collect(Collectors.groupingBy(b -> ""+b.getB_C().getC_D().getId(),
                     TreeMap::new, 
                     Collectors.toList()));
        
    System.out.println(result);
}

结果是:

{1=[B [id=1]], 2=[B [id=2], B [id=3]]}

PS。 @javaistaucheineinsel 不错的用户名 ;)

【讨论】:

    【解决方案2】:

    我会添加自定义 getter 来支持这一点(未编译,请原谅拼写错误):

    public class B{
        private int id; //this field is unique for all entities of type B
        private String b_1;
        private C b_C; //many objects of type B may have a reference to the same object of type C
        
        //setters + getters
    
        public int getBCDId() {
            return b_C.getCDId();
        }
    }
    
    public class C{
        private int id; //this field is unique for all entities of type C
        private String c_1;
        private D c_D; //many objects of type C may have a reference to the same object of type D
        
        //setters + getters
    
        public int getCDId() {
            return c_D.getId();
        }
    }
    

    现在您可以根据getBCDId 方法返回的值对B 对象进行排序和/或分组。就像您对 B 自己的属性所做的那样。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-12-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-21
      • 1970-01-01
      相关资源
      最近更新 更多