My question is How can I compare the strings that I have within a arraylist<String>
so that it shows me the ones that are repeated? BUT these strings are irregular, for example: I have the following files in a folder.
I have a method that lifts everything in the folder and loads it into an arraylist.
ArrayList<String> arrayArchivos = new ArrayList<>();
ArrayList<String> Array1 = new ArrayList<>();
public void buscarArchivo(File ruta) {
// Creo el vector que contendra todos los archivos de una ruta especificada.
File[] archivo = ruta.listFiles();
// Evaluo si la carpeta especificada contiene archivos.
if (archivo != null) {
arrayArchivos.clear();
// Recorro el vector el cual tiene almacenado la ruta del archivo a buscar.
for (int i = 0; i < archivo.length; i++) {
// Evaluo si el archivo o la ruta es una carpeta.
if (archivo[i].isDirectory()) {
// Le paso la nueva ruta de la carpeta si se cambia la ruta e busca nuevamente.
buscarArchivo(archivo[i]);
} else {
String file = archivo[i].getName();
// Quitamos la extencion
arrayArchivos.add(file.substring(0, file.length() - 4));
}
}
// Llamamos al metodo evaluarNombre.
evaluarNombre();
}
}
In my method evaluateName () I have:
public void evaluarNombre() {
if (arrayArchivos != null) {
for (String arrayArchivo : arrayArchivos) {
// Quitamos los espacios al principio y al final con trim(), luego mostramos solo letras.
String b = arrayArchivo.replaceAll("[^a-zA-Z ]", "").trim();
// Quitamos los espacios innecesarios para dejar solo un espacio entre letras.
String d = b.replaceAll(" +", " ");
// Añadimos a un nuevo array todos los String filtrados.
Array1.add(d);
}
} else {
System.out.println("No se encontro ningun archivo.");
}
contar(Array1);
}
In the count (Array1)
methodprivate void contar(ArrayList<String> arraStrings) {
// Declaramos un contador.
int c = 0;
if (arraStrings != null) {
// Nos posicionamos en un elemento y recorremos el resto comparando.
for (int i = 0; i < arraStrings.size(); i++) {
for (int j = i + 1; j < arraStrings.size(); j++) {
// Si los nombres de las cadenas son iguales, el contador se ingrementa.
if (arraStrings.get(i).substring(0, arraStrings.get(i).lastIndexOf(" ")).equals(arraStrings.get(j).substring(0, arraStrings.get(j).lastIndexOf(" ")))) {
c++;
}
}
// Evaluo que el contador sea mayor a 1.
if (c > 1) {
System.out.println("El archivo '" + arraStrings.get(i) + "' Se repite " + c);
}
}
} else {
System.out.println("Array vacio");
}
}
The output I get is:
The file 'Maluma Corazon Dexter Remix Global Music' Repeats 2
The file 'Maluma Corazon Dexter Remix Global Music' Repeats 2
The file 'Agapornis Moves the Waist Dexter Remix Global Music' Repeats 2
The file 'Lali Una Mas Dexter Remix Global Music' Repeats 3
The file 'Lali Una Mas Dexter Remix Global Music' Repeats 3
The file 'Charlie Puth Attention David Guetta Vs Dexter Remix Global Music' Repeats 3
As you can see in the image, it marks me files that are not repeated, such as:
Agapornis Moves the Dexter Remix Global Music Waist
Charlie Puth Attention David Guetta Vs Dexter Remix Global Music
However, he tells me. Like the file
Ozuna Ft Zion & Lennox Egoista Dexter Remix (Global Music11) copies
YES is repeated but with the difference that it has the word copy , I would be very helpful your collaboration to be able to identify what I am doing wrong, of course thank you.