title: Recursively calculating the directory size in Java but excluding duplicates
body: <p>I've written a small function to calculate the size of all the files in a directory. The actual function does a lot more but this example has been used for brevity.</p>
  
  <p>This works and walking the directory recursively is easy enough but I'd like to exclude all the filenames that have already been processed. I'd like to keep track of all the filenames in a <code>List</code> so that before getting the size of a file, I check if it exists in the <code>List</code> and if it does, it should be excluded. I don't want any MD5 checksums or anything. Filenames are good enough for my situation.</p>
  
  <p>Since I can only return one value from a function and Java doesn't allow pass-by-reference, I'm pretty lost as to what is the best way to implement this. Here's my code:</p>
  
  <pre><code>public static Long getFileSize(File dirDirectory) {
      Long lngSize = new Long(0);
  
      for (File filItem : dirDirectory.listFiles()) {
          if (filItem.isDirectory()) {
              lngSize += getFileSize(filItem);
          }
          else {
              //Is a file with the same filename alrwady been calculated
              //then exclude it
              //else
              //include it.
              lngSize += filItem.length();
          }
      }
  
      return lngSize;
  }
  </code></pre>