Find Minimum Value From ArrayList In Java | Java @ Desk
Home » , » Find Minimum Value From ArrayList In Java

Find Minimum Value From ArrayList In Java



Find Minimum Value From ArrayList In Java

There are three ways to get Minimum value from an ArrayList:

1) Collections.min() from java.util.Collections
2) Using Iteration
3) Using Collections.sort() from java.util.Collections

Click to see Sort ArrayList Using Comparator In Java

Sample Implementation:

package test;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class FindMinIntegerArrayList {

 public static void main(String args[]) {
  List<Integer> integers = new ArrayList<Integer>();

  integers.add(1000);
  integers.add(895);
  integers.add(640);
  integers.add(1089);

  // Using java.util.Collections
  System.out.println("Minimum Element : " + Collections.min(integers));

  // Using Iteration
  Integer min = Integer.MAX_VALUE;
  for (Integer integer : integers) {
   if (integer < min) {
    min = integer;
   }
  }
  System.out.println("Minimum Element after iteration : " + min);

  // Using Sort Technique of java.util.Collections
  Collections.sort(integers);
  System.out.println("Minimum Element After Sorting - "
    + integers.get(0));

 }
}




0 comments:

Post a Comment