其他分享
首页 > 其他分享> > list中过滤重复的实体类元素

list中过滤重复的实体类元素

作者:互联网

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;


/**
 * @Title:区域服务类
 * @Description
 * @Version
 */
@Service
@Slf4j
public class AreaServiceImpl implements IAreaService {

    @Resource
    private AreaMapper areaMapper;
    @Resource
    private ISysDictService sysDictService;

    @Override
    public List<Area> selectAllProvince() {
        return areaMapper.selectAllProvince();
    }

    @Override
    public List<Area> selectProvinceCityByParams(AreaSearchVo areaSearchVo) {
        return areaMapper.selectProvincecityByParams(areaSearchVo);
    }

    @Override
    public List<Area> selectAllProvinceByCityId() {
        List<SysDict> allProvinceByCity = sysDictService.getAllProvinceByCity();
        log.info("通过字典表获取的数据:{}", JSON.toJSONString(allProvinceByCity));
        List<Area>  areaList = new ArrayList<Area>();
        if(allProvinceByCity != null){
            allProvinceByCity.stream().forEach(c->{
                //System.out.println("城市id是:"+c.getValue());
                //根据城市id获取省信息 直辖市
                if("110000".equals(c.getValue()) || "120000".equals(c.getValue())
                        || "500000".equals(c.getValue()) || "310000".equals(c.getValue())
                ) {
                    Area centralInfo = areaMapper.selectProvinceNameById(c.getValue());
                    //c.setId(centralInfo.getId());
                    areaList.add(centralInfo);
                }else{ //非直辖市根据城市id获取省信息
                    //获取pid
                    Area cityInfo = areaMapper.selectCityNameById(c.getValue());
                    Area provinceInfo= areaMapper.selectProvinceNameById(cityInfo.getPid() + "");
                    areaList.add(provinceInfo);
                }
            });
        }//if end
        //list中去掉重复
         List endList = removeDuplicate(areaList);
         return endList;
    }

    public static List removeDuplicate(List<Area> list) {
        Map<Integer,String> singleMap = new HashMap<>();
        List<Area> reList = new ArrayList<>();
        for(Area a: list){
            if(!singleMap.containsKey(a.getId())){
                singleMap.put(a.getId(),"");
                reList.add(a);
            }
        }
        list.clear();
        return reList;
    }

}
 

标签:实体类,list,List,getValue,过滤,areaMapper,import,public
来源: https://blog.csdn.net/gb4215287/article/details/122599215