Comparing a List<Map<String, String>> to a List<Map<String, Object>> Java

Jedo :

I have a method that takes in a List<String> and List<Map<String, Object>>:

public List<Map<String, Object>> filterMethod() {

    List<String> listId = this.getUserIds(); //includes only the user id's
    List<Map<String, Object>> listUser = this.getUserDetails(); // includes user_id, user_name, and user_email
    List<Map<String, Object>> filteredListUser = null;

    return filteredListUser;
}

What I would like to be able to do, is compare these two and return a new List<Map<String, Object>>.

The comparison I'd like to do shown as an example:


Let's say:

List<String> listId = 
    [
        "8000",
        "8002",
        "8004",
        "8006",
        "8010",
        "8012",
        "8014",
        "8016",
        "8018",
        "8020",
        "8022",
        "8024"
    ]
List<Map<String, Object>> listUser =
    [
      {
        "USER_ID": "8001",
        "USER_NAME": "username1",
        "USER_MAIL": "[email protected]"
      },
      {
        "USER_ID": "8002",
        "USER_NAME": "username2",
        "USER_MAIL": "[email protected]"
      },
      {
        "USER_ID": "8003",
        "USER_NAME": "username3",
        "USER_MAIL": "[email protected]"
      },
      {
        "USER_ID": "8004",
        "USER_NAME": "username4",
        "USER_MAIL": "[email protected]"
      },
      {
        "USER_ID": "8005",
        "USER_NAME": "username5",
        "USER_MAIL": "[email protected]"
      },
      {
        "USER_ID": "8006",
        "USER_NAME": "username6",
        "USER_MAIL": "[email protected]"
      },
      {
        "USER_ID": "8007",
        "USER_NAME": "username7",
        "USER_MAIL": "[email protected]"
      }
    ]

I would like to return a new filtered List<Map<String, Object>>, that contains listUser rows where a listUser USER_ID is in listId (i.e:)

List<Map<String, Object>> filteredListUser =
    [
      {
        "USER_ID": "8002",
        "USER_NAME": "username2",
        "USER_MAIL": "[email protected]"
      },
      {
        "USER_ID": "8004",
        "USER_NAME": "username4",
        "USER_MAIL": "[email protected]"
      },
      {
        "USER_ID": "8006",
        "USER_NAME": "username6",
        "USER_MAIL": "[email protected]"
      }
    ]

The issue comes when I need to compare the user_id from listUser with listId in order to check if I need to add the row to filteredListUser.

I would know how to do this if this were simply two string arrays like so:

String[] a = {1, 2, 3, 4, 5, 6, 7};
String[] b = {2, 4, 6, 8, 10};

ArrayList<String> c = new ArrayList<String>();

for (int i = 0; i < a.length; i++) {
        if (Arrays.asList(b).contains(a[i])) {
            c.add(a[i]);
   }
}

I think a for-loop would also be appropriate for the List comparisons, but I am unsure as to how I can compare the user_id in listUser with listId in List<Map<String, Object>> and List<String>.

As an attempt and pseudo-code perspective, what I am trying to accomplish is this:

public List<Map<String, Object>> filterMethod() {

    List<String> listId = this.getUserIds(); //includes only the user id's
    List<Map<String, Object>> listUser = this.getUserDetails(); // includes user_id, user_name, and user_email
    List<Map<String, Object>> filteredListUser = null;

    for (int i = 0; i < listUser.length; i++) {
        if (listId.contains(listUser.USER_ID[i])) {
            filteredListUser.add(listUser[i]);
        }
    }
    return filteredListUser;
}

I'm not entirely sure where to go from here however - would appreciate any help!

Apologies if this is a really rudimentary question - I'm very new to programming. Thank you in advance!

Deadpool :

I would iterate the List<Map<String, Object>> and check corresponding value to USER_ID is present in List<String> listId. below is the approach by using java-8 streams

List<Map<String, Object>> result = listUser.stream()
                                      .filter(m-> listId.contains(m.get("USER_ID")))
                                      .collect(Collectors.toList());

or By using simple for loop

List<Map<String, Object>> filteredListUser = new ArrayList<>();

for(Map<String, Object> m : listUser) {
        if(listId.contains(m.get("USER_ID"))) {
            filteredListUser.add(m);
        }
    }

You can also do it by using removeIf but this modifies the existing map

listUser.removeIf(m->!listId.contains(m.get("USER_ID")));

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=310632&siteId=1