Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 502
  • Last Modified:

Java Prog to avoid Duplicate String collection

The program below gives me the path of all the files ending with ".dat"
However, is there a way to avoid duplicates. A file can exists in multiple folders, I need the program to display the file only once and skip all the occurences of the same file.
H:\folder\foldername\filename.dat, if file.name.dat is found again it should be skipped.

import java.io.*;
public class DatFilePrinter{
   public static void main(String args[]){
    printDatFiles(new File(args[0]));
   }
   public static void printDatFiles(File f){
      f.listFiles(new FileFilter(){
          public boolean accept(File f){
             if (f.isDirectory()) printDatFiles(f);
             else if (f.getName().endsWith(".dat")) System.out.println(f.getName());
             return true;
          }   });   }}
0
mkhan900
Asked:
mkhan900
  • 5
  • 5
  • 4
8 Solutions
 
CEHJCommented:
The the file names to a Set
0
 
CEHJCommented:
>>The the file names to a Set

should have said

Add the file names to a Set
0
 
objectsCommented:
there isn't with accept directly, you need to return all the files as a List and and then post process that list to return the duplicates.
Storing them in  map keyed on filename would seem the way to go.

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
mkhan900Author Commented:
there was very small change to the prog
import java.io.*;
public class DatFilePrinter{
   public static void main(String args[]){
    printDatFiles(new File(args[0]));
   }
   public static void printDatFiles(File f){
      f.listFiles(new FileFilter(){
          public boolean accept(File f){
             if (f.isDirectory()) printDatFiles(f);
             else if (f.getName().endsWith(".dat")) System.out.println(f.getAbsoluePath());
             return true;
          }   });   }}
0
 
objectsCommented:
something like:

Collection findFile(Map<String, File> dats, File dir)
{
      File[] files = f.listFiles(new FileFilter(){
          public boolean accept(File f){
               return f.getName().endsWith(".dat"))
          }});
      for (File file : files)
      {
          if (file.isDirectory())
          {
              findFiles(dats, file);
          }
          else
          {
              dats.put(file.getName(), file);
          }
     }
     return dats.values();
}
0
 
CEHJCommented:
Just need a small change:

import java.io.*;
import java.util.*;

public class DatFilePrinter {
      static Set files = new HashSet();
      
      public static void main(String args[]){
            printDatFiles(new File(args[0]));
      }

      public static void printDatFiles(File f) {
            f.listFiles(new FileFilter(){
                  public boolean accept(File f) {
                        String fileName = null;
                        if (f.isDirectory()) {
                              printDatFiles(f);
                        }
                        else if ((fileName = f.getName()).endsWith(".dat") && !files.contains(fileName)) {
                              System.out.println(fileName);
                              files.add(fileName);
                        }
                        return true;
                  }
            });  
      }
}
0
 
mkhan900Author Commented:
I made a minor change in the ESLE IF statement. Seems like it working. Can I have your opinion on it.

Appreciate everyone help

import java.io.*;
import java.util.*;
public class DatFilePrinter {
      static Set files = new HashSet();
     public static void main(String args[]){
            printDatFiles(new File(args[0]));      }
      public static void printDatFiles(File f) {
            f.listFiles(new FileFilter(){
                  public boolean accept(File f) {
                        String fileName = null;
                      String filepath = null;
                        if (f.isDirectory()) {    printDatFiles(f);   }
                        else if ((fileName = f.getName()).endsWith(".dat")
      && (filepath = f.getAbsolutePath()).endsWith(".dat")
      && !files.contains(fileName)) {
         files.add(fileName);
          System.out.println(filepath);  }
                        return true;   }   }  );
      }}
0
 
objectsCommented:
doing it like that is not thread safe, and also doesn't maintain what directory the file is actually in.
the approach I posted above handles both these situations (and is simpler)
0
 
mkhan900Author Commented:
objects,
I wasn't able to complete your code, can you help me with it plz.

thanks for the help.

0
 
objectsCommented:
sure, try the following. Lot more flexible, and nicely splits the collection of filenames from the display, allowing you to easily change what you want to do with the list of file names.

import java.io.File;
import java.io.FileFilter;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

public class DatFilePrinter
{
      public static void main(String args[]) {
            Collection<File> dats = findFiles(new HashMap<String, File>(), new File(args[0]));
            for (File file : dats) {
                  System.out.println(file);
            }
      }

      public static Collection<File> findFiles(Map<String, File> dats, File dir)
      {
            File[] files = dir.listFiles(new FileFilter() {
                public boolean accept(File f){
                     return f.isDirectory() || f.getName().endsWith(".dat");
                }});
            for (File file : files) {
                if (file.isDirectory()) {
                    findFiles(dats, file);
                } else {
                    dats.put(file.getName(), file);
                }
           }
           return dats.values();
      }
}
0
 
mkhan900Author Commented:
Objects,
This generated 6errors, it might be becoz of  version 1.4.2, which I use.
Most of these 6errors are becoz of the Hashmap.
Please advice.
Thanks again.
0
 
objectsCommented:
try this

import java.io.File;
import java.io.FileFilter;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class DatFilePrinter
{
      public static void main(String args[]) {
            Collection dats = findFiles(new HashMap(), new File(args[0]));
            Iterator i = dats.iterator();
            while (i.hasNext()) {
                  System.out.println(i.next());
            }
      }

      public static Collection findFiles(Map dats, File dir)
      {
            File[] files = dir.listFiles(new FileFilter() {
                public boolean accept(File f){
                     return f.isDirectory() || f.getName().endsWith(".java");
                }});
            for (int i=0; i<files.length; i++) {
                if (files[i].isDirectory()) {
                    findFiles(dats, files[i]);
                } else {
                    dats.put(files[i].getName(), files[i]);
                }
           }
           return dats.values();
      }
}
0
 
mkhan900Author Commented:
EXCELLENT.
IT WORKED
Thanks a lot
0
 
CEHJCommented:
:-)
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 5
  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now