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

public class UserType {

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

//Getters and Setters;

}


对于上面提到的列表,我想根据userType过滤掉列表。 userType不是唯一的(可以具有相同的名称,并且不能重复),但是userId,userTypeId和userType的组合是唯一的。

因此,要求是,如果我有userType,比如说“ ASSEMBLE”,那么我需要单独形成一个具有唯一userType的List,并将userId和userTypeId附加为Bean中的List。

例如:
输入格式:

[{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" }]


因此通常这必须形成一个像这样的bean-

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/

10-12 18:05