Skip to Content

Find maxmimum element of Java Vector Example

  1. /*
  2.   Find maxmimum element of Java Vector Example
  3.   This java example shows how to find a maximum element of Java Vector using
  4.   max method of Collections class.
  5. */
  6.  
  7. import java.util.Vector;
  8. import java.util.Collections;
  9.  
  10. public class FindMaximumOfVectorExample {
  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 maximum element of Java Vector use,
  26.        static Object max(Collection c) method of Collections class.
  27.      
  28.        This method returns the maximum element of Java Vector according to
  29.        its natural ordering.
  30.     */
  31.    
  32.     Object obj = Collections.max(v);
  33.    
  34.     System.out.println("Maximum Element of Java Vector is : " + obj);
  35.   }
  36. }
  37. /*
  38. Output would be
  39. Maximum Element of Java Vector is : 357.349
  40. */

Advertisement: