Online Java Compiler By
JavaTpoint.com
import java.util.*; public class VectorSortExample2 { public static void main(String arg[]) { //Create an empty vector Vector < String > vec = new Vector < String > (); //Add elements in the vector vec.add("White"); vec.add("Green"); vec.add("Black"); vec.add("Orange"); System.out.println("The vector elements are: "); //Display the vector elements for (String colors : vec) { System.out.println(" "+colors); } //Sorting the vector Collections.sort(vec); //Displaying the vector elements after sort() method System.out.println("The vector elements after sort() method are: "); for (String colors : vec) { System.out.println(" "+colors); } } }
Output