集合之间的操作util-在全集allSet中标识出在子集sonSet 出现过的元素
下面介绍一个用于操作集合与集合之间关系工具类,具体的应用场景有许多,目前我只想到如下的应用场景:
1、在全集allSet筛选出在子集中sonSet出现过的元素,并做标识(如可用于前端数据元素的checked判断)
2、在全集allSet筛选出在子集中sonSet出现过的元素,并剔除该元素(如用于批量删除)
本博客只是用于简单模拟,所以数据的构造比较随机,但是,这个工具类的产生,其实正是因为我本人在项目中遇到了相关问题,经过思索,得出的解决方案的某个步骤需要用到此工具类。既然都提到了实际的project,那不妨我就介绍介绍我遇到的问题:
“在开发角色资源的管理中,需要编辑某个角色下的资源,而资源是有层级关系的,此时我的想法自然是:首先load出该角色下的资源列表(可能是操作角色表与角色资源表),假设叫sonSet,得到的sonSet正是该角色本来就拥有的资源列表,但很明显还不够,因为你还得展示系统 所有的资源allSet 并标识出 哪些资源是已经被checked了(即已经存在该角色下了) ”。或许您有更好的想法,但我目前想到的办法即为本篇博客提供的解决方案。
下面是我的资源dto(仅用于模拟而已,实际项目肯定不止那么少字段)ResDto:
package com.debug.springboot.dto;
import java.io.Serializable;
/**
* Created by zhonglinsen on 2017/10/18.
*/
public class ResDto implements Serializable{
private Integer resId;
private String resName;
private Integer checked=0;
public Integer getResId() {
return resId;
}
public void setResId(Integer resId) {
this.resId = resId;
}
public String getResName() {
return resName;
}
public void setResName(String resName) {
this.resName = resName;
}
public Integer getChecked() {
return checked;
}
public void setChecked(Integer checked) {
this.checked = checked;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
ResDto resDto = (ResDto) o;
if (resId != null ? !resId.equals(resDto.resId) : resDto.resId != null) return false;
if (resName != null ? !resName.equals(resDto.resName) : resDto.resName != null) return false;
return checked != null ? checked.equals(resDto.checked) : resDto.checked == null;
}
@Override
public int hashCode() {
int result = resId != null ? resId.hashCode() : 0;
result = 31 * result + (resName != null ? resName.hashCode() : 0);
result = 31 * result + (checked != null ? checked.hashCode() : 0);
return result;
}
public ResDto(Integer resId, String resName) {
this.resId = resId;
this.resName = resName;
}
public ResDto() {
}
@Override
public String toString() {
return "\nResDto{" +
"resId=" + resId +
", resName='" + resName + '\'' +
", checked=" + checked +
'}';
}
}
重头戏当然是接下来的工具类,说明一下:这里用到google的开发工具 com.google.code.gson 版本为 2.6.1
其中,也用到了java8的lambda表达式,当然啦,如果看起来晦涩,我也提供了非lambda表达式的写法,下面是源码,欢迎诸位吐槽:
package com.debug.springboot.utils;
import com.debug.springboot.dto.ResDto;
import com.google.common.base.Predicate;
import com.google.common.collect.Sets;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import java.util.Objects;
import java.util.Set;
/**
* 在全集 allSet 中标识出在 子集sonSet 出现过的元素 checked=1
* Created by zhonglinsen on 2017/10/18.
*/
public class SetUtils {
private static final Logger log= LoggerFactory.getLogger(SetUtils.class);
//所有元素
private static final Set<ResDto> allSet= Sets.newHashSet();
//实际需要的元素
private static final Set<ResDto> sonSet=Sets.newHashSet();
static {
allSet.add(new ResDto(1,"aa"));
allSet.add(new ResDto(2,"bb"));
allSet.add(new ResDto(3,"cc"));
allSet.add(new ResDto(4,"dd"));
allSet.add(new ResDto(5,"ee"));
allSet.add(new ResDto(6,"ff"));
allSet.add(new ResDto(7,"gg"));
sonSet.add(new ResDto(4,"dd"));
sonSet.add(new ResDto(5,"ee"));
}
public static Set<ResDto> filterSet() throws Exception{
Set<ResDto> finalSet=Sets.newHashSet();
ResDto resDto=null;
for (final ResDto dto:allSet){
resDto=new ResDto();
BeanUtils.copyProperties(dto,resDto);
//低于java8的写法
Set<ResDto> filter=Sets.filter(sonSet, new Predicate<ResDto>() {
@Override
public boolean apply(ResDto input) {
return Objects.equals(input.getResId(),dto.getResId());
}
});
//java8的写法:lambda
//Set<ResDto> filter=Sets.filter(sonSet, input -> Objects.equals(input.getResId(),dto.getResId()));
if (filter.size()>0){
resDto.setChecked(1);
}
finalSet.add(resDto);
}
return finalSet;
}
public static void main(String[] args) throws Exception{
log.debug("全集: {}\n子集:{}",allSet,sonSet);
log.debug("处理后的全集: {}",filterSet());
}
}
下面就直接贴出结果了:
就先介绍到这里吧,如果有问题可以加入群讨论:
java开源技术交流:583522159
鏖战八方(开源群):391619659