【问题标题】:How to iterate through a Java list array save objects key value pair to its own array如何遍历Java列表数组将对象键值对保存到自己的数组中
【发布时间】:2019-12-16 19:05:41
【问题描述】:

我正在寻找是否可以获取 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

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

【问题讨论】:

    标签: java arrays list jpa


    【解决方案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);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-10-02
      • 2017-07-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-22
      • 2022-11-17
      • 1970-01-01
      相关资源
      最近更新 更多