有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

如何遍历Java列表数组将对象键值对保存到自己的数组

我想看看是否有可能获取对象的Java列表数组,搜索对象的键值对,并将它们保存到自己的数组中

带有对象的数组

     [
 {
"applicationUserSubscriptionUniqueId": 18639,
"createdByName": "2222",
"updatedDate": "2019-12-02T19:17:45.000+0000",
"applicationsSupported": {
    "appSupportedId": 5,
    "supportAreaId": 123,
    "supportAreas": {
        "applicationId": 123,
    },
    "appSupportedName": "app1"
},
"userSubscriptionInformation": {
    "userSubscribedUniqueId": 18638,
},
"reportSubscriptionId": 18638,

 },
 {
"applicationUserSubscriptionUniqueId": 18638,
"createdByName": "2222",
"updatedDate": "2019-12-02T19:17:45.000+0000",
"applicationsSupported": {
    "appSupportedId": 6,
    "supportAreaId": 123,
    "supportAreas": {
        "applicationId": 123,
    },
    "appSupportedName": "app2"
},
"userSubscriptionInformation": {
    "userSubscribedUniqueId": 18638,
},
"reportSubscriptionId": 18638,

},
{
"applicationUserSubscriptionUniqueId": 18637,
"createdByName": "2222",
"updatedDate": "2019-12-02T19:17:45.000+0000",
"applicationsSupported": {
    "appSupportedId": 15,
    "supportAreaId": 123,
    "supportAreas": {
        "applicationId": 123,
    },
    "appSupportedName": "app3"
},
"userSubscriptionInformation": {
    "userSubscribedUniqueId": 18638,
},
"reportSubscriptionId": 18638,

},
]

代码

public List<ApplicationUserSubscription> findEmailTest() {
int appId = 1;
List<ApplicationUserSubscription> myList = 
applicationUserSubscriptionRepository.findUsersEmailSubscribedToApplication(appId);
System.out.println(myList);

假设我想遍历这个数组,并将所有“appSupportedId”保存到它自己的数组中

NewArray[]=myList。appSupportedId

任何建议或帮助都将不胜感激,谢谢


共 (1) 个答案

  1. # 1 楼答案

    假设使用的对象具有以下字段,则可以尝试此操作

    myList.stream()
          .map(userSub -> userSub.getApplicationsSupported())
          .map(app -> app.getAppSupportedId())
          .toArray(Integer[] :: new);
    

    如果是Map,那么你可以试试

    myList.stream()
           .map(userSub -> userSub.get("applicationsSupported"))
           .map(app -> app.get("appSupportedId"))
           .toArray(Integer[] :: new);