1. 使用字典提取
把数组对象中相同的属性定义位字典的键,
{
“key1”:object,
"key2" : object,
"key3" : List
}
Map
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
infoEntities.add(infoEntity);
ContractListEntity entity =
new ContractListEntity(
info.contractID(),
info.demandType(),
info.consumerID(),
info.consumerName(),
info.consumerTel(),
infoEntities);
dataMap.put(contractID,entity);
}
}
2.数组遍历--该方法前: 相同数据的对象在数组中必须连续在一起
List
if (list == null)
return null;
int z = 1;
for (int i = 0, length = list.size(); i < length; i+=z){
z = 1;
List
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);
}