如何循环并比较 Spring Boot 中两个数组之间存在的数据

问题描述 投票:0回答:1

我有两个都有数据的数组,我想要做的是循环遍历每个数组并比较数组之间的数据,一个数组来自 Post 方法,另一个数组具有来自数据库的数据,因此比较必须完成检查数据是否存在于要更新的数据库中,否则应将其作为新记录插入。

下面是我的代码快照

@Autowired
private OrderRepository orderRepository; 


public void process_data(String ordercode, List<Order> order) {
                
List<Order> orderList = new ArrayList<>(); 
List<Order> orderData = orderRepository.findOrder(ordercode);

// here is where I want to compare the data between two array lists of order and orderData
   using a for loop

for(var data : order) {  // here is only one array present how can I add the second array list so that I can compare the data present between the two array lists

}
java spring spring-boot spring-data-jpa spring-data
1个回答
0
投票

这与springboot无关。据我了解,您将从发布请求中获得订单列表,并且您需要检查数据库中是否有订单。您从发布请求中获得的订单列表中的每个订单都应该有一个 ID。因此,您不需要立即从数据库获取订单列表。相反,你可以这样做:

    public void process_data(String ordercode, List<Order> order) {
                
       List<Order> orderList = new ArrayList<>(); 


       for(var data : order) { 
           if(orderRepository.findById(order.getId()) == null) {
              //Oder is not present in DB
           }

       }
   }
© www.soinside.com 2019 - 2024. All rights reserved.