Implementing Sorted Vector in Java Last Updated : 14 Feb, 2023 Comments Improve Suggest changes Like Article Like Report Vector is a class that implements the List interface. It is a type of dynamic array that means the size of a vector can be grown or shrink during the execution of the program. The initial size of the vector is 10 and if we insert more than 10 elements then the size of the vector is increased by 100%, or we can say that it doubles the size. Like, initially the size of the vector is 10, and if we insert more than 10 elements the size of the vector is 20 and after inserting 20 elements in the vector, the size of the vector will be 40. The Following diagram of the collection API In which there is an Interface name Collection which is extended by Set, List, and Queue interfaces, and List interface is extended by 'Vector' class. Approaches: Implementation for Descending Order Sorting For Implementing SortedVector Extend the Vector. (Demonstrated here)Extend ArrayList.Extend LinkedListDirectly implement the List interface. Algorithm: Extend vector class for implementation of SortedVector.A private data member for storing data in SortedVector.For creating objects of SortedVector, we have to define all constructors.For adding elements in SortedVector, we created a method 'addingElements'.Add elements.If the size of the data member is less than or equal to 1 then there is no need for sorting.If the size is greater than 1 then sort it.If the user entered strings then raising an error.In the end, add elements in a sorted vector.Clear SortedVector and after that add all data in sorted order. Below is the implementation of the above approach: Java // We are going to implement sorted vector // from inbuilt Vector class. import java.util.*; class SortedVector extends Vector<Object> { private final Vector<Object> list = new Vector<>(); private final Comparator<? super Object> comparator = Collections.reverseOrder(); public SortedVector() {} // method for adding elements in data // member of 'SortedVector' public void addingElement(Object obj) { list.add(obj); // if list size is less than or equal to one // element then there is no need of sorting. // here we are sorting elements if (list.size() > 1) { // If we are getting character as input then // Exceptions occurs in 'Collections.sort'. // So, we are type casting character to int. // and sorting character as integer. try { list.sort(comparator); } catch (Exception e) { Object recent = list.lastElement(); list.removeElementAt(list.size() - 1); int val; // If we are getting string as input then // we are handling this exception here. try { val = (char)recent; list.add(val); list.sort(comparator); } catch (Exception e1) { System.out.println( "You entered Strings"); } } } addingElementsInSortedVector(); } // adding element in object of 'SortedVector' private void addingElementsInSortedVector() { // clear all values of "SortedVector's" object clear(); // adding values in object of 'SortedVector' addAll(list); } } // ours code starts from here public class Main { public static void main(String[] args) { // creating an object of Sorted vector SortedVector sorted_vector = new SortedVector(); // we have a method for adding object in //'SortedVector' class called 'addingElement'. // adding element in object of 'SortedVector' sorted_vector.addingElement(1); System.out.println("After 1st element Insertion:"); for (Object i : sorted_vector) { System.out.println(i); } sorted_vector.addingElement(99); System.out.println("After 2nd element Insertion:"); for (Object i : sorted_vector) { System.out.println(i); } System.out.println("After 3rd element Insertion:"); sorted_vector.addingElement(2); for (Object i : sorted_vector) { System.out.println(i); } sorted_vector.addingElement(0); System.out.println("After 4th element Insertion:"); for (Object i : sorted_vector) { System.out.println(i); } } } OutputAfter 1st element Insertion: 1 After 2nd element Insertion: 99 1 After 3rd element Insertion: 99 2 1 After 4th element Insertion: 99 2 1 0 Time Complexity: O(n2 log n) Comment More infoAdvertise with us Next Article Implementing Sorted Vector in Java depresseddeadpool Follow Improve Article Tags : Java Java Programs Java-Vector Practice Tags : Java Similar Reads Implementing Sparse Vector in Java A vector or arraylist is a one-dimensional array of elements. The elements of a Sparse Vector have mostly zero values. It is inefficient to use a one-dimensional array to store a sparse vector. It is also inefficient to add elements whose values are zero in forming sums of sparse vectors. We convert 5 min read Replacing Element in Java Vector To replace an element in Java Vector, set() method of java.util.Vector class can be used. The set() method takes two parameters-the indexes of the element which has to be replaced and the new element. The index of a Vector is zero-based. So, to replace the first element, 0 should be the index passed 2 min read Iterate Over Vector Elements in Java Vector is like the dynamic array which can grow or shrink its size. Unlike array, we can store n-number of elements in it as there is no size limit. We can iterate over vector by the following ways: Simple for-loopEnhanced for-loopIteratorsEnumeration interface Method 1: Simple for-loop The idea is 4 min read Finding Minimum Element of Java Vector Vector implements a dynamic array that means it can grow or shrink as required. Like an array, it contains components that can be accessed using an integer index. We know two ways for declaring array i.e. either with a fixed size of array or size enter as per the demand of the user according to whic 3 min read Finding Maximum Element of Java Vector Vector implements a dynamic array that means it can grow or shrink as required. Like an array, it contains components that can be accessed using an integer index. We know two ways for declaring array i.e. either with a fixed size of array or size enter as per the demand of the user according to whic 3 min read Get Enumeration over Java Vector In java, the vector is a typical dynamic array whose size can increase or decrease. While in array the size cannot be changed after declaration. We have to include file import java.util.Vector to use Vector and store values in it. Also, import java.util.Enumeration to use enumeration. Approach 1: Co 3 min read Java Program to Search an Element in Vector A vector in Java is a dynamic array that can be resized as needed. It is synchronized, which means it is safe in multi-threaded programs. To find an element in a Vector we have to loop through its elements to find a match. In this article, we will learn how to search for a component in a Vector usin 3 min read Sort Java Vector in Descending Order Using Comparator The Vector class implements a growable array of objects. Vectors basically fall in legacy classes but now it is fully compatible with collections. It is found in java.util package and implements the List interface, so we can use all the methods of the List interface. There are two types of Sorting t 3 min read Searching Elements in Vector Using Index in Java Vector implements a dynamic array that means it can grow or shrink as required. Like an array, it contains components that can be accessed using an integer index. An element of a Vector can be searched using an index with different methods. Suppose if the element is not present in the Vector then th 4 min read How to Sort Vector Elements using Comparable Interface in Java? Vector is a child interface of collection. If we want to represent a group of the individual objects as a single entity where duplicates are allowed and insertion order must be preserved then we should go for vector. It is a resizable or growable array. It implements a Serializable, Cloneable, and R 5 min read Like