Find maximum element of ArrayList with Java CollectionsJava 8Object Oriented ProgrammingProgramming Show
In order to compute maximum element of ArrayList with Java Collections, we use the Collections.max() method. The java.util.Collections.max() returns the maximum element of the given collection. All elements must be mutually comparable and implement the comparable interface. They shouldn’t throw a ClassCastException. Declaration −The Collections.max() method is declared as follows − public static <T extends Object & Comparable> T max(Collection c)where c is the collection object whose maximum is to be found. Let us see a program to find the maximum element of ArrayList with Java collections − 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 1. IntroductionIn this article, We'll learn how to find the maximum (max) value from ArrayList. Finding the max value from ArrayList from Collection API is done by running a loop over all the elements or can be found max value with the Collections.max() method. Collections.max(): Returns the maximum element of the given collection, according to the natural ordering of its elements. Example programs to find the max value from a list of wrapper objects and a list of custom or user-defined objects. How to find the Minimum Maximum value in ArrayList?There are a couple of ways to find minimum and maximum value in Java ArrayList. 1) Find Min Max value in ArrayList using Collections classYou can use min and max methods of Collections class.
This method returns the minimum element/value of the specified collection according to the natural ordering of the elements.
This method returns the maximum element/value of the specified collection according to the natural ordering of the elements. Example
Output
How to find an index of minimum maximum elements in Java ArrayList?If you want to find index of minimum or maximum element instead of value, you can use indexOf method of the ArrayList class.
This method returns the index of the specified element. If the element is not found in the ArrayList, it returns -1.
Output
2) Find Min Max value in ArrayList using for loopIf you want to find min max values without using the Collections class, you can use for loop to find the same as given below.
Output
This example is a part of theJava ArrayList tutorial with examples. Please let me know your views in the comments section below. Find maximum element of Java ArrayList : ArrayList«Collections Data Structure«Java
Find maximum element of Java ArrayList import java.util.ArrayList; import java.util.Collections; public class Main { public static void main(String[] args) { ArrayList<Integer> arrayList = new ArrayList<Integer>(); arrayList.add(new Integer("3")); arrayList.add(new Integer("1")); arrayList.add(new Integer("8")); arrayList.add(new Integer("3")); arrayList.add(new Integer("5")); Object obj = Collections.max(arrayList); System.out.println(obj); } } //8Related examples in the same category
Example
package com.w3spoint; import java.util.ArrayList; import java.util.Collections; import java.util.List; public class Test { public static void main(String a[]){ List<Integer> list = new ArrayList<Integer>(); list.add(32); list.add(61); list.add(73); list.add(10); list.add(9); list.add(86); System.out.println("Maximum element from the list: "+Collections.max(list)); } } |