- Java Programming Examples
- Example - Home
- Example - Environment
- Example - Strings
- Example - Arrays
- Example - Date & Time
- Example - Methods
- Example - Files
- Example - Directories
- Example - Exceptions
- Example - Data Structure
- Example - Collections
- Example - Networking
- Example - Threading
- Example - Applets
- Example - Simple GUI
- Example - JDBC
- Example - Regular Exp
- Example - Apache PDF Box
- Example - Apache POI PPT
- Example - Apache POI Excel
- Example - Apache POI Word
- Example - OpenCV
- Example - Apache Tika
- Example - iText
- Java Tutorial
- Java - Tutorial
- Java Useful Resources
- Java - Quick Guide
- Java - Useful Resources
How to get the maximum element from a vector in Java
Problem Description
How to get the maximum element from a vector?
Solution
Following example demonstrates how to get the maximum element of a vector by using v.add() method of Vector class and Collections.max() method of Collection class.
import java.util.Collections; import java.util.Vector; public class Main { public static void main(String[] args) { Vector<Double> v = new Vector<Double>(); v.add(new Double("3.4324")); v.add(new Double("3.3532")); v.add(new Double("3.342")); v.add(new Double("3.349")); v.add(new Double("2.3")); Object obj = Collections.max(v); System.out.println("The max element is:"+obj); } }
Result
The above code sample will produce the following result.
The max element is: 3.4324
The following is an another example to get the maximum element of a vector by using v.add() method of Vector class and Collections.max() method of Collection class.
import java.util.Vector; import java.util.Collections; public class Demo { public static void main(String[] args) { Vector vec = new Vector(); vec.add(new Double("24.42")); vec.add(new Double("45.32")); vec.add(new Double("42.42")); vec.add(new Double("57.39")); vec.add(new Double("23.34")); Object object1 = Collections.max(vec); System.out.println("Maximum Element is : " + object1); } }
Result
The above code sample will produce the following result.
Maximum Element is : 57.39
java_data_structure.htm
Advertisements