Вопрос

I seem to be having trouble getting an output that shows the differences between two arraylist. Originally I was trying to read in two files and write to a new file the contents that were not in both original files but couldn't get it to work, so I tried a test with an arraylist but had the same issue.

I'm using the equals() method and it works fine with searching for content that are in both files. It's just when looking for content not in both files that the problem arises.

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.PrintWriter;
import java.util.ArrayList;

public static void main(String[] args) throws Exception {
    File txtFile = new File("file path goes here");

    // Contents of that text file
   /**
    *     Test1
    *     Test2
    *     Test3
    *     Test4
    *     Test5
    *     Test6
    */

    FileReader inputStream = new FileReader(txtFile);
    BufferedReader reader = new BufferedReader(inputStream);
    String input;
    ArrayList<String> list = new ArrayList<String>();
    ArrayList<String> list2 = new ArrayList<String>();

    while((input = reader.readLine()) != null) {
        list.add(input);
    }

    reader.close();
    list2.add("Test1");
    list2.add("Test4");
    list2.add("Test5");

    File newFile = new File("new file location/name");
    PrintWriter writer = new PrintWriter(newFile);

    for(int i = 0; i < list.size(); i++) {
        for(int c = 0; c < list2.size(); c++) {
            if(!list.get(i).equals(list2.get(c))) {
                writer.println(list.get(i));
            }
        }
    }
    writer.close();
}

The output that I get with the code as is is:

Test1
Test1
Test2
Test2
Test2
Test3
Test3
Test3
Test4
Test4
Test5
Test5
Test6
Test6
Test6

I've tried foreach loops and closing the writer in different places but can't get the correct output.

The output should be: Test2, Test3, Test6

I was wondering if someone can explain what is going on?

Это было полезно?

Решение

Try:

List<String> difference = new ArrayList<String>(list);
difference.removeAll(list2);

Looping difference gives: "Test2", "Test3" and "Test6".

Is this what you want?

Другие советы

Following piece of code store contents of file1 in list1 and contents of file2 in list2. Then compare both the list & store the uncommon contents in new list & then write in file3.

Introduce two new ArrayList variables tempList1 & tempList2. Copy the contents of list1 & list2 into them respectively. We're just need to remove same content from both tempList & we'll end up with two temp list variables which has unique elements in them. Copy the contents of one tempList2 into tempList1. The ArrayList tempList1 should be written to file3 now.

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.PrintWriter;
import java.util.ArrayList;

public class Test {

public static void main(String[] args) throws Exception {

    String line;

    ArrayList<String> list1 = new ArrayList<>();
    BufferedReader br1 = new BufferedReader(new FileReader("E:/file1.txt"));

    while ((line = br1.readLine()) != null) { // read lines of file1
        list1.add(line); // and store in list1
    }

    ArrayList<String> list2 = new ArrayList<>();
    BufferedReader br2 = new BufferedReader(new FileReader("E:/file2.txt"));

    while ((line = br2.readLine()) != null) { // read lines of file2
        list2.add(line); // and store in list2
    }

    list1 = compare(list1, list2);

    PrintWriter writer = new PrintWriter("E:/file3.txt");
    writer.write(list1.toString()); // writing desired output in file3
    writer.close();

}// END of main

// This method compare both list & return a new list with uncommon elements.
public static ArrayList<String> compare(ArrayList<String> list1,
        ArrayList<String> list2) {

    // we're going to remove matched elements while comparison.
    // That's why, we'll create copy of list1 & list2

    ArrayList<String> tempList1 = new ArrayList<>();
    tempList1.addAll(list1);
    ArrayList<String> tempList2 = new ArrayList<>();
    tempList2.addAll(list2);

    // Iterate & remove common elements
    for (String a : list1) {
        for (String b : list2) {
            if (a.equals(b)) {
                tempList1.remove(b);
                tempList2.remove(b);
            }
        }
    }
    // all common elements have been removed. both tempList has unique elements

    tempList1.addAll(tempList2);
    return tempList1;

}// END of compare()
}

The problem lies within this part:

for(int i = 0; i < list.size(); i++) {
    for(int c = 0; c < list2.size(); c++) {
        if(!list.get(i).equals(list2.get(c))) {
            writer.println(list.get(i));
        }
    }
 }

You visit each element of the second list for each element of the first. Instead it should be:

for(int i = 0; i < list.size(); i++) {
    boolean found = false;
    for(int c = 0; c < list2.size(); c++) {
        if(list.get(i).equals(list2.get(c))) {
            found = true;
            break;
        }
    }

    if(!found) {
        writer.println(list.get(i));
    }
 }

Edit: It also depends on what you want to achieve, this prints out every element in the first list that is not in the second list but not the other way around.

Use .equalsIgnoreCase("String");

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top