![](/img/trans.png)
[英]How to add Map<String, string> into Map<String, List<Object>> in java
[英]Comparing a List<Map<String, String>> to a List<Map<String, Object>> Java
我有一個接受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"
}
]
當我需要將listUser
中的user_id
與listId
進行比較以檢查是否需要將行添加到filteredListUser
時,問題就出現了。
如果這只是兩個字符串 arrays ,我會知道該怎么做:
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_id
與List<Map<String, Object>>
和List<String>
中的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;
}
我不完全確定 go 從這里到哪里 - 將不勝感激任何幫助!
如果這是一個非常基本的問題,我深表歉意——我對編程很陌生。 先感謝您!
我會迭代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")));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.