數組相同數據的合併(提取)

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);
        }


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章