D
D
dc65k2021-06-23 21:11:15
Java
dc65k, 2021-06-23 21:11:15

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"
    }
]


And List 2 (JSON representation):
[
    {
        "id": "1",
        "title": "one"
    },
    {
        "id": "10",
        "title": "ten"
    }
]


Result:
[
    {
        "id": "2",
        "title": "two"
    },
    {
        "id": "3",
        "title": "three"
    }
]


If I were to solve this problem in JavaScript, one solution would be:
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' } ]

 */


By analogy, I solve this problem in Java using the Stream API. The question is in the implementation of the function some?
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 answer(s)
D
Dmitry Roo, 2021-06-23
@dc65k

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 question

Ask a Question

731 491 924 answers to any question