How do I get the minimum or maximum value between two numbers?

The code below show you how to use the Math.min() and Math.max() method call the get the minimum and maximum value between two numbers. As other method in the Math class these methods also overloaded to accept many types of primitive data.

package org.kodejava.math;

public class GetMinMaxValueExample {
    public static void main(String[] args) {

        Double value1 = 100.0D;
        Double value2 = 200.0D;

        double max1 = Math.max(value1, value2);
        double min1 = Math.min(value1, value2);

        float max2 = Math.max(value1.floatValue(), value2.floatValue());
        float min2 = Math.min(value1.floatValue(), value2.floatValue());

        int max3 = Math.max(value1.intValue(), value2.intValue());
        int min3 = Math.min(value1.intValue(), value2.intValue());

        long max4 = Math.max(value1.longValue(), value2.longValue());
        long min4 = Math.min(value1.longValue(), value2.longValue());

        System.out.println("Max value in double: " + max1);
        System.out.println("Min value in double: " + min1);
        System.out.println("Max value in float : " + max2);
        System.out.println("Min value in float : " + min2);
        System.out.println("Max value in int   : " + max3);
        System.out.println("Min value in int   : " + min3);
        System.out.println("Max value in long  : " + max4);
        System.out.println("Min value in long  : " + min4);
    }
}

The result of the above program are:

Max value in double: 200.0
Min value in double: 100.0
Max value in float : 200.0
Min value in float : 100.0
Max value in int   : 200
Min value in int   : 100
Max value in long  : 200
Min value in long  : 100

How do I use min, max, avg and sum Projections?

The code below demonstration the use of Projections.min(), Projections.max(), Projections.avg() and Projections.sum().

package org.kodejava.hibernate;

import org.hibernate.SessionFactory;
import org.hibernate.Session;
import org.hibernate.HibernateException;
import org.hibernate.Criteria;
import org.hibernate.cfg.Configuration;
import org.hibernate.criterion.Projections;
import org.kodejava.hibernate.model.Track;

public class MinMaxAvgSumProjectionsDemo {
    public static Session getSession() throws HibernateException {
        String cfg = "hibernate.cfg.xml";
        SessionFactory sessionFactory = new Configuration().configure(cfg)
                .buildSessionFactory();
        return sessionFactory.openSession();
    }

    public static void main(String[] args) {
        try (Session session = getSession()) {
            Criteria criteria = session.createCriteria(Track.class)
                    .setProjection(Projections.max("duration"));
            Integer maxDuration = (Integer) criteria.uniqueResult();
            System.out.println("Max Track Duration = " + maxDuration);

            criteria.setProjection(Projections.min("duration"));
            Integer minDuration = (Integer) criteria.uniqueResult();
            System.out.println("Min Track Duration = " + minDuration);

            criteria.setProjection(Projections.avg("duration"));
            Double avgDuration = (Double) criteria.uniqueResult();
            System.out.println("Avg Track Duration = " + avgDuration);

            criteria.setProjection(Projections.sum("duration"));
            Long totalDuration = (Long) criteria.uniqueResult();
            System.out.println("Total Track Duration = " + totalDuration);
        }
    }
}

Maven Dependencies

<dependencies>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>5.6.9.Final</version>
    </dependency>
    <dependency>
        <groupId>com.mysql</groupId>
        <artifactId>mysql-connector-j</artifactId>
        <version>8.0.33</version>
    </dependency>
</dependencies>

Maven Central Maven Central

How do I know the minimum and maximum number in an array?

package org.kodejava.util;

import java.util.Arrays;
import java.util.Collections;

public class ArrayMinMax {
    public static void main(String[] args) {
        // Creates an array of integer numbers in it.
        Integer[] numbers = {8, 2, 6, 7, 0, 1, 4, 9, 5, 3};

        // To get the minimum or maximum value from the array we can
        // use the Collections.min() and Collections.max() methods.
        // But as this method requires a list type of data we need
        // to convert the array to list first.
        int min = Collections.min(Arrays.asList(numbers));
        int max = Collections.max(Arrays.asList(numbers));

        // Viola! we get the minimum and the maximum value from the
        // array.
        System.out.println("Min number: " + min);
        System.out.println("Max number: " + max);
    }
}

And here are the results:

Min number: 0
Max number: 9