英文:
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<Product> productsListOne;
List<Products> 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 -> o.getId() == p.getId()).findFirst().isPresent()){
System.out.println("found one");
}
}
答案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 -> {
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 < 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;
}
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论