gpt4 book ai didi

java - 如何迭代Java列表数组将对象键值对保存到它自己的数组中

转载 作者:行者123 更新时间:2023-12-02 09:11:39 25 4
gpt4 key购买 nike

我想看看是否可以采用 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

如有任何建议或帮助,我们将不胜感激,谢谢!

最佳答案

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

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);

关于java - 如何迭代Java列表数组将对象键值对保存到它自己的数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59362849/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com