Java LinkedList get max and min value

Introduction

To get Max and min value from LinkedList in Java

System.out.println("Minimum: " + Collections.min(ll));      
System.out.println("Maximum: " + Collections.max(ll));
    

Full source

import java.util.Collections;
import java.util.LinkedList;

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

    // Create and initialize linked list.
    LinkedList<Integer> ll = new LinkedList<Integer>();
    ll.add(-8);/*from   w  w w .  j a va  2 s.  com*/
    ll.add(321);
    ll.add(20);
    ll.add(-20);
    ll.add(123);
    ll.add(8);

    System.out.println("Minimum: " + Collections.min(ll));      
    System.out.println("Maximum: " + Collections.max(ll)); 
  }
}



PreviousNext

Related