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

标签 java loops data-structures

我有一个对象列表。该对象的类型为 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/

相关文章:

Python - 通过 pandas 数据帧迭代并分配和有条件更新日期时间变量

r - 在 R 中使用嵌套 for 循环创建数据框

data-structures - Prolog 中的自定义数据结构语法

c++ - 如何将 8 个字节写入 void * 地址?

java - 生成年份日期的便捷方法

java - Android 中类似 facebook 的 react 按钮

java - 根据用户输入的高度JAVA获取对角线或水平线组合的总组合

java - 如何从 Java jar 文件中读取资源文件?

loops - 迭代器是否返回对项目的引用或 Rust 中项目的值?

c - 我的程序在 C 中实现堆栈有什么问题