java - 根据对象的属性值过滤列表 - Java/Javascript?(首选流/功能类型的方法是首选)
问题描述
我有一个对象列表。对象的类型是 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" }]
预期:结果根据 userType 过滤掉:
[{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 循环来遍历对象列表中的对象。调用 userType 的 getter 方法并检查它是否等于要过滤的单词。如果是这样,请使用 getter 方法将其 userId 和 userTypeId 添加到相关列表中。
编辑:所以在阅读评论后,我进行了更改并使用 Set 来获取所有唯一类型,然后制作了一个 UserList 对象列表。
像这样:
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class Users{
private List<UserType> objectList = new ArrayList<>(); //arraylist containing your objects
private List<UserList> uniqueUserList = new ArrayList<>();
private void filter() {
List<String> userTypeList = new ArrayList<>();
for(UserType obj: objectList){
userTypeList.add(obj.getType()); //get only Types
}
Set<String> uniqueUserTypes = new HashSet(userTypeList); //set only contians unique strings
for(String s: uniqueUserTypes)
{
addToUniqueList(s);
}
}
private void addToUniqueList(String userTypeName){
String filterBy = userTypeName;
UserList listsForUniqueType = new UserList();
listsForUniqueType.setType(filterBy);
for(UserType obj: objectList) {
if(obj.getType().equals(filterBy)){
listsForUniqueType.addToUserId(obj.getId());
listsForUniqueType.addToUserId(obj.getTypeId());
}
}
uniqueUserList.add(listsForUniqueType);
}
public class UserList {
private String userType;
private List<Integer> userIds = new ArrayList<>();
private List<Integer> userTypeIds = new ArrayList<>();
public void setType(String typeName){
userType = typeName;
}
public void addToUserId(int id){
userIds.add(id);
}
public void addToTypeId(int id){
userTypeIds.add(id);
}
}
public class UserType {
private int userId = 0;
private int userTypeId = 0;
private String userType;
//Getters and Setters;
}
}
推荐阅读
- python - 从结果中删除数字;Python3
- java - onLocationResult 没有被调用
- android - 如何获取特定地址的经度和纬度而不是当前地址
- java - 为什么使用 TCP 使用一个不能总是精确的基本计时器,而 Java 中的计时器总是精确的?
- spring-boot - 如何使用 EmbeddedKafkaRule/EmbeddedKafka 设置 Spring Kafka 测试以修复 TopicExistsException 间歇性错误?
- svelte - Sapper 默认模板的汇总配置
- scaling - 为什么我们只在缩放问题的训练数据中使用 fit() 方法?
- python - 如何在同一图上正确绘制个别年份的月度数据
- performance - 带递归的while循环的时间复杂度
- react-native - React Native Firebase Storage - 尝试使用 .putFile() 保存图像时出错