Link to home
Start Free TrialLog in
Avatar of mkhan900
mkhan900

asked on

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;
          }   });   }}
SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mkhan900
mkhan900

ASKER

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;
          }   });   }}
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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;   }   }  );
      }}
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
objects,
I wasn't able to complete your code, can you help me with it plz.

thanks for the help.

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
EXCELLENT.
IT WORKED
Thanks a lot
:-)