【问题标题】:Comparing a List<Map<String, String>> to a List<Map<String, Object>> Java比较 List<Map<String, String>> 和 List<Map<String, Object>> Java
【发布时间】:2020-01-26 17:08:37
【问题描述】:

我有一个方法接受List&lt;String&gt;List&lt;Map&lt;String, Object&gt;&gt;

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&lt;Map&lt;String, Object&gt;&gt;

我想要做的比较作为示例显示:


假设:

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&lt;Map&lt;String, Object&gt;&gt;,其中包含listUser 行,其中listUser USER_IDlistId 中(即:)

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

当我需要将listUser 中的user_idlistId 进行比较以检查是否需要将行添加到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 比较,但我不确定如何将 listUser 中的 user_idList&lt;Map&lt;String, Object&gt;&gt;List&lt;String&gt; 中的 listId 进行比较。

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

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

我不完全确定从这里去哪里 - 将不胜感激任何帮助!

抱歉,如果这是一个非常初级的问题 - 我对编程很陌生。提前谢谢!

【问题讨论】:

  • 你需要一个比 List、Map、String 和 Object 更好的抽象。
  • User 是一个映射,其中包含 userId 的键和具有 userId、userName 等用户属性的对象的值。?

标签: java list for-loop arraylist compare


【解决方案1】:

我将迭代List&lt;Map&lt;String, Object&gt;&gt; 并检查USER_ID 的对应值是否存在于List&lt;String&gt; 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")));

【讨论】:

    猜你喜欢
    • 2018-09-22
    • 2020-08-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-29
    • 2015-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多