CodesJava

Easy learning with example program codes

How to get max element from the given list?


The java.util.Collections class consists exclusively of static methods that operate on or return collections. It contains polymorphic algorithms that operate on collections, “wrappers”, which return a new collection backed by a specified collection, and a few other odds and ends.

Collections.max() method will returns the maximum element of the given collection, according to the natural ordering of its elements.
Syntax:public static int max(Collection coll)

Example

package com.codesjava;
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));
    }
}

Output

Maximum element from the list: 86

Java Collections class examples

Sign Up/ Sign In
Ask a Question


Copyright © 2018 CodesJava DMCA.com Protection Status SiteMap Reference: Java Wiki