简体   繁体   中英

How to iterate through a Java list array save objects key value pair to its own array

I am looking to see if it's possible to take a Java LIST array of objects and search the key value pairs of the objects and save them to their own array for example.

Array with objects

     [
 {
"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,

},
]

code

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

Lets say I wanted to iterate through this array and save all of the 'appSupportedId' to its own array

NewArray[] = myList.appSupportedId

Any suggestions or help would be appreciated, thanks!

You can try this assuming you are using an object with the following fields.

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

If it is a Map , then you can try

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM