@Test public void test_getMedian_偶数_割り切れない() { List<Long> list = Arrays.asList(new Long[] { 2L, 5L, 10L, 15L }); assertEquals(7L, MathUtil.getMedian(list, 0L)); }
public static long getMedian(List<Long> longList, long defaultValue) { if (longList == null || longList.isEmpty()) { return defaultValue; } int size = longList.size(); if (size % 2 == 0) { long a = longList.get(size / 2 - 1); long b = longList.get(size / 2); return (a + b) / 2L; } else { return longList.get((size - 1) / 2); } }
MathUtil { public static long getMedian(List<Long> longList, long defaultValue) { if (longList == null || longList.isEmpty()) { return defaultValue; } int size = longList.size(); if (size % 2 == 0) { long a = longList.get(size / 2 - 1); long b = longList.get(size / 2); return (a + b) / 2L; } else { return longList.get((size - 1) / 2); } } }
MathUtil { public static long getMedian(List<Long> longList, long defaultValue) { if (longList == null || longList.isEmpty()) { return defaultValue; } int size = longList.size(); if (size % 2 == 0) { long a = longList.get(size / 2 - 1); long b = longList.get(size / 2); return (a + b) / 2L; } else { return longList.get((size - 1) / 2); } } private  MathUtil(); }
MathUtil { public static long getMedian(List<Long> longList, long defaultValue) { if (longList == null || longList.isEmpty()) { return defaultValue; } int size = longList.size(); if (size % 2 == 0) { long a = longList.get(size / 2 - 1); long b = longList.get(size / 2); return (a + b) / 2L; } else { return longList.get((size - 1) / 2); } } private  MathUtil(); static long getSum(List<Long> longList); static long getAverage(List<Long> longList, long sum, long devByZero); static long getStandardDeviation(List<Long> longList, long average, long devByZero); static double getRate(long numerator, long denominator, double devByZeroValue); static long getMedian(List<Long> longList, long defaultValue); static long getPercentile(List<Long> longList, int p, long defaultValue); }
MathUtil { public static long getMedian(List<Long> longList, long defaultValue) { if (longList == null || longList.isEmpty()) { return defaultValue; } int size = longList.size(); if (size % 2 == 0) { long a = longList.get(size / 2 - 1); long b = longList.get(size / 2); return (a + b) / 2L; } else { return longList.get((size - 1) / 2); } } private  MathUtil(); static long getSum(List<Long> longList); static long getAverage(List<Long> longList, long sum, long devByZero); static long getStandardDeviation(List<Long> longList, long average, long devByZero); static double getRate(long numerator, long denominator, double devByZeroValue); static long getMedian(List<Long> longList, long defaultValue); static long getPercentile(List<Long> longList, int p, long defaultValue);  }