1. OverviewThis tutorial is a quick intro on how to find the min and max values from a given list or collection with the powerful Stream API in Java 8. Show 1. Stream min() Method1.1. Method SyntaxThe method takes a non-interfering, stateless Comparator to compare elements of the stream. It returns an Optional describing the maximum element of the stream, or an empty Optional if the stream is empty. The min() method throws NullPointerException if the minimum element found is null. Optional<T> min(Comparator<? super T> comparator)1.2. Description
Find Min or Max DateTo get max or min date from a stream of dates, you can use Comparator.comparing( LocalDate::toEpochDay ) comparator. toEpochDay() function simply increment count of days for a date where day 0 is for 1970-01-01. Program Output. maxDate = 2017-10-30 minDate = 2017-09-13Use above program to find earliest date or latest date from a list of dates. 1. Find Max & Min Number in a ListFinding max and min number from a List Java
Output: Console Output
Finding the Minimum or Maximum Value in Java ArrayList
The minimum value is the one with the smallest value and the maximum value is the one with the largest value. The main task here is to find the minimum and maximum value from the ArrayList. Consider an example of an ArrayList, and we need to find the largest and the smallest element. Example: Input List: {10, 20, 8, 32, 21, 31}; Output: Maximum is: 32 Minimum is: 8Method 1: By iterating over ArrayList values
Java
Output Maximum is : 32 Minimum is : 8 Method 2: Using Collection class Methods We can use the min() and max() method of the collection class of Java. Collections in java is basically a framework that provides an architecture to accumulate and handle the group of objects. Java Collection framework provides many classes such as ArrayList, Vector, LinkedList, PriorityQueue, HashSet, LinkedHashSet, TreeSet. Approach:
Java
Output Array elements are : 10 20 5 8 Maximum is : 20 Minimum is : 5 Method 3: By sorting the ArrayList
Java
Output Elements of the array : 10 12 5 8 21 16 15 Arrays after sorting : 5 8 10 12 15 16 21 Maximum is : 21 Minimum is : 5 Article Tags : Java Java Programs Java-ArrayList Practice Tags : Java Min and Max in a List in Java
Given an unsorted list of integers, find maximum and minimum values in it. Input : list = [10, 4, 3, 2, 1, 20] Output : max = 20, min = 1 Input : list = [10, 400, 3, 2, 1, -1] Output : max = 400, min = -1
|