数组相同数据的合并(提取)

来源:互联网 发布:导游软件 编辑:程序博客网 时间:2024/04/29 13:24

1. 使用字典提取

把数组对象中相同的属性定义位字典的键,

{

“key1”:object,

"key2" : object,

"key3" : List<object> , //相同数据数组

}

  Map<Integer, ContractListEntity> dataMap = new HashMap<>();


        for(int i = 0, length = list.size(); i < length; i++ ){


//获取当前对象
            ContractListInfo info = list.get(i);   

//获取当前对象的某个属性
            int contractID = info.contractID(); 

//判断对象属性是否在字典中
            boolean bool = dataMap.containsKey(contractID);

//如果存在,进行数据的合并
            if(bool){
                ServerInfoEntity infoEntity =
                        new ServerInfoEntity(
                                info.serverID(),
                                info.serverName(),
                                info.serverTel());
                dataMap.get(contractID).getServerInfos().add(infoEntity);


            }else{//没有--添加到字典中


                ServerInfoEntity infoEntity =
                        new ServerInfoEntity(
                                info.serverID(),
                                info.serverName(),
                                info.serverTel());

                List<ServerInfoEntity> infoEntities = new ArrayList<>();


                infoEntities.add(infoEntity);


                ContractListEntity entity =
                        new ContractListEntity(
                                info.contractID(),
                            info.demandType(),
                            info.consumerID(),
                            info.consumerName(),
                            info.consumerTel(),
                            infoEntities);
                dataMap.put(contractID,entity);
            }
        }



2.数组遍历--该方法前: 相同数据的对象在数组中必须连续在一起

List<ContractListEntity> contractListEntityList = new ArrayList<>();

        if (list == null)
            return null;


        int z = 1;
        for (int i = 0, length = list.size(); i < length; i+=z){


            z = 1;


            List<ServerInfoEntity> serverInfoEntityList = new ArrayList<>();


            for (int y = i; ; y++){
                if(y == length - 1){
                    ContractListInfo info = list.get(y);
                    ServerInfoEntity infoEntity =
                            new ServerInfoEntity(
                                    info.serverID(),
                                    info.serverName(),
                                    info.serverTel());
                    serverInfoEntityList.add(infoEntity);
                    break;
                }
                else{


                    if((list.get(y).contractID() == list.get(y+1).contractID())) {


                        ContractListInfo info = list.get(y);

                        ServerInfoEntity infoEntity =
                                new ServerInfoEntity(
                                        info.serverID(),
                                        info.serverName(),
                                        info.serverTel());

                        serverInfoEntityList.add(infoEntity);

                        z++;


                    }else{


                        ContractListInfo info = list.get(y);

                        ServerInfoEntity infoEntity =
                                new ServerInfoEntity(
                                        info.serverID(),
                                        info.serverName(),
                                        info.serverTel());

                        serverInfoEntityList.add(infoEntity);


                        break;
                    }
                }
            }


            ContractListInfo info = list.get(i);
            ContractListEntity entity = new ContractListEntity(
                    info.contractID(),
                    info.contractCode(),
                    info.consumerID(),
                    info.consumerName(),
                    info.consumerTel(),
                    serverInfoEntityList
            );
            contractListEntityList.add(entity);
        }


原创粉丝点击