0

I have a task list, where each task has a user list, and I need to validate if there is a duplicate user in any of the tasks in the user list.

Below is my object that I would like to validate:

{
  "tasks": [
    {
      "id": 1,
      "users": [
        {
          "username": "username1" 
        }
      ]
    },
    {
      "id": 2,
      "users": [
        {
          "username": "username1"
        },
        {
          "username": "username2"
        }
      ]
    }
  ]
}

I don't have a code that meets this solution, I need a boolean value of true or false if there is a duplicate user in my task list.

How can I do this using Java 11?

  • This question is similar enough that you should easily be able to adapt the streams solution: https://stackoverflow.com/a/54793580/421195. Or (same thread): https://stackoverflow.com/a/35001661/421195 – paulsm4 Sep 22 '22 at 22:15
  • This doesn't look complicated. Since you asked for stream solution you will need *something* (since I don't know which JSON library you are using, nor API of corresponding POJOs) like `rootJSON.getTasks().stream().flatMap(task->task.getUsers().stream()).map(user->user.getUserName())`. With this you should get stream containing all users. Now you just want to check if there are duplicates in it. This was already answered in [How to check if exists any duplicate in Java 8 Streams?](https://stackoverflow.com/q/30053487). – Pshemo Sep 22 '22 at 22:28

0 Answers0