简体   繁体   English

如何比较具有不同名称引用但具有相同实际数据的两个列表

[英]How to compare two lists with different name reference but with same actual data

I have 2 lists which got the same content but with different name reference. 我有2个列表,它们具有相同的内容但具有不同的名称参考。

I have a table which I download with a button 'Export' which download a CSV file to my local file system and I am getting table using selenium I have tried to convert those lists into 'Collections' but that did not do the trick. 我有一个表,我下载了一个按钮'导出',它将CSV文件下载到我的本地文件系统,我正在使用selenium获取表格我试图将这些列表转换为'集合',但这并没有成功。

This is how my CSV list looks like: 这就是我的CSV列表的样子:

AuditCsvRow{dateStr='"2019-04-14 11:48"', userStr='"admin"', entityStr='"Users"', actionStr='"Login"', nameStr='"admin"', descriptionStr='"User login successful: admin"', clientIpStr='"192.168.51.35"'}

This is the list downloaded using selenium: 这是使用selenium下载的列表:

AuditRow{dateStr='2019-04-14 11:48', userStr='admin', entityStr='Users', actionStr='Login', nameStr='admin', descriptionStr='User login successful: admin', clientIpStr='192.168.51.35', clientIpTxt='192.168.51.35'}
List<AuditRow> uiRowList = new ArrayList<>();
        List<AuditCsvRow>CSVrows = readAuditLogFromCSV(CSV_FILE_PATH);
        uiRowList = auditPage.getAudittable();
 Collection listOne = new ArrayList(Arrays.asList(CSVrows));
        Collection listTwo = new ArrayList(Arrays.asList(uiRowList));

        listOne.equals(listTwo);
private List<AuditRow> readAuditLogFromCSV(String fileName) throws IOException {
        List<AuditRow> rowsList = new ArrayList<>();
        Path pathToFile = Paths.get(fileName);

        try(BufferedReader br = Files.newBufferedReader(pathToFile, StandardCharsets.US_ASCII)){
            String line = br.readLine();
            while(line != null){
                String[] attributes = line.split(",");
                AuditRow auditCsvRow = auditPage.createCsvRow(attributes);
                rowsList.add(auditCsvRow);
                line = br.readLine();
            }
        }
        return rowsList;
    }

The answer of @Erwin Bolwidt is very good, it helps you to reduce duplicated code. @Erwin Bolwidt的答案非常好,它可以帮助您减少重复的代码。 However, I would like to share my trick to compare objects like that. 但是,我想分享我的技巧来比较这样的对象。

Basically, I would parse them into Json and compare the strings 基本上,我会将它们解析为Json并比较字符串

Gson gson = new Gson();
boolean isTheSameData = gson.toJson(CSVrows).equals(gson.toJson(uiRowList));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM