Skip to Content

Find Minimum element of Java Vector Example

  1. /*
  2.   Find Minimum element of Java Vector Example
  3.   This java example shows how to find a minimum element of Java Vector using
  4.   min method of Collections class.
  5. */
  6.  
  7. import java.util.Vector;
  8. import java.util.Collections;
  9.  
  10. public class FindMinimumOfVectorExample {
  11.  
  12.   public static void main(String[] args) {
  13.    
  14.     //create a Vector object
  15.     Vector v = new Vector();
  16.    
  17.     //Add elements to Vector
  18.     v.add(new Double("324.4324"));
  19.     v.add(new Double("345.3532"));
  20.     v.add(new Double("342.342"));
  21.     v.add(new Double("357.349"));
  22.     v.add(new Double("23.32453"));
  23.    
  24.     /*
  25.        To find minimum element of Java Vector use,
  26.        static Object min(Collection c) method of Collections class.
  27.      
  28.        This method returns the minimum element of Java Vector according to
  29.        its natural ordering.
  30.     */
  31.    
  32.     Object obj = Collections.min(v);
  33.    
  34.     System.out.println("Minimum Element of Java Vector is : " + obj);
  35.   }
  36. }
  37. /*
  38. Output would be
  39. Minimum Element of Java Vector is : 23.32453
  40. */

Advertisement: