我有一个静态列表对象,其中包含User类型的对象。用户正在使用过滤器列表。我们的想法是在不创建新列表的情况下更新List对象。
public class User implements Entity {
public User() {
}
private String userName;
private String userId;
private List<Filter> filters;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public List<Filter> getFilters() {
return filters;
}
public void setFilters(List<Filter> filters) {
this.filters = filters;
}
}
public class Filter {
private String name;
private FilterValue value;
private String type;
private boolean encrypted;
private boolean readOnly;
private Date createdOn;
private Date modifiedOn;
private boolean valid;
private boolean enabled;
private String description;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public FilterValue getFilterValue() {
return value;
}
public void setFilterValue(FilterValue value) {
this.value = value;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public boolean isEncrypted() {
return encrypted;
}
public void setEncrypted(boolean encrypted) {
this.encrypted = encrypted;
}
public boolean isReadOnly() {
return readOnly;
}
public void setReadOnly(boolean readOnly) {
this.readOnly = readOnly;
}
public Date getCreatedOn() {
return createdOn;
}
public void setCreatedOn(Date createdOn) {
this.createdOn = createdOn;
}
public Date getModifiedOn() {
return modifiedOn;
}
public void setModifiedOn(Date modifiedOn) {
this.modifiedOn = modifiedOn;
}
public boolean isValid() {
return valid;
}
public void setValid(boolean valid) {
this.valid = valid;
}
public boolean isEnabled() {
return enabled;
}
public void setEnabled(boolean enabled) {
this.enabled = enabled;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
我有一种更新用户的方法以及相关的相应过滤器。当我嵌套过滤器列表时,该方法变得非常笨拙。无论如何,我可以轻松地更新过滤器列表元素。
private static List<User> users;
@Override
public void update(User item) {
users.forEach(element -> {
if(element.getUserId() == item.getUserId()){
element.setUserId(item.getUserId());
element.setUserName(item.getUserName());
element.getFilters().forEach(filter -> {
Set<String> filters = item.getFilters().stream().map(f -> f.getName()).collect(Collectors.toSet());
List<Filter> present = item.getFilters().stream().filter(x -> filters.contains(x.getName())).collect(Collectors.toList());
List<Filter> notPresent = item.getFilters().stream().filter(x -> !filters.contains(x.getName())).collect(Collectors.toList());
if(present.isEmpty()){
element.setFilters(item.getFilters());
}else {
element.getFilters().stream().forEach(filt -> {
present.stream().forEach(x -> {
if(filt.getName() == x.getName()){
filt.setName(x.getName());
filt.setFilterValue(x.getFilterValue());
filt.setCreatedOn(x.getCreatedOn());
filt.setModifiedOn(x.getModifiedOn());
filt.setEnabled(x.isEnabled());
filt.setEncrypted(x.isEncrypted());
filt.setReadOnly(x.isReadOnly());
filt.setType(x.getType());
filt.setValid(x.isValid());
filt.setDescription(x.getDescription());
}
});
});
}
if(!notPresent.isEmpty()){
}
});
}
});
}
这看起来非常臃肿。