Remove duplicate values in a list and print them

Asked

Viewed 132 times

1

I have a file called arq1.txt and the other arq2.txt.

Values within the arq1:

2
5
1
10
21
7
8
8
3

Values of arq2:

1,2,3

In this case, my job is to compare the values present in the 2 files and create a new file making the text written in it the value of arq1 - arq2.

In this case, it would remove the values 1, 2 and 3 of the arq1, being as follows (in case the code would create a 3° file and store this value in it):

arq3.txt:

5
10
21
7
8
8

So far I’ve achieved this:

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
 
public class App {
 
  public static void main(final String[] args) throws IOException {
    final List<String> linhasA = Files.readAllLines(Paths.get("C:\\q\\arq1.txt"));
    final List<String> linhasB = Files.readAllLines(Paths.get("C:\\q\\arq2.txt"));
    
    linhasB.forEach(linhaB -> {
       linhasA.forEach(linhaA -> {
           final String []valoresLinhaB = linhaB.split("\\,");
           final String []valoresLinhaA = linhaA.split("\\,");
    
           final List<String> duplicados = new ArrayList<>(Arrays.asList(valoresLinhaB));
           duplicados.retainAll(Arrays.asList(valoresLinhaA));
    
           if(duplicados.size() > 0){
               
             System.out.println(duplicados);
              }

        


         });
        });
    }
}

Thus the value returned is:

[2]
[1]
[3]

That in the case the code basically made the comparison between the 2 files and verified the duplicate data.

Now I have a fucking huge difficulty in making it be printed on the screen the value of arq1 removing duplicate values.

I tried to make some comparisons but they didn’t work (I’m new in java), something like:

for(int i=0; i<10; i++){
if(valoresLinhaA[i] != duplicados){
   System.out.println(valoreslinhaA[i]);
 }
}

But the big mistake is that the valoresLinhaA[i] is a string and has no way to compare that way (at least the IDE tells me so).

Some other way to compare the values and print only the value without duplication?

About creating the archive, I believe I can turn around after getting the result without duplicate values.

1 answer

1


In your case, the simplest seems to be to use the method removeAll. First you read all the values of each of the files, and then do the "subtraction":

// método auxiliar para ler todos os valores do arquivo e retornar uma lista com esses valores
static List<String> getValuesFromFile(String filename) throws IOException {
    List<String> valores = new ArrayList<String>();

    for (String linha : Files.readAllLines(Paths.get(filename))) {
        for (String valor : linha.split(",")) {
            valores.add(valor);
        }
    }
    return valores;
}

// ler valores de cada arquivo
List<String> valoresArq1 = getValuesFromFile("arq1.txt");
List<String> valoresArq2 = getValuesFromFile("arq2.txt");

// remover de arq1 todos os valores que estão em arq2
valoresArq1.removeAll(valoresArq2);

// escrever o resultado no arquivo arq3.txt (cada elemento da lista ficará em uma linha)
Files.write(Paths.get("arq3.txt"), valoresArq1, Charset.defaultCharset());

That’s all. removeAll removes all elements of valoresArq2 who are in valoresArq1. It does not matter if there are repeats or if some value of arq2 does not exist in arq1, the method already takes care of all this.


One point that is unclear is whether the values of arq1 are one on each line, or if it is possible to have lines like 10,20,30 (since you do split in both files).

In that case, if arq1 have a line like 10,20,30 (and supposing that these values are not in arq2), the final result will have the numbers 10, 20 and 30 separated, one on each line.


About comparison of strings, in Java should be used equals instead of operators == and !=.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.