将List <Map <String,String >>与List <Map <String,Object >> Java比较

时间:2019-09-26 12:07:29

标签: java list for-loop arraylist compare

我有一个接受List<String>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;
}

我想做的是比较这两个并返回一个新的List<Map<String, Object>>

我想做的比较作为示例:


让我们说:

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": "email1@foo.com"
      },
      {
        "USER_ID": "8002",
        "USER_NAME": "username2",
        "USER_MAIL": "email2@foo.com"
      },
      {
        "USER_ID": "8003",
        "USER_NAME": "username3",
        "USER_MAIL": "email3@foo.com"
      },
      {
        "USER_ID": "8004",
        "USER_NAME": "username4",
        "USER_MAIL": "email4@foo.com"
      },
      {
        "USER_ID": "8005",
        "USER_NAME": "username5",
        "USER_MAIL": "email5@foo.com"
      },
      {
        "USER_ID": "8006",
        "USER_NAME": "username6",
        "USER_MAIL": "email6@foo.com"
      },
      {
        "USER_ID": "8007",
        "USER_NAME": "username7",
        "USER_MAIL": "email7@foo.com"
      }
    ]

我想返回一个新的过滤后的List<Map<String, Object>>,其中包含listUser行,其中listUser USER_ID位于listId中(即:)

List<Map<String, Object>> filteredListUser =
    [
      {
        "USER_ID": "8002",
        "USER_NAME": "username2",
        "USER_MAIL": "email2@foo.com"
      },
      {
        "USER_ID": "8004",
        "USER_NAME": "username4",
        "USER_MAIL": "email4@foo.com"
      },
      {
        "USER_ID": "8006",
        "USER_NAME": "username6",
        "USER_MAIL": "email6@foo.com"
      }
    ]

当我需要将user_id中的listUserlistId进行比较以检查是否需要将行添加到filteredListUser时,就会出现问题。

如果这只是两个字符串数组,我会知道如何做到这一点:

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]);
   }
}

我认为for循环也适用于List比较,但是我不确定如何将user_id中的listUser与{{1 }}和listId

从尝试和伪代码的角度来看,我想要完成的是:

List<Map<String, Object>>

但是我不确定从这儿去哪里-我们将不胜感激!

很抱歉,这是一个非常基本的问题-我是编程新手。预先谢谢你!

1 个答案:

答案 0 :(得分:4)

我将迭代List<Map<String, Object>>并检查USER_ID中是否存在与List<String> listId对应的值。下面是使用java-8流的方法

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

或通过使用简单的for循环

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

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

您也可以使用removeIf来完成此操作,但这会修改​​现有地图

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