简体   繁体   中英

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.

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.

This is how my CSV list looks like:

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:

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. However, I would like to share my trick to compare objects like that.

Basically, I would parse them into Json and compare the strings

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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