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\filen ame.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(".da t")) System.out.println(f.getNa me());
return true;
} }); }}
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\filen
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(".da
return true;
} }); }}
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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(".da t")
&& (filepath = f.getAbsolutePath()).endsW ith(".dat" )
&& !files.contains(fileName)) {
files.add(fileName);
System.out.println(filepat h); }
return true; } } );
}}
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(".da
&& (filepath = f.getAbsolutePath()).endsW
&& !files.contains(fileName))
files.add(fileName);
System.out.println(filepat
return true; } } );
}}
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
objects,
I wasn't able to complete your code, can you help me with it plz.
thanks for the help.
I wasn't able to complete your code, can you help me with it plz.
thanks for the help.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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.
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
EXCELLENT.
IT WORKED
Thanks a lot
IT WORKED
Thanks a lot
:-)
ASKER
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(".da
return true;
} }); }}