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

时间:2019-12-16 19:05:41

标签: java arrays list jpa

我正在寻找是否有可能采用对象的Java LIST数组并搜索对象的键值对并将其保存到例如自己的数组中。

带有对象的数组

     [
 {
"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 个答案:

答案 0 :(得分: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);