1、背景
List对象定义形式和现有的值如下所示。
List<Test> testList = new ArrayList<>(); [{ "ISDEL": "", "ATNAM": "Z008_80_PC_4270Y153", "AEDTM": "20230808", "MATNR": "80.PC-4270" }, { "ISDEL": "", "ATNAM": "Z008_80_PC_4270Y153", "AEDTM": "20230808", "MATNR": "80.PC-4270" }, { "ISDEL": "", "ATNAM": "Z008_80_PC_4270Y153", "AEDTM": "20230808", "MATNR": "81.PC-4271" }, { "ISDEL": "", "ATNAM": "Z007_80_PC_4270Y154", "AEDTM": "20230808", "MATNR": "80.PC-4270" }]
2、目的
如果ATNAM和MATNR都一样,就认为list中的这两条记录是一样的。
3、去重
//通过MATNR+ATNAM作为唯一标识去重
List<Test> testListOnly = removeDuplicates(testList); private List<Test> removeDuplicates(List<Test> testList) { Set<String> uniqueIdentifiers = new HashSet<>(); List<Test> result = new ArrayList<>(); for (Test test : testList) { String identifier = test.getMatnr() + test .getAtnam(); if (!uniqueIdentifiers.contains(identifier)) { uniqueIdentifiers.add(identifier); result.add(test); } } return result; }
4、结果
[{ "ISDEL": "", "ATNAM": "Z008_80_PC_4270Y153", "AEDTM": "20230808", "MATNR": "80.PC-4270" }, { "ISDEL": "", "ATNAM": "Z008_80_PC_4270Y153", "AEDTM": "20230808", "MATNR": "81.PC-4271" }, { "ISDEL": "", "ATNAM": "Z007_80_PC_4270Y154", "AEDTM": "20230808", "MATNR": "80.PC-4270" }]
标签:MATNR,多个,PC,ATNAM,AEDTM,List,标识,80 From: https://www.cnblogs.com/doubleflower/p/18034501