java - 比较 List<Map<String, String>> 和 List<Map<String, Object>> Java

标签 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 循环也适用于列表比较,但我不确定如何比较 user_idlistUserlistIdList<Map<String, Object>>List<String> .

作为一种尝试和伪代码的观点,我想要完成的是:

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_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 来完成此操作但这会修改​​现有 map

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

关于java - 比较 List<Map<String, String>> 和 List<Map<String, Object>> Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58116492/

相关文章:

java - 使用 JaCoCO 时测试失败

c - 将节点插入 Dllist

java - For循环在第二次后不执行第一条命令

c - 如何退出 C 中的外循环(不使用++)?

java - 回显 jpassword 字符一次然后隐藏它

java - Weka模型在android中读取错误

java - 复杂搜索查询 JPA

Python __init__ setattr 关于参数?

C# 没有指定大小的数组

c - 反转字符串 - 循环不执行