簡體   English   中英

如何將File []的元素與對象列表中每個元素的名稱進行比較

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

我需要知道目錄的哪些文件不在列表中。 代碼如下,問題是我不知道如何比較它們。

*比較僅基於文件名。

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());
}

如您所見,我有一個File []類型的列表,其中包含目錄中所有文件的列表。 還有具有所有記錄對象的List類型的列表。

除了從myFiles列表中的files數組中查找每個文件名之外,您無需執行其他操作。

您嘗試了什么,遇到了什么問題? 無論如何,這是一個快速入門。

/**
 * 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;
}

這是不帶嵌套的for循環的更具可讀性的版本。 當然,這會執行不區分大小寫的搜索(通過小寫所有文件名),但是您可以根據需要執行區分大小寫的搜索。

/**
 * 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尚未初始化,因此您必須先初始化此列表,然后才能執行您想要的所有操作...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM