Class Median

java.lang.Object
com.landawn.abacus.util.Median

public final class Median extends Object
Author:
Haiyang Li
  • Method Summary

    Modifier and Type
    Method
    Description
    of(byte... a)
    Returns a Pair with left is the value at the index of (a.length - 1) / 2 when the array is sorted, and the right is the value at the index of a.length / 2 if the length of array is even.
    of(byte[] a, int fromIndex, int toIndex)
    Returns a Pair with left is the value at the index of (a.length - 1) / 2 when the array is sorted, and the right is the value at the index of a.length / 2 if the length of array is even.
    of(char... a)
    Returns a Pair with left is the value at the index of (a.length - 1) / 2 when the array is sorted, and the right is the value at the index of a.length / 2 if the length of array is even.
    of(char[] a, int fromIndex, int toIndex)
    Returns a Pair with left is the value at the index of (a.length - 1) / 2 when the array is sorted, and the right is the value at the index of a.length / 2 if the length of array is even.
    of(double... a)
    Returns a Pair with left is the value at the index of (a.length - 1) / 2 when the array is sorted, and the right is the value at the index of a.length / 2 if the length of array is even.
    of(double[] a, int fromIndex, int toIndex)
    Returns a Pair with left is the value at the index of (a.length - 1) / 2 when the array is sorted, and the right is the value at the index of a.length / 2 if the length of array is even.
    of(float... a)
    Returns a Pair with left is the value at the index of (a.length - 1) / 2 when the array is sorted, and the right is the value at the index of a.length / 2 if the length of array is even.
    of(float[] a, int fromIndex, int toIndex)
    Returns a Pair with left is the value at the index of (a.length - 1) / 2 when the array is sorted, and the right is the value at the index of a.length / 2 if the length of array is even.
    of(int... a)
    Returns a Pair with left is the value at the index of (a.length - 1) / 2 when the array is sorted, and the right is the value at the index of a.length / 2 if the length of array is even.
    of(int[] a, int fromIndex, int toIndex)
    Returns a Pair with left is the value at the index of (a.length - 1) / 2 when the array is sorted, and the right is the value at the index of a.length / 2 if the length of array is even.
    of(long... a)
    Returns a Pair with left is the value at the index of (a.length - 1) / 2 when the array is sorted, and the right is the value at the index of a.length / 2 if the length of array is even.
    of(long[] a, int fromIndex, int toIndex)
    Returns a Pair with left is the value at the index of (a.length - 1) / 2 when the array is sorted, and the right is the value at the index of a.length / 2 if the length of array is even.
    of(short... a)
    Returns a Pair with left is the value at the index of (a.length - 1) / 2 when the array is sorted, and the right is the value at the index of a.length / 2 if the length of array is even.
    of(short[] a, int fromIndex, int toIndex)
    Returns a Pair with left is the value at the index of (a.length - 1) / 2 when the array is sorted, and the right is the value at the index of a.length / 2 if the length of array is even.
    static <T extends Comparable<? super T>>
    Pair<T,u.Nullable<T>>
    of(Collection<? extends T> c)
     
    static <T> Pair<T,u.Nullable<T>>
    of(Collection<? extends T> c, Comparator<? super T> cmp)
     
    static <T extends Comparable<? super T>>
    Pair<T,u.Nullable<T>>
    of(T[] a)
    Returns a Pair with left is the value at the index of (a.length - 1) / 2 when the array is sorted, and the right is the value at the index of a.length / 2 if the length of array is even.
    static <T extends Comparable<? super T>>
    Pair<T,u.Nullable<T>>
    of(T[] a, int fromIndex, int toIndex)
    Returns a Pair with left is the value at the index of (a.length - 1) / 2 when the array is sorted, and the right is the value at the index of a.length / 2 if the length of array is even.
    static <T> Pair<T,u.Nullable<T>>
    of(T[] a, int fromIndex, int toIndex, Comparator<? super T> cmp)
    Returns a Pair with left is the value at the index of (a.length - 1) / 2 when the array is sorted, and the right is the value at the index of a.length / 2 if the length of array is even.
    static <T> Pair<T,u.Nullable<T>>
    of(T[] a, Comparator<? super T> cmp)
    Returns a Pair with left is the value at the index of (a.length - 1) / 2 when the array is sorted, and the right is the value at the index of a.length / 2 if the length of array is even.

    Methods inherited from class java.lang.Object

    equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • of

      @SafeVarargs public static Pair<Character,u.OptionalChar> of(char... a) throws IllegalArgumentException
      Returns a Pair with left is the value at the index of (a.length - 1) / 2 when the array is sorted, and the right is the value at the index of a.length / 2 if the length of array is even.
      Parameters:
      a -
      Returns:
      Throws:
      IllegalArgumentException - if the specified Array is null or empty.
      See Also:
    • of

      public static Pair<Character,u.OptionalChar> of(char[] a, int fromIndex, int toIndex)
      Returns a Pair with left is the value at the index of (a.length - 1) / 2 when the array is sorted, and the right is the value at the index of a.length / 2 if the length of array is even.
      Parameters:
      a -
      fromIndex -
      toIndex -
      Returns:
      See Also:
    • of

      @SafeVarargs public static Pair<Byte,u.OptionalByte> of(byte... a) throws IllegalArgumentException
      Returns a Pair with left is the value at the index of (a.length - 1) / 2 when the array is sorted, and the right is the value at the index of a.length / 2 if the length of array is even.
      Parameters:
      a -
      Returns:
      Throws:
      IllegalArgumentException - if the specified Array is null or empty.
      See Also:
    • of

      public static Pair<Byte,u.OptionalByte> of(byte[] a, int fromIndex, int toIndex)
      Returns a Pair with left is the value at the index of (a.length - 1) / 2 when the array is sorted, and the right is the value at the index of a.length / 2 if the length of array is even.
      Parameters:
      a -
      fromIndex -
      toIndex -
      Returns:
      See Also:
    • of

      @SafeVarargs public static Pair<Short,u.OptionalShort> of(short... a) throws IllegalArgumentException
      Returns a Pair with left is the value at the index of (a.length - 1) / 2 when the array is sorted, and the right is the value at the index of a.length / 2 if the length of array is even.
      Parameters:
      a -
      Returns:
      Throws:
      IllegalArgumentException - if the specified Array is null or empty.
      See Also:
    • of

      public static Pair<Short,u.OptionalShort> of(short[] a, int fromIndex, int toIndex)
      Returns a Pair with left is the value at the index of (a.length - 1) / 2 when the array is sorted, and the right is the value at the index of a.length / 2 if the length of array is even.
      Parameters:
      a -
      fromIndex -
      toIndex -
      Returns:
      See Also:
    • of

      @SafeVarargs public static Pair<Integer,u.OptionalInt> of(int... a) throws IllegalArgumentException
      Returns a Pair with left is the value at the index of (a.length - 1) / 2 when the array is sorted, and the right is the value at the index of a.length / 2 if the length of array is even.
      The input array don't need to be sorted. in other words, there is no benefit even if the array is sorted.
       
      
       Median.of(1); // -> [1, empty]
       Median.of(1, 3); // -> [1, 3]
       Median.of(1, 3, 5); // -> [3, empty]
      
       Median.of(1, 1); // -> [1, 1]
       Median.of(1, 1, 3); // -> [1, empty]
       Median.of(1, 1, 3, 5); // -> [1, 3]
       Median.of(1, 1, 1, 3, 5); // -> [1, empty]
       Median.of(1, 1, 1, 3, 3, 5); // -> [1, 3]
       Median.of(1, 1, 1, 3, 3, 3, 5); // -> [3, empty]
      
       
       
      Parameters:
      a -
      Returns:
      Throws:
      IllegalArgumentException - if the specified Array is null or empty.
    • of

      public static Pair<Integer,u.OptionalInt> of(int[] a, int fromIndex, int toIndex)
      Returns a Pair with left is the value at the index of (a.length - 1) / 2 when the array is sorted, and the right is the value at the index of a.length / 2 if the length of array is even.
      Parameters:
      a -
      fromIndex -
      toIndex -
      Returns:
      Throws:
      IllegalArgumentException - if specified array is empty or length is zero.
    • of

      @SafeVarargs public static Pair<Long,u.OptionalLong> of(long... a) throws IllegalArgumentException
      Returns a Pair with left is the value at the index of (a.length - 1) / 2 when the array is sorted, and the right is the value at the index of a.length / 2 if the length of array is even.
      Parameters:
      a -
      Returns:
      Throws:
      IllegalArgumentException - if the specified Array is null or empty.
      See Also:
    • of

      public static Pair<Long,u.OptionalLong> of(long[] a, int fromIndex, int toIndex)
      Returns a Pair with left is the value at the index of (a.length - 1) / 2 when the array is sorted, and the right is the value at the index of a.length / 2 if the length of array is even.
      Parameters:
      a -
      fromIndex -
      toIndex -
      Returns:
      See Also:
    • of

      @SafeVarargs public static Pair<Float,u.OptionalFloat> of(float... a) throws IllegalArgumentException
      Returns a Pair with left is the value at the index of (a.length - 1) / 2 when the array is sorted, and the right is the value at the index of a.length / 2 if the length of array is even.
      Parameters:
      a -
      Returns:
      Throws:
      IllegalArgumentException - if the specified Array is null or empty.
      See Also:
    • of

      public static Pair<Float,u.OptionalFloat> of(float[] a, int fromIndex, int toIndex)
      Returns a Pair with left is the value at the index of (a.length - 1) / 2 when the array is sorted, and the right is the value at the index of a.length / 2 if the length of array is even.
      Parameters:
      a -
      fromIndex -
      toIndex -
      Returns:
      See Also:
    • of

      @SafeVarargs public static Pair<Double,u.OptionalDouble> of(double... a) throws IllegalArgumentException
      Returns a Pair with left is the value at the index of (a.length - 1) / 2 when the array is sorted, and the right is the value at the index of a.length / 2 if the length of array is even.
      Parameters:
      a -
      Returns:
      Throws:
      IllegalArgumentException - if the specified Array is null or empty.
      See Also:
    • of

      public static Pair<Double,u.OptionalDouble> of(double[] a, int fromIndex, int toIndex)
      Returns a Pair with left is the value at the index of (a.length - 1) / 2 when the array is sorted, and the right is the value at the index of a.length / 2 if the length of array is even.
      Parameters:
      a -
      fromIndex -
      toIndex -
      Returns:
      See Also:
    • of

      public static <T extends Comparable<? super T>> Pair<T,u.Nullable<T>> of(T[] a) throws IllegalArgumentException
      Returns a Pair with left is the value at the index of (a.length - 1) / 2 when the array is sorted, and the right is the value at the index of a.length / 2 if the length of array is even.
      Type Parameters:
      T -
      Parameters:
      a -
      Returns:
      Throws:
      IllegalArgumentException - if the specified Array is null or empty.
      See Also:
    • of

      public static <T extends Comparable<? super T>> Pair<T,u.Nullable<T>> of(T[] a, int fromIndex, int toIndex)
      Returns a Pair with left is the value at the index of (a.length - 1) / 2 when the array is sorted, and the right is the value at the index of a.length / 2 if the length of array is even.
      Type Parameters:
      T -
      Parameters:
      a -
      fromIndex -
      toIndex -
      Returns:
      See Also:
    • of

      public static <T> Pair<T,u.Nullable<T>> of(T[] a, Comparator<? super T> cmp) throws IllegalArgumentException
      Returns a Pair with left is the value at the index of (a.length - 1) / 2 when the array is sorted, and the right is the value at the index of a.length / 2 if the length of array is even.
      Type Parameters:
      T -
      Parameters:
      a -
      cmp -
      Returns:
      Throws:
      IllegalArgumentException - if the specified Array is null or empty.
      See Also:
    • of

      public static <T> Pair<T,u.Nullable<T>> of(T[] a, int fromIndex, int toIndex, Comparator<? super T> cmp)
      Returns a Pair with left is the value at the index of (a.length - 1) / 2 when the array is sorted, and the right is the value at the index of a.length / 2 if the length of array is even.
      Type Parameters:
      T -
      Parameters:
      a -
      fromIndex -
      toIndex -
      cmp -
      Returns:
      See Also:
    • of

      public static <T extends Comparable<? super T>> Pair<T,u.Nullable<T>> of(Collection<? extends T> c)
      Type Parameters:
      T -
      Parameters:
      c -
      Returns:
    • of

      public static <T> Pair<T,u.Nullable<T>> of(Collection<? extends T> c, Comparator<? super T> cmp)
      Type Parameters:
      T -
      Parameters:
      c -
      cmp -
      Returns: