在Java的ArrayList中查找对象并修改其值。

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

Finding an object in Java ArrayList and changing its value

问题

我有两个列表:

List<Product> productsListOne;
List<Products> productsListTwo;

第一个列表包含所有产品,第二个列表包含一些产品(其中一些产品具有相同的ID),但所属类别不同。
我的目标是查找在productsListOne中是否存在来自productsListTwo的产品ID。任务的第二部分是,如果找到这样的产品,需要将productsListOne中的产品类别更改为与productsListTwo中的产品类别相同。
我尝试过一些使用for each循环的解决方案,但并没有取得很大的成功。

for (Product secondListProduct : productsListTwo) {
    for (Product firstListProduct : productsListOne) {
        if (productsListTwo.get(productsListTwo.indexOf(secondListProduct)).getId() == 
            firstListProduct.get(firstListProduct.indexOf(firstListProduct)).getId()) {
            firstListProduct.get(firstListProduct.indexOf(firstListProduct)).setCategory(
                productsListTwo.get(productsListTwo.indexOf(secondListProduct)).getCategory());
        }
    }
}

第二次尝试是使用流:

for (Product p : secondListProduct) {
    if (firstListProduct.stream().filter(o -> o.getId() == p.getId()).findFirst().isPresent()) {
        System.out.println("找到一个");
    }
}
英文:

I have two lists:

List&lt;Product&gt; productsListOne;
List&lt;Products&gt; productsListTwo;

First list contains all products and the second one contains some products(some of this products have the same id), but with differen category.
My goal is to find if in productsListOne there is id of product from productsListTwo. Second part of task is, if it finds such product it needs to change productsListOne product category to same category from product from productsListTwo.
I`ve tried some solutions with for each loops but withous bigger success.

       for (Product secondListProduct: productsListTwo) {
            for (Product firstListProduct: productsListOne) {
                if (productsListTwo.get(productsListTwo.indexOf(secondListProduct)).getId() == firstListProduct.get(firstListProduct.indexOf(firstListProduct)).getId()){
                    firstListProduct.get(firstListProduct.indexOf(firstListProduct)).setCategory(productsListTwo.get(productsListTwo.indexOf(secondListProduct)).getCategory());
                }
            }
        }

Second attemp was with streams:

        for (Product p:secondListProduct) {
            if(firstListProduct.stream().filter(o -&gt; o.getId() == p.getId()).findFirst().isPresent()){
                System.out.println(&quot;found one&quot;);
            }
        }

答案1

得分: 1

以下是翻译好的内容:

你可以尝试以下两种解决方案(使用/不使用流)。这两种解决方案都假定第一个列表中可能存在多个具有相同 id 的项。如果不是这种情况,那么你可以通过在内部 if 语句的末尾添加 break 来轻松解决它,使用双重循环。

使用流:

for (Product product : productsListTwo) {
  productsListOne = productsListOne.stream()
    .map(p -> {
      if (p.getId() == product.getId()) {
        p.setCategory(product.getCategory());
        return p;
      }
      return p;
    })
    .collect(Collectors.toList());
}

不使用流:

for (Product product : productsListTwo) {
  for (int i = 0; i < productsListOne.size(); i++) {
    Product listOneProduct = productsListOne.get(i);
    if (listOneProduct.getId() == product.getId()) {
      listOneProduct.setCategory(product.getCategory());
      break; // 如果每个 id 在列表中只出现一次 - 否则移除
    }
  }
}
英文:

You can try one of the following two solutions (with/without streams). Both solutions make the assessment that more than one items with the same id can exist in the first list. If that is not the case, then you can solve it easily with the double for loop by adding a break in the end of the inner if statement.

With stream:

for (Product product : productsListTwo) {
  productsListOne = productsListOne.stream()
		.map(p -&gt; {
			if (p.getId() == product.getId()) {
				p.setCategory(product.getCategory());
				return p;
			}
			return p;
		})
    .collect(Collectors.toList());
}

Without stream:

for(Product product: productsListTwo) {
  for (int i = 0; i &lt; productsListOne.size(); i++) {
    Product listOneProduct = productsListOne.get(i);
    if (listOneProduct.getId() == product.getId()) {
      listOneProduct.setCategory(product.getCategory());
      break; // if each id exists only one time in the list - otherwise remove
    }
  }
}

答案2

得分: 0

对于终极产品产品在productsListOne中{
   对于终极产品产品在productsListTwo中{
      如果产品.id == 产品.id{
         产品.category = 产品.category;
         中断;
      }
   }
}
英文:
for (final Product product in productsListOne) {
   for (final Products products in productsListTwo) {
      if (product.id == products.id) {
         product.category = products.category;
         break;
      }
   }
}

huangapple
  • 本文由 发表于 2020年9月15日 18:43:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/63900185.html
匿名

发表评论

匿名网友

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

确定