package com.hcz.db;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
public class DbTest {
public static void main(String[] args) {
List<Object> obj = getList();//模拟代码sql查询出的集合
List<Object> result = dealObjectList(obj);
}
/**
* 将转换后的list传递给该方法,进行list中对象的合并
* @param obj
* @return
*/
private static List<Object> dealObjectList(List<Object> obj) {
List<Object> result = new ArrayList<Object>();//合并后保存对象的集合
List<Integer> useFlag = new ArrayList<Integer>();
try {
for(int i = 0 ;i < obj.size();i++){//这里还有一点就是已经比较过的对象不应该进行再次比较
if(useFlag.contains(i)){//在用过的集合中包含,那么该下标对象将不可参与对象的比较,因为已经使用过了
continue;
}
Object[] objOne = (Object[])obj.get(i);//取集合第一个对象
if(objOne == null){
continue;
}
Object[] newObj = null;//保存每次比较合并后的结果
boolean flag = true;
for(int j = i+1 ;j < obj.size();j++){
Object[] objSec = (Object[])obj.get(j);//去集合第二个对象
//这里进行对象比较
flag = objCompare(objOne,objSec);
if(!flag){//默认对象buil_id相同,不相同跳出这层比较
continue;
}
//此时找到了相同的,下次该对象即不可参与比较
useFlag.add(j);
//这里进行对象的合并
if(newObj == null){
newObj = objComTogether(objOne,objSec);
}else{
newObj = objComTogether(newObj,objSec);
}
}
if(newObj == null){//某个对象跟后面的对象都比较完毕,没有发现和它一样的
Object[] objAnother = (Object[])obj.get(i);
result.add(objAnother);;
continue;
}
//这里进行合并后对象的添加
result.add(newObj);
}
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
/**
* 针对buil_id相同的数组对象进行字段值的合并
* @param objOne
* @param objSec
* @return
*/
private static Object[] objComTogether(Object[] objOne, Object[] objSec) {
Object[] newObj = objOne;
try {
for(int i = 0 ;i < objOne.length;i++){
if(objOne[i] != null && objOne[i] instanceof String){
if(objOne[i] != null && objOne[i].equals(objSec[i])){
newObj[i] = objOne[i];
}else{
newObj[i] = objOne[i] + "," + objSec[i];
}
}
if(objOne[i] != null && objOne[i] instanceof BigDecimal){
BigDecimal temp = (BigDecimal)objOne[i];
BigDecimal temp2 = (BigDecimal)objSec[i];
if(temp != null && temp2 != null && temp.floatValue() == temp2.floatValue()){
newObj[i] = temp.floatValue();
}else {
newObj[i] = temp.floatValue()+ "," + temp2.floatValue();
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return newObj;
}
/**
* 进行两个数组对象比较,
* @param objOne
* @param objSec
* @return false 数组对象不相同;true 数组对象相同,可进行字段值合并
*/
private static boolean objCompare(Object[] objOne, Object[] objSec) {
boolean flag = false;
try {
if(objOne == null || objOne.length == 0){
return false;
}
if(objSec == null || objSec.length == 0){
return false;
}
if(objOne[0] == null || objSec[0]== null){
return false;
}
if(objOne[0] instanceof String ){
String tempSec = (String)objSec[0] ;
String tempOne = (String)objOne[0];
if(tempOne != null && tempOne.equals(tempSec)){
flag = true;
}
}
} catch (Exception e) {
e.printStackTrace();
}
return flag;
}
public static List<Object> getList (){
Object[] tempone = new Object[13];
tempone[0] = "110-0173";
tempone[1] = "rf173";
tempone[2] = "新华联控股有限公司";
tempone[3] = "北京华源西路人防工程技术服务建设项目名称建设项目名称建设项目名称审查单位人防工程基本信息审查单位人防工程基本信息楼号,北京华源西路人防工程技术服务中心";
tempone[4] = "通州区台湖镇外郎营村";
tempone[5] = 345.45f;
tempone[6] = 443.00f;
tempone[7] ="1";
tempone[8] ="1";
tempone[9] = "乙";
tempone[10]= "库房,社区指挥所";
tempone[11]= "北京华源西路人防工程技术服务中心";
tempone[12]= "2014-02-28";
Object[] tempSec = new Object[13];
tempSec[0] = "110-0173";
tempSec[1] = "rf173";
tempSec[2] = "新华联控股有限公司";
tempSec[3] = "北京华源西路人防工程技术服务建设项目名称建设项目名称建设项目名称审查单位人防工程基本信息审查单位人防工程基本信息楼号,北京华源西路人防工程技术服务中心";
tempSec[4] = "通州区台湖镇外郎营村";
tempSec[5] = 345.45f;
tempSec[6] = 100.00f;
tempSec[7] ="2";
tempSec[8] ="1";
tempSec[9] = "丙";
tempSec[10]= "自行车库";
tempSec[11]= "北京华源西路人防工程技术服务中心";
tempSec[12]= "2014-02-28";
Object[] tempThr = new Object[13];
tempThr[0] = "110-0112";
tempThr[1] = "rf116";
tempThr[2] = "北京颖泰嘉和生物科技有限公司";
tempThr[3] = "研发中心科研办公楼";
tempThr[4] = "昌平区回龙观";
tempThr[5] = 345.45f;
tempThr[6] = 100.00f;
tempThr[7] ="2";
tempThr[8] ="1";
tempThr[9] = "丙";
tempThr[10]= "自行车库";
tempThr[11]= "北京华源西路人防工程技术服务中心";
tempThr[12]= "2014-02-28";
Object[] tempFor = new Object[13];
tempFor[0] = "110-0173";
tempFor[1] = "rf173";
tempFor[2] = "新华联控股有限公司";
tempFor[3] = "北京华源西路人防工程技术服务建设项目名称建设项目名称建设项目名称审查单位人防工程基本信息审查单位人防工程基本信息楼号,北京华源西路人防工程技术服务中心";
tempFor[4] = "通州区台湖镇外郎营村";
tempFor[5] = 355.45f;
tempFor[6] = 120.00f;
tempFor[7] ="3";
tempFor[8] ="1";
tempFor[9] = "甲";
tempFor[10]= "自行车库2";
tempFor[11]= "北京华源西路人防工程技术服务中心";
tempFor[12]= "2014-02-28";
ArrayList<Object> obj = new ArrayList<Object>();
obj.add(tempone);
obj.add(tempSec);
obj.add(tempThr);
obj.add(tempFor);
return obj;
}
}
对象数组集合中对象数组比较合并
最新推荐文章于 2024-01-02 09:48:47 发布