Помогите оставить логику такой же, а код сократить. 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(); BufferedReader reader1 = new BufferedReader(new FileReader(file1)); List<String> strings1 = new ArrayList<>(); while (reader1.ready()) { strings1.add(reader1.readLine()); } reader1.close(); BufferedReader reader2 = new BufferedReader(new FileReader(file2)); List<String> strings2 = new ArrayList<>(); while (reader2.ready()) { strings2.add(reader2.readLine()); } reader2.close(); int min = 0; min = strings1.size() < strings2.size() ? strings1.size() : strings2.size(); int count1 = 0; int count2 = 0; for (int i = 0; i < min; i++) { if(count2+1 < min) { if (strings1.get(i).equals(strings2.get(count2))) { lines.add(new LineItem(Type.SAME, strings1.get(i))); count1++; count2++; } else if (strings1.get(i).equals(strings2.get(count2 + 1))) { lines.add(new LineItem(Type.ADDED, strings2.get(count2))); lines.add(new LineItem(Type.SAME, strings2.get(count2 + 1))); count1++; count2 = count2 + 2; } else {lines.add(new LineItem(Type.REMOVED, strings1.get(i))); count1++;} } } if(count1 < strings1.size() && count2 < strings2.size()){ System.out.println(strings1.get(count1) + " " + strings2.get(count2)); if(strings1.get(count1).equals(strings2.get(count2))){ lines.add(new LineItem(Type.SAME, strings1.get(count1))); count1++; count2++; } else if(strings1.size() - count1 == strings2.size() - count2){ lines.add(new LineItem(Type.REMOVED, strings1.get(count1))); lines.add(new LineItem(Type.ADDED, strings2.get(count2))); count1++; count2++; } else{ lines.add(new LineItem(Type.REMOVED, strings1.get(count1))); lines.add(new LineItem(Type.SAME, strings1.get(count1+1))); count1 = count1 + 2; count2++; } } if(count2 < strings2.size()){ lines.add(new LineItem(Type.ADDED, strings2.get(count2))); } if(count1 < strings1.size()){ lines.add(new LineItem(Type.REMOVED, strings1.get(count1))); } for (int i = 0; i < lines.size(); i++) { System.out.println("" + lines.get(i).type + " " + lines.get(i).line); } } 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; } } }