ronantakizawa/codereview-bench huggingface dataset:
Repository: TheAlgorithms/Java
File: src/main/java/com/thealgorithms/maths/Means.java (Java)

Code before:
package com.thealgorithms.maths;

import java.util.List;

/**
 * https://en.wikipedia.org/wiki/Mean
 * <p>
 * by: Punit Patel
 */
public final class Means {

    private Means() {
    }

    /**
     * Arithmetic / Pythagorean Mean = (x₁ + x₂ + ... +  xₙ) / n
     */
    public static Double arithmetic(final List<Double> numbers) {
        if (numbers.isEmpty()) throw new IllegalArgumentException("Emtpy list given for Mean computation.");

        return numbers.stream().reduce((x, y) -> x + y).get() / numbers.size();
    }

    /**
     * Geometric Mean = (x₁ · x₂ · ... · xₙ) ^ 1/n
     */
    public static Double geometric(final List<Double> numbers) {
        if (numbers.isEmpty()) throw new IllegalArgumentException("Emtpy list given for Mean computation.");

        return Math.pow(numbers.stream().reduce((x, y) -> x * y).get(), 1.0 / numbers.size());
    }

    /**
     * Harmonic Mean = n / (1/x₁ + 1/x₂ + ... +  1/xₙ)
     */
    public static double harmonic(final List<Double> numbers) {
        if (numbers.isEmpty()) throw new IllegalArgumentException("Emtpy list given for Mean computation.");

        return numbers.size() / numbers.stream().map(x -> 1 / x).reduce((x, y) -> x + y).get();
    }
}


Review comment (suggestion):
```suggestion
        if (numbers.isEmpty()) {
            throw new IllegalArgumentException("Emtpy list given for Mean computation.");
        }
```

Code after:
package com.thealgorithms.maths;

import java.util.stream.StreamSupport;
import org.apache.commons.collections4.IterableUtils;

/**
 * https://en.wikipedia.org/wiki/Mean
 * <p>
 * by: Punit Patel
 */
public final class Means {

    private Means() {
    }

    /**
     * @brief computes the [Arithmetic Mean](https://en.wikipedia.org/wiki/Arithmetic_mean) of the input
     * @param numbers the input numbers
     * @throws IllegalArgumentException empty input
     * @return the arithmetic mean of the input numbers
     */
    public static Double arithmetic(final Iterable<Double> numbers) {
        checkIfNotEmpty(numbers);
        return StreamSupport.stream(numbers.spliterator(), false).reduce((x, y) -> x + y).get() / IterableUtils.size(numbers);
    }

    /**
     * @brief computes the [Geometric Mean](https://en.wikipedia.org/wiki/Geometric_mean) of the input
     * @param numbers the input numbers
     * @throws IllegalArgumentException empty input
     * @return the geometric mean of the input numbers
     */
    public static Double geometric(final Iterable<Double> numbers) {
        checkIfNotEmpty(numbers);
        return Math.pow(StreamSupport.stream(numbers.spliterator(), false).reduce((x, y) -> x * y).get(), 1d / IterableUtils.size(numbers));
    }

    /**
     * @brief computes the [Harmonic Mean](https://en.wikipedia.org/wiki/Harmonic_mean) of the input
     * @param numbers the input numbers
     * @throws IllegalArgumentException empty input
     * @return the harmonic mean of the input numbers
     */
    public static Double harmonic(final Iterable<Double> numbers) {
Repository: TheAlgorithms/Java
File: src/main/java/com/thealgorithms/maths/Means.java (Java)

Code before:
    private Means() {
    }

    /**
     * Arithmetic / Pythagorean Mean = (x₁ + x₂ + ... +  xₙ) / n
     */
    public static Double arithmetic(final List<Double> numbers) {
        if (numbers.isEmpty()) throw new IllegalArgumentException("Emtpy list given for Mean computation.");

        return numbers.stream().reduce((x, y) -> x + y).get() / numbers.size();
    }

    /**
     * Geometric Mean = (x₁ · x₂ · ... · xₙ) ^ 1/n
     */
    public static Double geometric(final List<Double> numbers) {
        if (numbers.isEmpty()) throw new IllegalArgumentException("Emtpy list given for Mean computation.");

        return Math.pow(numbers.stream().reduce((x, y) -> x * y).get(), 1.0 / numbers.size());
    }

    /**
     * Harmonic Mean = n / (1/x₁ + 1/x₂ + ... +  1/xₙ)
     */
    public static double harmonic(final List<Double> numbers) {
        if (numbers.isEmpty()) throw new IllegalArgumentException("Emtpy list given for Mean computation.");

        return numbers.size() / numbers.stream().map(x -> 1 / x).reduce((x, y) -> x + y).get();
    }
}


Review comment (suggestion):
```suggestion
        if (numbers.isEmpty()) {
            throw new IllegalArgumentException("Emtpy list given for Mean computation.");
        }
```

Code after:

    private Means() {
    }

    /**
     * @brief computes the [Arithmetic Mean](https://en.wikipedia.org/wiki/Arithmetic_mean) of the input
     * @param numbers the input numbers
     * @throws IllegalArgumentException empty input
     * @return the arithmetic mean of the input numbers
     */
    public static Double arithmetic(final Iterable<Double> numbers) {
        checkIfNotEmpty(numbers);
        return StreamSupport.stream(numbers.spliterator(), false).reduce((x, y) -> x + y).get() / IterableUtils.size(numbers);
    }

    /**
     * @brief computes the [Geometric Mean](https://en.wikipedia.org/wiki/Geometric_mean) of the input
     * @param numbers the input numbers
     * @throws IllegalArgumentException empty input
     * @return the geometric mean of the input numbers
     */
    public static Double geometric(final Iterable<Double> numbers) {
        checkIfNotEmpty(numbers);
        return Math.pow(StreamSupport.stream(numbers.spliterator(), false).reduce((x, y) -> x * y).get(), 1d / IterableUtils.size(numbers));
    }

    /**
     * @brief computes the [Harmonic Mean](https://en.wikipedia.org/wiki/Harmonic_mean) of the input
     * @param numbers the input numbers
     * @throws IllegalArgumentException empty input
     * @return the harmonic mean of the input numbers
     */
    public static Double harmonic(final Iterable<Double> numbers) {
        checkIfNotEmpty(numbers);
        return IterableUtils.size(numbers) / StreamSupport.stream(numbers.spliterator(), false).reduce(0d, (x, y) -> x + 1d / y);
    }

    private static void checkIfNotEmpty(final Iterable<Double> numbers) {
        if (!numbers.iterator().hasNext()) {
            throw new IllegalArgumentException("Emtpy list given for Mean computation.");
        }
    }
}
Repository: TheAlgorithms/Java
File: src/main/java/com/thealgorithms/maths/Means.java (Java)

Code before:
    /**
     * @brief computes the [Geometric Mean](https://en.wikipedia.org/wiki/Geometric_mean) of the input
     * @param numbers the input numbers
     * @throws IllegalArgumentException empty input
     * @return the geometric mean of the input numbers
     */
    public static Double geometric(final Iterable<Double> numbers) {
        if (!numbers.iterator().hasNext()) {
            throw new IllegalArgumentException("Emtpy list given for Mean computation.");
        }

        return Math.pow(StreamSupport.stream(numbers.spliterator(), false).reduce((x, y) -> x * y).get(), 1.0 / IterableUtils.size(numbers));
    }

    /**
     * @brief computes the [Harmonic Mean](https://en.wikipedia.org/wiki/Harmonic_mean) of the input
     * @param numbers the input numbers
     * @throws IllegalArgumentException empty input
     * @return the harmonic mean of the input numbers
     */
    public static double harmonic(final Iterable<Double> numbers) {
        if (!numbers.iterator().hasNext()) {
            throw new IllegalArgumentException("Emtpy list given for Mean computation.");
        }

        return IterableUtils.size(numbers) / StreamSupport.stream(numbers.spliterator(), false).map(x -> 1 / x).reduce((x, y) -> x + y).get();
    }
}


Review comment (suggestion):
I think this can be simplified to
```suggestion
        return IterableUtils.size(numbers) / StreamSupport.stream(numbers.spliterator(), false).reduce(0.0, (x, y) -> x + 1d / y);
```

Code after:
     * @throws IllegalArgumentException empty input
     * @return the geometric mean of the input numbers
     */
    public static Double geometric(final Iterable<Double> numbers) {
        checkIfNotEmpty(numbers);
        return Math.pow(StreamSupport.stream(numbers.spliterator(), false).reduce((x, y) -> x * y).get(), 1d / IterableUtils.size(numbers));
    }

    /**
     * @brief computes the [Harmonic Mean](https://en.wikipedia.org/wiki/Harmonic_mean) of the input
     * @param numbers the input numbers
     * @throws IllegalArgumentException empty input
     * @return the harmonic mean of the input numbers
     */
    public static Double harmonic(final Iterable<Double> numbers) {
        checkIfNotEmpty(numbers);
        return IterableUtils.size(numbers) / StreamSupport.stream(numbers.spliterator(), false).reduce(0d, (x, y) -> x + 1d / y);
    }

    private static void checkIfNotEmpty(final Iterable<Double> numbers) {
        if (!numbers.iterator().hasNext()) {
            throw new IllegalArgumentException("Emtpy list given for Mean computation.");
        }
    }
}
Repository: TheAlgorithms/Java
File: src/main/java/com/thealgorithms/io/BufferedReader.java (Java)

Code before:
package com.thealgorithms.io;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * Mimics the actions of the Original buffered reader
 * implements other actions, such as peek(n) to lookahead,
 * block() to read a chunk of size {BUFFER SIZE}
 * <p>
 * Author: Kumaraswamy B.G (Xoma Dev)
 */
public class BufferedReader {

    private static final int DEFAULT_BUFFER_SIZE = 5;

    /**
     * The Maximum number of bytes the buffer can hold.
     * Value is changed when encountered Eof to not
     * cause overflow read of 0 bytes
     */

    private int bufferSize;
    private final byte[] buffer;

    /**
     * posRead -> indicates the next byte to read
     */
    private int posRead = 0, bufferPos = 0;

    private boolean foundEof = false;

    private InputStream input;

    public BufferedReader(byte[] input) throws IOException {
        this(new ByteArrayInputStream(input));
    }

    public BufferedReader(InputStream input) throws IOException {
        this(input, DEFAULT_BUFFER_SIZE);
    }

    public BufferedReader(InputStream input, int bufferSize) throws IOException {

Review comment (suggestion):
```suggestion
     * The maximum number of bytes the buffer can hold.
```

Code after:
package com.thealgorithms.io;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * Mimics the actions of the Original buffered reader
 * implements other actions, such as peek(n) to lookahead,
 * block() to read a chunk of size {BUFFER SIZE}
 * <p>
 * Author: Kumaraswamy B.G (Xoma Dev)
 */
public class BufferedReader {

    private static final int DEFAULT_BUFFER_SIZE = 5;

    /**
     * The maximum number of bytes the buffer can hold.
     * Value is changed when encountered Eof to not
     * cause overflow read of 0 bytes
     */

    private int bufferSize;
    private final byte[] buffer;

    /**
     * posRead -> indicates the next byte to read
     */
    private int posRead = 0, bufferPos = 0;

    private boolean foundEof = false;

    private InputStream input;

    public BufferedReader(byte[] input) throws IOException {
        this(new ByteArrayInputStream(input));
    }

    public BufferedReader(InputStream input) throws IOException {
        this(input, DEFAULT_BUFFER_SIZE);
    }

    public BufferedReader(InputStream input, int bufferSize) throws IOException {
Repository: TheAlgorithms/Java
File: src/main/java/com/thealgorithms/misc/TwoSumProblem.java (Java)

Code before:
package com.thealgorithms.misc;

import java.util.*;

public class TwoSumProblem {
    /**
     * The function "twoSum" takes an array of integers and a target integer as input, and returns an
     * array of two indices where the corresponding elements in the input array add up to the target.
     * @param nums An array of integers.
     * @param target The target is the sum that we are trying to find using two numbers from the given array.
     * @return The method `twoSum` returns an array of integers.
     * @author Bama Charan Chhandogi (https://github.com/BamaCharanChhandogi)
     */
    public static int[] twoSum(int[] arr, int target) {
        HashMap<Integer, Integer> map = new HashMap<>();
        for (int i = 0; i < arr.length; i++) {
            map.put(arr[i], i);
        }
        for (int i = 0; i < arr.length; i++) {
            int arrP = arr[i];
            int rem = target - arrP;
            if (map.containsKey(rem)) {
                int index = map.get(rem);
                if (index == i) {
                    continue;
                }
                return new int[] {i, index};
            }
        }
        return new int[] {};
    }
}


Review comment (suggestion):
Instead of first adding all elements and then check for answer , we can do this in one loop itself by first checking for answer and then adding element in hashmap, this way time taken for program to run will be reduced and extra condition `index == i `will be be removed.

Code after:
package com.thealgorithms.misc;

import java.util.HashMap;
import java.util.Optional;
import org.apache.commons.lang3.tuple.Pair;

public final class TwoSumProblem {
    private TwoSumProblem() {
    }

    /**
     * The function "twoSum" takes an array of integers and a target integer as input, and returns an
     * array of two indices where the corresponding elements in the input array add up to the target.
     * @param values An array of integers.
     * @param target The target is the sum that we are trying to find using two numbers from the given array.
     * @return A pair or indexes such that sum of values at these indexes equals to the target
     * @author Bama Charan Chhandogi (https://github.com/BamaCharanChhandogi)
     */

    public static Optional<Pair<Integer, Integer>> twoSum(final int[] values, final int target) {
        HashMap<Integer, Integer> valueToIndex = new HashMap<>();
        for (int i = 0; i < values.length; i++) {
            final var rem = target - values[i];
            if (valueToIndex.containsKey(rem)) {
                return Optional.of(Pair.of(valueToIndex.get(rem), i));
            }
            if (!valueToIndex.containsKey(values[i])) {
                valueToIndex.put(values[i], i);
            }
        }
        return Optional.empty();
    }
}
Repository: TheAlgorithms/Java
File: src/main/java/com/thealgorithms/misc/TwoSumProblem.java (Java)

Code before:
package com.thealgorithms.misc;

import java.util.*;

public class TwoSumProblem {
    /**
     * The function "twoSum" takes an array of integers and a target integer as input, and returns an
     * array of two indices where the corresponding elements in the input array add up to the target.
     * @param nums An array of integers.
     * @param target The target is the sum that we are trying to find using two numbers from the given array.
     * @return The method `twoSum` returns an array of integers.
     * @author Bama Charan Chhandogi (https://github.com/BamaCharanChhandogi)
     */
    public static int[] twoSum(int[] arr, int target) {
        HashMap<Integer, Integer> map = new HashMap<>();
        for (int i = 0; i < arr.length; i++) {
            int rem = target - arr[i];
            if (map.containsKey(rem)) {
                int index = map.get(rem);
                return new int[] {index, i};
            }
            if (!map.containsKey(arr[i])) {
                map.put(arr[i], i);
            }
        }
        return new int[] {};
    }
}


Review comment (suggestion):
```suggestion
public final class TwoSumProblem {
    private TwoSumProblem() {
    }
```

Code after:
package com.thealgorithms.misc;

import java.util.HashMap;
import java.util.Optional;
import org.apache.commons.lang3.tuple.Pair;

public final class TwoSumProblem {
    private TwoSumProblem() {
    }

    /**
     * The function "twoSum" takes an array of integers and a target integer as input, and returns an
     * array of two indices where the corresponding elements in the input array add up to the target.
     * @param values An array of integers.
     * @param target The target is the sum that we are trying to find using two numbers from the given array.
     * @return A pair or indexes such that sum of values at these indexes equals to the target
     * @author Bama Charan Chhandogi (https://github.com/BamaCharanChhandogi)
     */

    public static Optional<Pair<Integer, Integer>> twoSum(final int[] values, final int target) {
        HashMap<Integer, Integer> valueToIndex = new HashMap<>();
        for (int i = 0; i < values.length; i++) {
            final var rem = target - values[i];
            if (valueToIndex.containsKey(rem)) {
                return Optional.of(Pair.of(valueToIndex.get(rem), i));
            }
            if (!valueToIndex.containsKey(values[i])) {
                valueToIndex.put(values[i], i);
            }
        }
Repository: TheAlgorithms/Java
File: src/main/java/com/thealgorithms/misc/TwoSumProblem.java (Java)

Code before:
package com.thealgorithms.misc;

import java.util.*;

public class TwoSumProblem {
    /**
     * The function "twoSum" takes an array of integers and a target integer as input, and returns an
     * array of two indices where the corresponding elements in the input array add up to the target.
     * @param nums An array of integers.
     * @param target The target is the sum that we are trying to find using two numbers from the given array.
     * @return The method `twoSum` returns an array of integers.
     * @author Bama Charan Chhandogi (https://github.com/BamaCharanChhandogi)
     */
    public static int[] twoSum(int[] arr, int target) {
        HashMap<Integer, Integer> map = new HashMap<>();
        for (int i = 0; i < arr.length; i++) {
            int rem = target - arr[i];
            if (map.containsKey(rem)) {
                int index = map.get(rem);
                return new int[] {index, i};
            }
            if (!map.containsKey(arr[i])) {
                map.put(arr[i], i);
            }
        }
        return new int[] {};
    }
}


Review comment (suggestion):
```suggestion
    public static int[] twoSum(final int[] arr, final int target) {
```

Code after:
package com.thealgorithms.misc;

import java.util.HashMap;
import java.util.Optional;
import org.apache.commons.lang3.tuple.Pair;

public final class TwoSumProblem {
    private TwoSumProblem() {
    }

    /**
     * The function "twoSum" takes an array of integers and a target integer as input, and returns an
     * array of two indices where the corresponding elements in the input array add up to the target.
     * @param values An array of integers.
     * @param target The target is the sum that we are trying to find using two numbers from the given array.
     * @return A pair or indexes such that sum of values at these indexes equals to the target
     * @author Bama Charan Chhandogi (https://github.com/BamaCharanChhandogi)
     */

    public static Optional<Pair<Integer, Integer>> twoSum(final int[] values, final int target) {
        HashMap<Integer, Integer> valueToIndex = new HashMap<>();
        for (int i = 0; i < values.length; i++) {
            final var rem = target - values[i];
            if (valueToIndex.containsKey(rem)) {
                return Optional.of(Pair.of(valueToIndex.get(rem), i));
            }
            if (!valueToIndex.containsKey(values[i])) {
                valueToIndex.put(values[i], i);
            }
        }
        return Optional.empty();
    }
}
Repository: TheAlgorithms/Java
File: src/main/java/com/thealgorithms/datastructures/disjointsetunion/Node.java (Java)

Code before:
package com.thealgorithms.datastructures.disjointsetunion;

public class Node<T> {

    /**
     * The rank of the node, used for optimizing union operations.
     */
    public int rank;

    /**
     * Reference to the parent node in the set.
     * Initially, a node is its own parent (represents a singleton set).
     */
    public Node<T> parent;

    /**
     * The data element associated with the node.
     */
    public T data;

    public Node(T data) {
        this.data = data;
        parent = this; // Initially, a node is its own parent.
    }
}


Review comment (suggestion):
```suggestion
    public Node(final T data) {
```

Code after:
package com.thealgorithms.datastructures.disjointsetunion;

public class Node<T> {

    /**
     * The rank of the node, used for optimizing union operations.
     */
    public int rank;

    /**
     * Reference to the parent node in the set.
     * Initially, a node is its own parent (represents a singleton set).
     */
    public Node<T> parent;

    /**
     * The data element associated with the node.
     */
    public T data;

    public Node(final T data) {
        this.data = data;
        parent = this; // Initially, a node is its own parent.
    }
}
Repository: TheAlgorithms/Java
File: src/main/java/com/thealgorithms/datastructures/disjointsetunion/DisjointSetUnion.java (Java)

Code before:
package com.thealgorithms.datastructures.disjointsetunion;

/**
 * Disjoint Set Union or DSU is useful for solving problems related to connected components,
 * cycle detection in graphs, and maintaining relationships in disjoint sets of data.
 * It is commonly employed in graph algorithms and problems.
 *
 * @see <a href="https://en.wikipedia.org/wiki/Disjoint-set_data_structure">Disjoint Set Union</a>
 */
public class DisjointSetUnion<T> {

    /**
     * Creates a new node of DSU with parent initialised as same node
     */
    public Node<T> makeSet(T x) {
        return new Node<T>(x);
    }

    /**
     * Finds and returns the representative (root) element of the set to which a given element belongs.
     * This operation uses path compression to optimize future findSet operations.
     */
    public Node<T> findSet(Node<T> node) {
        while (node != node.parent) {
            node = node.parent;
        }
        return node;
    }

    /**
     * Unions two sets by merging their representative elements. The merge is performed based on the rank of each set
     * to ensure efficient merging and path compression to optimize future findSet operations.
     */
    public void unionSets(Node<T> x, Node<T> y) {
        Node<T> nx = findSet(x);
        Node<T> ny = findSet(y);

        if (nx == ny) {
            return; // Both elements already belong to the same set.
        }

Review comment (suggestion):
```suggestion
    public Node<T> makeSet(final T x) {
```

Code after:
package com.thealgorithms.datastructures.disjointsetunion;

/**
 * Disjoint Set Union or DSU is useful for solving problems related to connected components,
 * cycle detection in graphs, and maintaining relationships in disjoint sets of data.
 * It is commonly employed in graph algorithms and problems.
 *
 * @see <a href="https://en.wikipedia.org/wiki/Disjoint-set_data_structure">Disjoint Set Union</a>
 */
public class DisjointSetUnion<T> {

    /**
     * Creates a new node of DSU with parent initialised as same node
     */
    public Node<T> makeSet(final T x) {
        return new Node<T>(x);
    }

    /**
     * Finds and returns the representative (root) element of the set to which a given element belongs.
     * This operation uses path compression to optimize future findSet operations.
     */
    public Node<T> findSet(Node<T> node) {
        while (node != node.parent) {
            node = node.parent;
        }
        return node;
    }

    /**
     * Unions two sets by merging their representative elements. The merge is performed based on the rank of each set
     * to ensure efficient merging and path compression to optimize future findSet operations.
     */
    public void unionSets(final Node<T> x, final Node<T> y) {
        Node<T> nx = findSet(x);
        Node<T> ny = findSet(y);

        if (nx == ny) {
            return; // Both elements already belong to the same set.
        }