Repository: TheAlgorithms/Java
File: src/main/java/com/thealgorithms/datastructures/graphs/BoruvkaAlgorithm.java (Java)

Code before:
        }
    }

    /**
     * Represents a subset for Union-Find operations
     */
    static class Component {
        int parent;
        int rank;

        Component(final int parent, final int rank) {
            this.parent = parent;
            this.rank = rank;
        }
    }

    /**
     * Represents the state of Union-Find components and the result list
     */
    static class BoruvkaState {
        List<Edge> result;
        Component[] components;
        Graph graph;

        BoruvkaState(Graph graph) {
            this.result = new ArrayList<>();
            this.components = initializeSubsets(graph);
            this.graph = graph;
        }

        /**
         * Adds the cheapest edges to the result list and performs Union operation on the subsets.
         *
         * @param cheapest Array containing the cheapest edge for each subset.
         */
        void merge(Edge[] cheapest) {
            for (int i = 0; i < graph.vertex; ++i) {
                if (cheapest[i] != null) {
                    final var set1 = find(components, cheapest[i].src);
                    final var set2 = find(components, cheapest[i].dest);

                    if (set1 != set2) {
                        result.add(cheapest[i]);
                        union(components, set1, set2);
                    }
                }
            }
        }

        /**
         * Checks if there are more edges to add to the result list

Review comment (suggestion):
```suggestion
        List<Edge> result = new ArrayList<>();;
        Component[] components;
        final Graph graph;

        BoruvkaState(final Graph graph) {
```

Code after:
        }
    }

    /**
     * Represents a subset for Union-Find operations
     */
    private static class Component {
        int parent;
        int rank;

        Component(final int parent, final int rank) {
            this.parent = parent;
            this.rank = rank;
        }
    }

    /**
     * Represents the state of Union-Find components and the result list
     */
    private static class BoruvkaState {
        List<Edge> result;
        Component[] components;
        final Graph graph;

        BoruvkaState(final Graph graph) {
            this.result = new ArrayList<>();
            this.components = initializeComponents(graph);
            this.graph = graph;
        }

        /**
         * Adds the cheapest edges to the result list and performs Union operation on the subsets.
         *
         * @param cheapest Array containing the cheapest edge for each subset.
         */
        void merge(final Edge[] cheapest) {
            for (int i = 0; i < graph.vertex; ++i) {
                if (cheapest[i] != null) {
                    final var component1 = find(components, cheapest[i].src);
                    final var component2 = find(components, cheapest[i].dest);

                    if (component1 != component2) {
                        result.add(cheapest[i]);
                        union(components, component1, component2);
                    }
                }
            }
        }

        /**
         * Checks if there are more edges to add to the result list