Java 对两个对象进行任何匹配的过滤流将一个对象的值设置为另一个对象的值

Java 对两个对象进行任何匹配的过滤流将一个对象的值设置为另一个对象的值,java,spring-boot,java-stream,Java,Spring Boot,Java Stream,长期读者/暗处潜伏者,第一次海报。 这可能是一个有点愚蠢的问题,我相信有一个简单的解决办法 我有一份客户名单和一份供应商名单 顾客: private int-ID; 私有字符串名称; 私人整数年龄; 私有LocalDateTime createdDate; 私有字符串附加名; 供应商 private int-ID; 私有字符串名称; 私人整数年龄; 私人int客户ID; 私有LocalDateTime createdDate; 我正在获取以下虚拟信息: List suppliersList=

长期读者/暗处潜伏者,第一次海报。 这可能是一个有点愚蠢的问题,我相信有一个简单的解决办法

我有一份客户名单和一份供应商名单

顾客:

private int-ID;
私有字符串名称;
私人整数年龄;
私有LocalDateTime createdDate;
私有字符串附加名;
供应商

private int-ID;
私有字符串名称;
私人整数年龄;
私人int客户ID;
私有LocalDateTime createdDate;
我正在获取以下虚拟信息:

List suppliersList=supplierRepository.findAll();
List customersList=customerRepository.findAll();
这让我想起了这样的事情:

Suppliers List:
==============================================================================================
  Supplier{ID=1, name='Supplier A', age=19, customerID=1, createdDate=2020-09-25T23:15:31.798}
, Supplier{ID=2, name='Supplier B', age=20, customerID=1, createdDate=2020-09-25T23:15:31.799}
, Supplier{ID=3, name='Supplier C', age=21, customerID=2, createdDate=2020-09-25T23:15:31.799}
, Supplier{ID=4, name='Supplier D', age=22, customerID=3, createdDate=2020-09-25T23:15:31.799}
==============================================================================================
Customers List:
  Customer{ID=1, name='Customer A', age=19, createdDate=2020-09-25T23:15:31.770}
, Customer{ID=2, name='Customer B', age=20, createdDate=2020-09-25T23:15:31.779}
, Customer{ID=3, name='Customer C', age=21, createdDate=2020-09-25T23:15:31.779}
, Customer{ID=4, name='Customer D', age=22, createdDate=2020-09-25T23:15:31.779}
==============================================================================================
基本上,我想做的是,如果供应商中的customerID与客户中的ID匹配,我想将客户中的supplierName设置为供应商的name值

我已经有一个流正在为我查找匹配项

List filteredList=customersList.stream()
.filter(客户->供应商列表.stream()
.anyMatch(supl->String.valueOf(cust.getID())
.equals(String.valueOf(supl.getCustomerID()))
).collect(Collectors.toList());
这为我提供了3个匹配ID的以下输出

  Customer{ID=1, name='Customer A', age=19, createdDate=2020-09-25T23:22:16.768}
, Customer{ID=2, name='Customer B', age=20, createdDate=2020-09-25T23:22:16.776}
, Customer{ID=3, name='Customer C', age=21, createdDate=2020-09-25T23:22:16.777}
长话短说,我试图实现的是,对于所有这些匹配,我希望将Customer supplierName设置为Supplier中的name值


在某一点上,我还想加入另一个对象流(因此我们现在有3个流),并从一个对象到另一个对象设置一个或多个值。如果有人对此有一个解决方案,那么就可以获得额外的积分:)

我认为,如果没有纯流解决方案,完成这项任务会更好。在这里,按步骤分开:

Map<Integer, List<Supplier>> suppliersByCustomerId = suppliersList.stream()
        .collect(Collectors.groupingBy(Supplier::getCustomerID));
Map suppliersByCustomerId=suppliersList.stream()
.collect(collector.groupingBy(Supplier::getCustomerID));
这将按customerID对供应商进行分组。然后,我们需要迭代客户并检查每个客户是否有供应商。如果匹配,我们将第一个供应商的名称设置为客户

customers.forEach(c -> {
    List<Supplier> suppliers = suppliersByCustomerId.get(c.getID());
    if (suppliers != null && !suppliers.isEmpty()) {
        Supplier first = suppliers.get(0);
        String suppliername = first.getName();
        c.setSupplierName(supplierName);
    }
});
customers.forEach(c->{
List suppliers=suppliersByCustomerId.get(c.getID());
if(suppliers!=null&&!suppliers.isEmpty()){
供应商优先=供应商。获取(0);
字符串suppliername=first.getName();
c、 设置供应商名称(供应商名称);
}
});

由于您没有指定如果每个客户有多个供应商该怎么办,因此我决定将第一个供应商的名称设置为该客户。

最好直接从数据库中获取合并数据,这取决于关系。不过,如果您在获取后需要解决方案,您可以创建customerId supplierName迭代供应商列表的映射,并迭代客户列表,并设置从map-O(n)获取的供应商名称。从您的示例中,我看到一个客户可能有多个供应商。在这种情况下会发生什么?应该为客户设置哪个供应商名称?嗨,Eklavya,是的,理想情况下我会在取货后再做。我正在做的是,有一个非常低效的查询,它将大量的表连接到一个庞大的对象中。我想将其拆分成多个较小的查询,将较少的表连接成较小的对象,然后将这些对象合并成一个。因此,如果从客户和供应商的例子开始,我可以想出该怎么做,我希望我可以将同样的事情应用到大型查询中。嗨,fps,好问题。我可能应该看看如何改进这个例子。对于解决方案,我们可以安全地假设将存在一对一映射。第二组(供应商)中可能有多个ID,如示例中所示,但不太可能。您好,fps,这就像一个符咒,同意将其拆分会更有意义。到目前为止,我对streams还是一个新手,他们让我很困惑,我还有很多东西要学。对于我来说,使用多个流和对象列表来实现这一点也要容易得多。非常感谢:)您可以直接收集名字作为映射键,如
…collect(Collectors.toMap(Supplier::getCustomerID,Supplier::getName,(a,b)->a))
很酷,谢谢Eklavya,我们也将对此进行研究
customers.forEach(c -> {
    List<Supplier> suppliers = suppliersByCustomerId.get(c.getID());
    if (suppliers != null && !suppliers.isEmpty()) {
        Supplier first = suppliers.get(0);
        String suppliername = first.getName();
        c.setSupplierName(supplierName);
    }
});