Тестировал и решил задачу на списках:
Стартовый:
file2
file3
file4
file5
file6
file8
file10
file12
file13
file14
Измененный:
file1
file2
file4
file6
file7
file8
file9
file10
file11
file12
file14
file15
Результирующий:
file1 add
file2 save
file3 remove
file4 save
file5 remove
file6 save
file7 add
file8 save
file9 add
file10 save
file11 add
file12 save
file13 remove
file14 save
file15 add
Все отлично работает. НО как оказалось тут еще и названия строк могут повторяться даже в 1-м файле что полностью ломает логику моего кода. Так что скиньте рабочий код, посмотрю как тут надо было изощриться
package com.javarush.task.task19.task1916;
import java.util.*;
import java.io.*;
/*
Отслеживаем изменения
*/
public class Solution {
public static List<LineItem> lines = new ArrayList<LineItem>();
public static void main(String[] args) throws IOException{
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String file1 = reader.readLine();
String file2 = reader.readLine();
reader.close();
// String file1 = "src/test.txt";
// String file2 = "src/test2.txt";
ArrayList<String> list1 = new ArrayList<>();
ArrayList<String> list2 = new ArrayList<>();
BufferedReader reader1 = new BufferedReader(new FileReader(file1));
BufferedReader reader2 = new BufferedReader(new FileReader(file2));
while (reader1.ready()) {
list1.add(reader1.readLine());
}
reader1.close();
while (reader2.ready()) {
list2.add(reader2.readLine());
}
reader2.close();
HashSet<String> set = new HashSet<>();
for (int i = 0; i < list1.size(); i++){
set.add(list1.get(i));
}
for (int i = 0; i < list2.size(); i++){
set.add(list2.get(i));
}
ArrayList<String> listAdded = new ArrayList<>();
ArrayList<String> listRemoved = new ArrayList<>();
ArrayList<String> listSame = new ArrayList<>();
for (String elem : set){
if (! list1.contains(elem))
listAdded.add(elem);
if (! list2.contains(elem))
listRemoved.add(elem);
if (list2.contains(elem) && list1.contains(elem))
listSame.add(elem);
}
int k = 0;
int flag = 0;
String[] finArr = new String[listAdded.size() + listRemoved.size() + listSame.size()];
for (int i = 0; i < list1.size(); i++){
if (i == 0 && flag == 0) {
if ( list1.get(0).equals(list2.get(0))) {
finArr[k] = list1.get(0);
}else if (listRemoved.contains(list1.get(0))) {
finArr[k] = list1.get(0);
}else
finArr[k] = list2.get(0);
i--; flag++;
}else {
if (listSame.contains(finArr[k - 1])) {
if (listSame.contains(list1.get(i))) {
int gg = list2.indexOf(finArr[k - 1]);
finArr[k] = list2.get(gg + 1);
i--;
}else {
finArr[k] = list1.get(i);
}
}else {
if (listSame.contains(list1.get(i))) {
finArr[k] = list1.get(i);
}else {
String sss = list1.get(i-1);
int gg = list2.indexOf(sss);
finArr[k] = list2.get(gg + 1);
i--;
}
}
}
k++;
}
if (finArr.length > k) {
int gg = list2.indexOf(finArr[k-1]);
finArr[k] = list2.get(gg + 1);
}
for (int i = 0; i < finArr.length; i++) {
System.out.println(" elem: "+finArr[i]);
}
for (int i = 0; i < finArr.length; i++){
if (listSame.contains(finArr[i]))
lines.add(new LineItem(Type.SAME, finArr[i]));
if (listRemoved.contains(finArr[i]))
lines.add(new LineItem(Type.REMOVED, finArr[i]));
if (listAdded.contains(finArr[i]))
lines.add(new LineItem(Type.ADDED, finArr[i]));
}
}
public static enum Type {
ADDED, //добавлена новая строка
REMOVED, //удалена строка
SAME //без изменений
}
public static class LineItem {
public Type type;
public String line;
public LineItem(Type type, String line) {
this.type = type;
this.line = line;
}
}
}