Java Program to Return the Largest Element in a List
Last Updated :
18 Jan, 2023
Given a List, find the largest element in it. There are multiple approaches to tackle this problem, such as iterating through the List or using various inbuilt functions.
Input : List = [5, 3, 234, 114, 154]
Output : 234
Input : List = {10, 20, 4}
Output : 20
Approach 1: Using a ForEach Loop
- Create List object and store multiple elements in it.
- Create a variable and initialize it with Integer.MIN_VALUE.
- Start iterating through the List using for each loop and compare each element with the variable.
- If the current element is greater than variable then update the variable.
- At the end of the iteration, print the variable.
Below is the implementation of the above approach:
Java
// Java Program to Return the Largest Element in a List
import java.util.Arrays;
import java.util.List;
public class Test {
public static void main(String[] args)
{
// List input
List<Integer> arrayList
= Arrays.asList(5, 3, 15, 234, 114, 1540);
// Create maxValue variable and initialize with
// minimum value
int maxValue = Integer.MIN_VALUE;
// Check maximum element using for loop
for (Integer integer : arrayList) {
if (integer > maxValue)
maxValue = integer;
}
System.out.println("The maximum value is "
+ maxValue);
}
}
OutputThe maximum value is 1540
Approach 2: Using Iterators
- Create List object and store multiple elements in it.
- Create a variable and initialize it with Integer.MIN_VALUE.
- Start iterating through the List using List iterator and compare each element with the variable.
- If the current element is greater than variable then update the variable.
- At the end of the iteration, print the variable.
Below is the implementation of the above approach:
Java
// Java Program to Return the Largest Element in a List
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
public class Test {
public static void main(String[] args)
{
// List as input
List<Integer> arrayList
= Arrays.asList(5, 3, 15, 234, 114, 1540);
// List iterator
Iterator listIterator = arrayList.iterator();
// Create maxValue variable and initialize with
// minimum value
Integer maxValue = Integer.MIN_VALUE;
// Iterate list
while (listIterator.hasNext()) {
Integer integer = (Integer)listIterator.next();
// If value is greater then update maxValue
if (integer > maxValue)
maxValue = integer;
}
System.out.println("The maximum value is "
+ maxValue);
}
}
OutputThe maximum value is 1540
Approach 3: Using Indexing
- Create List object and store multiple elements in it.
- Create a variable and initialize it with Integer.MIN_VALUE.
- Start iterating through the List and compare each element with the variable.
- If the current element is greater than variable then update the variable.
- At the end of the iteration, print the variable.
Below is the implementation of the above approach:
Java
// Java Program to Return the Largest Element in a List
import java.util.Arrays;
import java.util.List;
public class Test {
public static void main(String[] args)
{
// List as input
List<Integer> arrayList
= Arrays.asList(5, 3, 15, 234, 114, 1540);
// Create maxValue variable and initialize with 0
Integer maxValue = 0;
// Iterate List using for each loop
for (int i = 0; i < arrayList.size(); i++) {
// If element is greater the update maxValue
if (arrayList.get(i) > maxValue)
maxValue = arrayList.get(i);
}
System.out.println("The maximum value is "
+ maxValue);
}
}
OutputThe maximum value is 1540
Approach 4: Using JDK 8
Java
import java.util.Arrays;
import java.util.List;
public class Test {
public static void main(String[] args)
{
// List as input
List<Integer> arrayList
= Arrays.asList(5, 3, 15, 234, 114, 1540);
// Initialize with inbuilt max function
int maxValue = arrayList.stream()
.max(Integer::compareTo)
.get();
System.out.println("The maximum value is "
+ maxValue);
}
}
OutputThe maximum value is 1540
Similar Reads
Java Program to Find the K'th largest element in a stream Given an infinite stream of integers, find the k'th largest element at any point of time.Example: Input: stream[] = {10, 20, 11, 70, 50, 40, 100, 5, ...} k = 3 Output: {_, _, 10, 11, 20, 40, 50, 50, ...} Extra space allowed is O(k). Recommended: Please solve it on "PRACTICE" first, before moving on
4 min read
Java Program to Find Largest Element in an Array Finding the largest element in an array is a common programming task. There are multiple approaches to solve it. In this article, we will explore four practical approaches one by one to solve this in Java.Example Input/Output:Input: arr = { 1, 2, 3, 4, 5}Output: 5Input: arr = { 10, 3, 5, 7, 2, 12}Ou
4 min read
Java Program To Find Largest Between Three Numbers Using Ternary Operator Java offers a lot of Operators and one such operator is the Ternary Operator. It is a linear replacement for an if-else statement. Thus, it is a very useful operator and it uses less space in comparison to an if-else statement. Ternary Operator Syntax : variable = condition ? expression1 : expressio
2 min read
Java Program to Find the Largest of three Numbers Problem Statement: Given three numbers x, y, and z of which aim is to get the largest among these three numbers.Example:Â Input: x = 7, y = 20, z = 56Output: 56 // value stored in variable zFlowchart For Largest of 3 numbers:Algorithm to find the largest of three numbers:1. Start2. Read the three num
4 min read
Java Program to Get the Maximum Element From a Vector Prerequisite: Vectors in Java Why We Use Vector? Till now, we have learned two ways for declaring either with a fixed size of array or size enter as per the demand of the user according to which array is allocated in memory. int Array_name[Fixed_size] ; int array_name[variable_size] ; Both ways we l
4 min read