国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Java > 正文

java集合求和最大值最小值示例分享

2019-11-26 15:45:21
字體:
供稿:網(wǎng)友

復(fù)制代碼 代碼如下:

package com.happyelements.athene.game.util;

import static com.google.common.base.Preconditions.checkNotNull;

import java.util.Collection;

import com.google.common.collect.Lists;

/**
 * Math工具類
 * 
 * @version 1.0
 * @since 1.0
 */
public class MathUtil {

 /**
  * @see MathUtil#min(Collection)
  * @param ts
  * @return
  */
 public static <T extends Comparable<T>> T min(T... ts) {
  return min(Lists.newArrayList(ts));
 }

 /**
  * 取最小值
  * @param values
  * @throws NullPointerException if (values == null || values.contain(null))
  * @return
  */
 public static <T extends Comparable<T>> T min(Collection<T> values) {
  checkNotNull(values);
  T min = null;

  for (T t : values) {
   checkNotNull(t);
   if (min == null) {
    min = t;
   } else {
    min = min.compareTo(t) < 0 ? min : t;
   }
  }
  return min;
 }

 /**
  * @see MathUtil#max(Collection)
  * @param ts
  * @return
  */
 public static <T extends Comparable<T>> T max(T... ts) {
  return max(Lists.newArrayList(ts));
 }

 /**
  * 取最大值
  * @param values
  * @throws NullPointerException if (values == null || values.contain(null))
  * @return
  */
 public static <T extends Comparable<T>> T max(Collection<T> values) {
  checkNotNull(values);
  T max = null;

  for (T t : values) {
   checkNotNull(t);
   if (max == null) {
    max = t;
   } else {
    max = max.compareTo(t) > 0 ? max : t;
   }
  }
  return max;
 }

 /**
  * 求和
  * @param values
  * @throws NullPointerException if (values == null || values.contain(null))
  * @return
  */
 public static Integer sum(Collection<Integer> values) {
  checkNotNull(values);
  int sum = 0;
  for (Integer integer : values) {
   checkNotNull(integer);
   sum += integer;
  }
  return sum;
 }

 /**
  * @see MathUtil#sum(Collection)
  * @param ts
  * @return
  */
 public static Integer sum(Integer... ts) {
  return sum(Lists.newArrayList(ts));
 }

}

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 开鲁县| 马边| 延安市| 炎陵县| 雷州市| 公安县| 来宾市| 平南县| 云安县| 兴化市| 开平市| 贡觉县| 蒲城县| 梨树县| 涟水县| 四子王旗| 昂仁县| 墨玉县| 礼泉县| 沁水县| 喀什市| 潮州市| 乌拉特中旗| 吉隆县| 达拉特旗| 德州市| 汉阴县| 双城市| 苗栗市| 临西县| 栾川县| 寿光市| 云阳县| 龙川县| 延川县| 新干县| 广南县| 满城县| 芷江| 柳江县| 贵德县|