Написал такую каракатицу. Не пойму почему выводит пары палиндромов не в том порядке как в файле (содержание файла как в примере в условии)
public class Solution {
    public static List<Pair> result = new LinkedList<>();



    public static void main(String[] args) {
        try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
             BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(bufferedReader.readLine()),"UTF-8"))){
            String data = "";
            while (reader.ready()) {
                data +=  reader.readLine() + " ";
            }
            //System.out.println(data);

            ArrayList<String> words = new ArrayList<>(Arrays.asList(data.split(" ")));//поместили в список слова из файла, просплитав через пробелы
            Map <String, String> map = new HashMap<>();//чтобы отсечь повторяющиеся ключи

            for (String word1: words
            ) {
                for (String word2: words
                ) {
                    StringBuilder stringBuilderReverse = new StringBuilder(word2);
                    String word2Reverse = stringBuilderReverse.reverse().toString();
                    if (word1.equals(word2Reverse)&&!map.containsValue(word2Reverse)) {
                        map.put(word1, word2);
                    }
                }
            }

            for (Map.Entry<String, String> entry: map.entrySet()
                 ) {
                //System.out.println(entry.getKey()+" : "+entry.getValue());
                Pair pair = new Pair();
                pair.first = entry.getKey();
                pair.second = entry.getValue();
                result.add(pair);
            }

            result.forEach(System.out::println);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static class Pair {
        String first;
        String second;

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;

            Pair pair = (Pair) o;

            if (first != null ? !first.equals(pair.first) : pair.first != null) return false;
            return second != null ? second.equals(pair.second) : pair.second == null;

        }

        @Override
        public int hashCode() {
            int result = first != null ? first.hashCode() : 0;
            result = 31 * result + (second != null ? second.hashCode() : 0);
            return result;
        }

        @Override
        public String toString() {
            return first == null && second == null ? "" :
                    first == null ? second :
                            second == null ? first :
                                    first.compareTo(second) < 0 ? first + " " + second : second + " " + first;

        }
    }
}
вывод: рот тор тот тот о о