Оптимизируй код
package com.example.songmaster;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.scene.control.ListView;
import javafx.scene.layout.AnchorPane;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.scene.text.TextFlow;

import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.stream.Stream;

public class HelloController {
    @FXML
    private ListView<String> songDir;

    @FXML
    private TextFlow songFlow;


    @FXML
    private TextFlow chords;


    public void first () throws URISyntaxException {
        File dir = new File(getClass().getResource("Songs").toURI());
        File[] files = dir.listFiles();
        ObservableList<String> items = FXCollections.observableArrayList();
        for (File file: files) {
            String fileName = file.getName();
            String nameWithoutExtension = fileName.substring(0, fileName.lastIndexOf('.'));
            items.add(nameWithoutExtension);
        }
        songDir.setItems(items);
        songDir.setOnMouseClicked(event -> {
            String selectedSong = songDir.getSelectionModel().getSelectedItem();
            if (selectedSong != null) {
                String filePath = getClass().getResource("Songs/" + selectedSong + ".txt").toString();
                Path path = Paths.get(URI.create(filePath));
                String fileContent = readFileContent(path);
                songFlow.getChildren().clear();
                chords.getChildren().clear();
                displayText(fileContent);
            }
        });
    }

    private String readFileContent(Path filePath) {
        StringBuilder contentBuilder = new StringBuilder();
        try (Stream<String> stream = Files.lines(filePath, StandardCharsets.UTF_8)) {
            stream.forEach(s -> contentBuilder.append(s).append("\n"));
        } catch (IOException e) {
            e.printStackTrace();
        }
        return contentBuilder.toString();
    }

    private void displayText(String text) {
        songFlow.getChildren().clear();
        chords.getChildren().clear();

        String[] lines = text.split("\\n");

        // ArrayList для хранения слов после ">"
        ArrayList<String> wordsAfterSymbol = new ArrayList<>();

        for (String line : lines) {
            Text textNode = new Text();
            if (line.startsWith(">")) {
                String words = line.substring(1);
                words = words.replaceAll("\\s+", " ");
                textNode.setText(words);
                textNode.setFill(Color.BLUE);

                // Используем регулярное выражение для разделения слов
                String[] wordsArray = words.split("\\s+");
                for (String word : wordsArray) {
                    if (!word.isEmpty()) {
                        wordsAfterSymbol.add(word);
                    }
                }
            } else {
                textNode.setText(line);
                textNode.setFill(Color.BLACK);
            }
            textNode.setFont(Font.font("Arial", 12));
            songFlow.getChildren().add(textNode);
            songFlow.getChildren().add(new Text("\n"));
        }

        // Удаляем повторяющиеся слова
        HashSet<String> uniqueBlueWords = new HashSet<>(wordsAfterSymbol);
        uniqueBlueWords.remove(""); // Удаляем пустую строку, если присутствует

        for (String uniqueWord : uniqueBlueWords) {
            Text blueWordNode = new Text();
            blueWordNode.setText(uniqueWord);
            blueWordNode.setFill(Color.BLUE);
            blueWordNode.setFont(Font.font("Arial", 12));
            chords.getChildren().add(blueWordNode);
        }
    }


}