gpt4 book ai didi

java - 根据对象的属性值过滤列表 - Java/Javascript?(首选流/功能类型的方法)

转载 作者:行者123 更新时间:2023-12-01 19:41:59 27 4
gpt4 key购买 nike

我有一个对象列表。该对象的类型为 Class - UserType。

public class UserType {

private int userId = 0;
private int userTypeId = 0;
private String userType;

//Getters and Setters;

}

对于上面提到的List,我想根据userType过滤掉List。 userType不唯一(可以重名且不能重复),但userId、userTypeId和userType的组合是唯一的。

所以要求是,如果我有 userType,让我们说 - “ASSEMBLE”,那么我需要单独形成一个具有唯一 userType 的列表,并将 userId 和 userTypeId 作为列表附加到 Bean 中。

例如:输入格式:

[{userId: 1, userTypeId: 101, userType: "ASSEMBLE" },
{userId: 1, userTypeId: 102, userType: "ASSEMBLE" },
{userId: 2, userTypeId: 103, userType: "ARCHS" },
{userId: 3, userTypeId: 103, userType: "ARCHS" },
{userId: 4, userTypeId: 104, userType: "BAYLEAF" },
{userId: 4, userTypeId: 105, userType: "BAYLEAF" },
{userId: 5, userTypeId: 106, userType: "CHARSET" }]

预期:根据用户类型过滤结果:

 [{userIds: [1] userTypeIds: [101,102], userType: "ASSEMBLE" },
{userId: [2,3], userTypeId: [103], userType: "ARCHS" },
{userId: [4], userTypeId: [104,105] userType: "BAYLEAF" },
{userId: [5], userTypeId: [106], userType: "CHARSET" }]

所以通常这必须形成一个像 -

public class UserType {

private String userType;
private List userIds = 0;
private List userTypeIds = 0;


//Getters and Setters;

}

如何根据需求过滤这个对象?还可以提供 Javascript 解决方案,以便研究一种更加优化的解决方案。提前致谢。

最佳答案

您可以使用 for 循环和 Map 来完成此操作,如下所示:

import java.util.*;

public class GroupBy {

public static void main(String[] args) {
List<UserType> userTypes = new ArrayList<>();
userTypes.add(new UserType(1, 101, "ASSEMBLE"));
userTypes.add(new UserType(1, 102, "ASSEMBLE"));
userTypes.add(new UserType(2, 103, "ARCHS"));
userTypes.add(new UserType(3, 103, "ARCHS"));
userTypes.add(new UserType(4, 104, "BAYLEAF"));
userTypes.add(new UserType(4, 105, "BAYLEAF"));
userTypes.add(new UserType(5, 106, "CHARSET"));

Map<String, UserTypeGroup> map = new HashMap<>();
for (UserType u : userTypes) {
if (!map.containsKey(u.getUserType())) {
map.put(u.getUserType(), new UserTypeGroup(u.getUserType()));
}
map.get(u.getUserType()).getUserIds().add(u.getUserId());
map.get(u.getUserType()).getUserTypeIds().add(u.getUserTypeId());
}

System.out.println("INPUT:");
System.out.println(userTypes);

System.out.println("\nOUTPUT:");
System.out.println(map.values());
}
}

class UserType {

private int userId;
private int userTypeId;
private String userType;

UserType(int userId, int userTypeId, String userType) {
this.userId = userId;
this.userTypeId = userTypeId;
this.userType = userType;
}

int getUserId() {
return userId;
}

int getUserTypeId() {
return userTypeId;
}

String getUserType() {
return userType;
}

@Override
public String toString()
{
return "{userId: " + userId + ", userTypeId: " + userTypeId +", userType: " + userType + "}\n";
}
}

class UserTypeGroup {

private String userType;
private Set<Integer> userIds = new HashSet<>();
private Set<Integer> userTypeIds = new HashSet<>();

UserTypeGroup(String userType) {
this.userType = userType;
}

Set<Integer> getUserIds() {
return userIds;
}

Set<Integer> getUserTypeIds() {
return userTypeIds;
}

@Override
public String toString()
{
return "{userIds: " + userIds + ", userTypeIds: " + userTypeIds + ", userType: " + userType + "}\n";
}
}

输出将是这样的:

INPUT:
[{userId: 1, userTypeId: 101, userType: ASSEMBLE}
, {userId: 1, userTypeId: 102, userType: ASSEMBLE}
, {userId: 2, userTypeId: 103, userType: ARCHS}
, {userId: 3, userTypeId: 103, userType: ARCHS}
, {userId: 4, userTypeId: 104, userType: BAYLEAF}
, {userId: 4, userTypeId: 105, userType: BAYLEAF}
, {userId: 5, userTypeId: 106, userType: CHARSET}
]

OUTPUT:
[{userIds: [5], userTypeIds: [106], userType: CHARSET}
, {userIds: [1], userTypeIds: [101, 102], userType: ASSEMBLE}
, {userIds: [2, 3], userTypeIds: [103], userType: ARCHS}
, {userIds: [4], userTypeIds: [104, 105], userType: BAYLEAF}
]

关于java - 根据对象的属性值过滤列表 - Java/Javascript?(首选流/功能类型的方法),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55062557/

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