Java collection sum maximum minimum sample sharing _java

Source: Internet
Author: User

Copy Code code as follows:

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 Tool Class
*
* @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));
}

/**
* Take the minimum value
* @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));
}

/**
* Take the maximum value
* @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;
}

/**
* Sum
* @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));
}

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.