Answer the question
In order to leave comments, you need to log in
How to process a scenario in a match task?
Hello everyone, please tell me how to solve the problem.
I have List 1 (JSON representation):
[
{
"id": "1",
"title": "one"
},
{
"id": "2",
"title": "two"
},
{
"id": "3",
"title": "three"
},
{
"id": "10",
"title": "ten"
}
]
[
{
"id": "1",
"title": "one"
},
{
"id": "10",
"title": "ten"
}
]
[
{
"id": "2",
"title": "two"
},
{
"id": "3",
"title": "three"
}
]
const arrayFilter = (a, a2) => {
return a.reduce((accumulator, currentValue) => {
const isMatches = a2.some((element) => {
return element.id === currentValue.id;
});
if (!isMatches) {
accumulator.push(currentValue);
}
return accumulator;
}, []);
}
console.log(arrayFilter(arr1, arr2));
/*
[ { id: '2', title: 'two' }, { id: '3', title: 'three' } ]
*/
package com.example.demo;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
class DataObj {
private String id;
private String title;
public DataObj(String id, String title) {
this.id = id;
this.title = title;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
@Override
public String toString() {
return "DataObj{" +
"id='" + id + '\'' +
", title='" + title + '\'' +
'}';
}
}
public class Test {
public static void main(String[] args) {
List<DataObj> dataObjs1 = new ArrayList<>();
List<DataObj> dataObjs2 = new ArrayList<>();
DataObj dataObj1 = new DataObj("1", "one");
DataObj dataObj2 = new DataObj("2", "two");
DataObj dataObj3 = new DataObj("3", "three");
DataObj dataObj4 = new DataObj("4", "four");
dataObjs1.add(dataObj1);
dataObjs1.add(dataObj2);
dataObjs2.add(dataObj1);
dataObjs2.add(dataObj3);
dataObjs2.add(dataObj2);
dataObjs2.add(dataObj4);
System.out.println(dataObjs1);
System.out.println(dataObjs2);
List<DataObj> result = dataObjs2.stream().reduce((accumulator, currentValue) -> {
System.out.println(currentValue);
/*
const isMatches = a2.some((element) => {
return element.id === currentValue.id;
});
if (!isMatches) {
accumulator.push(currentValue);
}
*/
return accumulator;
}).stream().collect(Collectors.toList());
System.out.println(result);
}
}
Answer the question
In order to leave comments, you need to log in
1. There is a filter in the stream api.
2. You can try something like list1.removeAll(list2)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question