国产探花免费观看_亚洲丰满少妇自慰呻吟_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ā)表
主站蜘蛛池模板: 安岳县| 香河县| 屏山县| 佳木斯市| 平阳县| 石城县| 太湖县| 赣榆县| 青铜峡市| 南宁市| 四川省| 华池县| 淮南市| 尼玛县| 日土县| 泽普县| 钟山县| 永新县| 安国市| 邛崃市| 普洱| 蕉岭县| 鄂托克前旗| 绥江县| 泰兴市| 荆州市| 南岸区| 交城县| 永兴县| 湘潭市| 夹江县| 沂南县| 横峰县| 绥中县| 武清区| 甘德县| 通辽市| 龙南县| 惠安县| 惠水县| 陕西省|