合并两个Arraylist&;保留Java中的所有值
我有两个合并两个Arraylist&;保留Java中的所有值,java,arraylist,collections,Java,Arraylist,Collections,我有两个arrayList的PresentErrorList和PastErrorList都有3个字段errorCode,presentErrorCount和pasterrocount presentErrorList [errorCode=1000,presentErrorCount=10,pastErrorCount =0] [errorCode=1100,presentErrorCount=2,pastErrorCount =0] [errorCode=1003,presentErrorC
arrayList
的PresentErrorList
和PastErrorList
都有3个字段errorCode
,presentErrorCount
和pasterrocount
presentErrorList
[errorCode=1000,presentErrorCount=10,pastErrorCount =0]
[errorCode=1100,presentErrorCount=2,pastErrorCount =0]
[errorCode=1003,presentErrorCount=0,pastErrorCount =10]
[errorCode=1104,presentErrorCount=0,pastErrorCount =12]
[errorCode=1000,presentErrorCount=0,pastErrorCount =12]
粘贴错误列表
[errorCode=1000,presentErrorCount=10,pastErrorCount =0]
[errorCode=1100,presentErrorCount=2,pastErrorCount =0]
[errorCode=1003,presentErrorCount=0,pastErrorCount =10]
[errorCode=1104,presentErrorCount=0,pastErrorCount =12]
[errorCode=1000,presentErrorCount=0,pastErrorCount =12]
计算当前值时,pastErrorCount=0,反之亦然。我的最终名单应该是
[errorCode=1000,presentErrorCount=10,**pastErrorCount =12**]
[errorCode=1100,presentErrorCount=2,pastErrorCount =0]
[errorCode=1003,presentErrorCount=0,pastErrorCount =10]
[errorCode=1104,presentErrorCount=0,pastErrorCount =12]`
正如您在
errorCode=1000
的最终列表中所看到的,因为它在过去是重复的,所以只有一个对象同时具有当前和过去的错误计数。因此,很难找到一种方法,但最后是一个,这就是我所做的:
public static void main(String[] args) {
List<ErrorCodeModel> presentErrorList = new ArrayList<>();
presentErrorList.add(new ErrorCodeModel("1000", 10, 0));
presentErrorList.add(new ErrorCodeModel("1100", 2, 0));
List<ErrorCodeModel> pastErrorList = new ArrayList<>();
pastErrorList.add(new ErrorCodeModel("1003", 0, 10));
pastErrorList.add(new ErrorCodeModel("1104", 0, 12));
pastErrorList.add(new ErrorCodeModel("1000", 0, 12));
Map<String, ErrorCodeModel> map = Stream.of(presentErrorList, pastErrorList)
.flatMap(Collection::stream)
.collect(Collectors.toMap(ErrorCodeModel::getErrorCode,
Function.identity(),
(oldValue, newValue)
-> new ErrorCodeModel(oldValue.getErrorCode(),
oldValue.getPresentErrorCount()+newValue.getPresentErrorCount(),
oldValue.getPastErrorCount()+newValue.getPastErrorCount())));
List<ErrorCodeModel> errorList = new ArrayList<>(map.values());
errorList.sort((err1, err2) //line 20*
-> Integer.compare(Integer.parseInt(err1.getErrorCode()),
Integer.parseInt(err2.getErrorCode())));
System.out.println(errorList.toString());
//*line 20 : Optionnal, if you want to sort by errorCode
//(need to parse to int to avoir alphabetical order
}
publicstaticvoidmain(字符串[]args){
List presentErrorList=新建ArrayList();
添加(新的ErrorCodeModel(“1000”,10,0));
添加(新的ErrorCodeModel(“1100”,2,0));
List pastErrorList=new ArrayList();
添加(新的ErrorCodeModel(“1003”,0,10));
添加(新的ErrorCodeModel(“1104”,0,12));
添加(新的ErrorCodeModel(“1000”,0,12));
Map Map=Stream.of(presentErrorList,PasterErrorList)
.flatMap(集合::流)
.collect(Collectors.toMap(ErrorCodeModel::getErrorCode),
Function.identity(),
(旧值,新值)
->新的ErrorCodeModel(oldValue.getErrorCode(),
oldValue.getPresentErrorCount()+newValue.getPresentErrorCount(),
oldValue.getPastErrorCount()+newValue.getPastErrorCount());
List errorList=newarraylist(map.values());
errorList.sort((err1,err2)//第20行*
->Integer.compare(Integer.parseInt(err1.getErrorCode()),
整数.parseInt(err2.getErrorCode());
System.out.println(errorList.toString());
//*第20行:Optionnal,如果要按错误代码排序
//(需要解析为int以避免按字母顺序排列
}
因此,在添加元素后,将执行以下操作:
- 完成每2个
列表的流
- 目标是将它们添加到
映射中
:(对象的代码,对象)
- 使用(oldValue,newValue)lambda exp是因为
键始终在映射中,我告诉它添加一个新对象,该对象具有上一个和要添加的新对象之和
- 映射之后,将根据表示所请求的ErrorCodeModel的
值生成
列表
之间的列表的类型是什么?因此你有一个代表w的类帽子在里面,你能给出定义吗?(属性)列表有上面给出的3个属性。Hi@arzon thanx非常适合这个解决方案,但是你能把它转换成Java7吗?我对Java8不太有信心,因为我无法理解这些解决方案。同样,这将极大地帮助thanx alot。Hi@arzon thnx buddy我明白了转换的方法。流是Java8的一个功能,在Java7中是可能的,但在或者做得不好,所以我不会,因为我已经回答了,你可能会接受随着功能的发展而发展,而不是停留在过去,我认为没有理由对Java8不“自信”…^^^^是的,正如你在java7中所说的,它很凌乱,我没有其他选择,因为我们的整个产品代码都只在java7中。所以我必须处理它。