beginnersbook.com

  • Home
  • All Tutorials
    • Learn Servlet
    • Learn JSP
    • Learn JSTL
    • Learn C
    • Learn C++
    • Learn MongoDB
    • Learn XML
    • Learn Python
    • Learn Perl
    • Learn Kotlin
  • Core Java
  • OOPs
  • Collections
  • Java I/O
  • JSON
  • DBMS

How to Sort Vector using Collections.sort in java – Example

By Chaitanya Singh | Filed Under: Java Collections

Vector maintains the insertion order which means it displays the elements in the same order, in which they got added to the Vector. In this example, we will see how to sort Vector elements in ascending order by using Collections.sort(). The Steps are as follows:
1) Create a Vector object
2) Add elements to the Vector using add(Element e) method
3) Sort it using Collections.sort(Vector object)
4) Display the sorted elements list.

import java.util.Collections;
import java.util.Vector;
public class SortingVectorExample {
 public static void main(String[] args) {

    // Create a Vector
    Vector<String> vector = new Vector<String>();
 
    //Add elements to Vector
    vector.add("Walter");
    vector.add("Anna");
    vector.add("Hank");
    vector.add("Flynn");
    vector.add("Tom");
 
    // By Default Vector maintains the insertion order
    System.out.println("Vector elements before sorting: ");
    for(int i=0; i < vector.size(); i++){
       //get(i) method fetches the element from index i
       System.out.println(vector.get(i));
    }

    // Collection.sort() sorts the collection in ascending order
    Collections.sort(vector);
 
    //Display Vector elements after sorting using Collection.sort
    System.out.println("Vector elements after sorting: :");
    for(int i=0; i < vector.size(); i++){
       System.out.println(vector.get(i));
    }
 } 
}

Output:

Vector elements before sorting: 
Walter
Anna
Hank
Flynn
Tom
Vector elements after sorting: :
Anna
Flynn
Hank
Tom
Walter

Enjoyed this post? Try these related posts

  1. How to get the last element of Arraylist?
  2. Java – Convert Vector to ArrayList example
  3. Java – LinkedList ListIterator example
  4. Java – Add element at specific index in LinkedList example
  5. Java – Check if a particular value exists in HashMap example
  6. How to sort ArrayList in descending order in Java

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Java Tutorial

  • Java Tutorial
  • OOPs Concepts

Java Collections

  • ArrayList
  • LinkedList
  • ArrayList vs LinkedList
  • Vector
  • ArrayList vs Vector
  • HashMap
  • TreeMap
  • LinkedHashMap
  • HashSet
  • TreeSet
  • LinkedHashSet
  • Hashtable
  • HashMap vs Hashtable
  • Queue
  • PriorityQueue
  • Deque & ArrayDeque
  • Iterator
  • ListIterator
  • Comparable Interface
  • Comparator Interface
  • Java Collections Interview Q

MORE ...

  • Java String
  • Exception handling
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations

Copyright © 2012 – 2019 BeginnersBook . Privacy Policy . Sitemap