A Vector method addFirst() adds an element as the first element of this collection. Once an operation completed normally, a specified element appear as first position in this Vector.
1 default void addFirst(E e)
e : It is an element to be added at first position in this vector.
Exceptions:
1. | NullPointerException : if the specified element is null and this collection does not permit null elements |
2. | UnsupportedOperationException : if this collection implementation does not support this operation |
1 package iogyan.example;
2
3 import java.util.Arrays;
4 import java.util.Vector;
5
6 public class Main {
7
8 public static void main(String[] args) {
9
10 Vector<Integer> vector = new Vector<>();
11
12
13 vector.addAll(Arrays.asList(10, 20, 60, 30));
14 System.out.println("A vector elements : " + vector);
15
16
17 vector.addFirst(100);
18 System.out.println("An updated vector elements :" + vector);
19 }
20 }
In the above example, a Vector of type Integer created and elements added using addAll() called by passing a collection. A addFirst() method called by passing a value that added as first position in this vector and print updated vector elements.
1 A vector elements : [10, 20, 60, 30]
2 An updated vector elements :[100, 10, 20, 60, 30]
Vector method addFirst() with String
Vector method addFirst() with String
1 package iogyan.example;
2
3 import java.util.Arrays;
4 import java.util.Vector;
5
6 public class StringMain {
7
8 public static void main(String[] args) {
9
10 Vector<String> vector = new Vector<>(5);
11
12 vector.addAll(Arrays.asList("Welcome", "to", "IOGyan"));
13 System.out.println("A vector elements : " + vector);
14
15
16 vector.addFirst("thanks");
17 System.out.println("An updated vector elements :" + vector);
18 }
19 }
In the above example, a Vector of type String created and elements added using addAll() called by passing a collection. A addFirst() method called by passing a string that added as first position in this vector and print updated vector elements.
1 A vector elements : [Welcome, to, IOGyan]
2 An updated vector elements :[thanks, Welcome, to, IOGyan]
Vector method addFirst() with custom object
Vector method addFirst() with custom object
1 package iogyan.example;
2
3 import iogyan.model.Employee;
4
5 import java.util.Arrays;
6 import java.util.Vector;
7
8
9 public class EmployeeMain {
10
11 public static void main(String[] args) {
12
13 Employee emp1 = new Employee(1, "Emp1","Tech", 1000);
14 Employee emp2 = new Employee(2, "Emp2","Admin", 5000);
15 Employee emp3 = new Employee(3, "Emp3","Admin", 12000);
16
17 Vector<Employee> vector = new Vector<>();
18 vector.addAll(Arrays.asList(emp1, emp3));
19 System.out.println("An elements of vector : " + vector);
20
21
22 vector.addFirst(emp2);
23 System.out.println("An updated vector elements :" + vector);
24 }
25 }
In the above example, a Vector of type Employee created and elements added using addAll() called by passing a collection. A addFirst() method called by passing an employee object that added as first position in this vector and print updated vector elements by calling the Employee class toString() method.
1 An elements of vector : [Emp1 Tech 1000.0, Emp3 Admin 12000.0]
2 An updated vector elements :[Emp2 Admin 5000.0, Emp1 Tech 1000.0, Emp3 Admin 12000.0]
Employee class
1 package iogyan.model;
2
3 public class Employee implements Comparable<Employee> {
4 private final int id;
5 private final String name;
6 private final String dept;
7 private final double salary;
8
9 public Employee(int id, String name, String dept, double salary) {
10 this.id = id;
11 this.name = name;
12 this.dept = dept;
13 this.salary = salary;
14 }
15
16 public int getId() {
17 return this.id;
18 }
19
20 public String getName() {
21 return this.name;
22 }
23
24 public String getDept() {
25 return this.dept;
26 }
27
28 public double getSalary() {
29 return this.salary;
30 }
31
32 @Override
33 public int hashCode() {
34 return super.hashCode();
35 }
36
37 @Override
38 public boolean equals(Object obj) {
39 return this.id == ((Employee)obj).id;
40 }
41
42 @Override
43 public String toString() {
44 return this.getName() + " " + this.getDept() + " " + this.getSalary();
45 }
46
47 @Override
48 public int compareTo(Employee o) {
49 return this.getId() - o.getId();
50 }
51 }
Related options for your search