简体   繁体   中英

How to compare elements of File[] with name of each element in a list of objects

I need to know which files of a directory are not in my list. The code is as following and the problem is that I do not know how to compare them.

*Comparison is based on files names only.

File dir = new File(directory);
File[] files = dir.listFiles(); 

List<Records> myFiles; 
for(int i=0;i<myFiles.size();i++){
            System.err.println(myFiles.get(i).getDetails().getName());
}

As you can see I have aa list with File[] type that has a list of all files in the directory. Also a list of type List that has all record objects.

There's nothing different you need to do other than to lookup every file name from the files array in the myFiles list.

What did you try and what problems were you facing? Here's a quick implementation to get you started anyway.

/**
 * Returns a set of files in the {@code filesInDir} that are not included in the {@code myFiles} list.
 * 
 * <p>
 * Does a case insensitive comparison of file names to confirm presence.
 * 
 * @param filesInDir the files in a given directory
 * @param myFiles my list of files
 * @return files in the {@code filesInDir} that are not included in the {@code myFiles} list
 */
Set<File> filesNotIncludedInMyList(File[] filesInDir, List<Record> myFiles) {
    Set<File> filesNotInMyList = new HashSet<>();
    for (File fileInDirectory : filesInDir) {
        boolean fileInDirectoryIncludedInMyList = false;
        for (Record myFile : myFiles) {
            if (fileInDirectory.getName().equalsIgnoreCase(myFile.getDetails().getName())) {
                fileInDirectoryIncludedInMyList = true;
            }
        }
        if (! fileInDirectoryIncludedInMyList) {
            filesNotInMyList.add(fileInDirectory);
        }
    }

    return filesNotInMyList;
}

Here's a bit more readable version w/o the nested for loop. Of course this does a case-insensitive search (by lower casing all the file names), but you can do a case sensitive one if you wanted.

/**
 * Returns files in the {@code filesInDir} that are not included in the {@code myFiles} list.
 * 
 * <p>
 * Does a case insensitive comparison of file names to confirm presence.
 * 
 * @param filesInDir the files in a given directory
 * @param myFiles my list of files
 * @return files in the {@code filesInDir} that are not included in the {@code myFiles} list
 */
Set<File> filesNotIncludedInMyList(File[] filesInDir, List<Record> myFiles) {
    Set<File> filesNotInMyList = new HashSet<>();
    Set<String> myFileNames = recordFileNames(myFiles);
    for (File fileInDirectory : filesInDir) {
        if (! myFileNames.contains(fileInDirectory.getName().toLowerCase())) {
            filesNotInMyList.add(fileInDirectory);
        }
    }

    return filesNotInMyList;
}

/**
 * Creates a set containing the file names of all the records in the given list of records.
 * 
 * @param myFiles my list of files
 * @return a set containing the file names of all the records in the given list of records
 */
Set<String> recordFileNames(List<Record> myFiles) {
    Set<String> recordFileNames = new HashSet<>();
    for (Record file : myFiles) {
        recordFileNames.add(file.getDetails().getName().toLowerCase());
    }
    return recordFileNames;
}

如代码中所示, List myFiles尚未初始化,因此您必须先初始化此列表,然后才能执行您想要的所有操作...

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