A ArrayDeque method addLast() inserts the specified element at the end or at the tail of this deque.
1 public void addLast(E e)
e : It is an element to be inserted at the end of this queue.
Exception:
NullPointerException : if the specified element is null
ArrayDeque method addLast()
1 package com.java;
2
3 import java.util.ArrayDeque;
4 import java.util.Arrays;
5
6 public class Main {
7
8 public static void main(String[] args) {
9
10 ArrayDeque<Integer> arrayDeque = new ArrayDeque<>();
11 arrayDeque.addAll(Arrays.asList(10, 30, 20, 80, 70));
12 System.out.println("ArrayDeque elements : " + arrayDeque);
13
14
15 arrayDeque.addLast(100);
16 System.out.println("ArrayDeque elements : " + arrayDeque);
17 }
18 }
In the above example, a ArrayDequeue of type Integer created and elements added using addAll() method by passing a collection. A addLast() method called by passing a value that inserts specified value at the end (tail) of this deque. An updated deque print that includes newly added value at the end of this deque.
1 ArrayDeque elements : [10, 30, 20, 80, 70]
2 ArrayDeque elements : [10, 30, 20, 80, 70, 100]
ArrayDeque method addLast() with String
ArrayDeque method addLast() with String
1 package com.java;
2
3 import java.util.ArrayDeque;
4 import java.util.Arrays;
5
6 public class StringExp {
7
8 public static void main(String[] args) {
9
10 ArrayDeque<String> arrayDeque = new ArrayDeque<>(5);
11 arrayDeque.addAll(Arrays.asList("Welcome", "to", "IOGyan"));
12 System.out.println("ArrayDeque elements : " + arrayDeque);
13
14
15 arrayDeque.addLast("thanks");
16 System.out.println("ArrayDeque elements : " + arrayDeque);
17 }
18 }
In the above example, a ArrayDequeue of type String created and elements added using addAll() method by passing a collection. A addLast() method called by passing a string that inserts specified string at the end (tail) of this deque. An updated deque print that includes newly added string at the end of this deque.
1 ArrayDeque elements : [Welcome, to, IOGyan]
2 ArrayDeque elements : [Welcome, to, IOGyan, thanks]
ArrayDeque method addLast() with custom object
ArrayDeque method addLast() with custom object
1 package com.java;
2
3 import java.util.ArrayDeque;
4 import java.util.Arrays;
5
6 public class Example {
7
8 public static void main(String[] args) {
9
10 Employee emp1 = new Employee(100, "Emp1", "Tech", 12000);
11 Employee emp2 = new Employee(200, "Emp2", "Tech", 31000);
12 Employee emp3 = new Employee(300,"Emp3", "Admin", 21000);
13
14 ArrayDeque<Employee> arrayDeque = new ArrayDeque<>(5);
15 arrayDeque.addAll(Arrays.asList(emp1, emp3));
16 System.out.println("ArrayDeque elements : " + arrayDeque);
17
18
19 arrayDeque.addLast(emp2);
20 System.out.println("ArrayDeque elements : " + arrayDeque);
21 }
22 }
In the above example, a ArrayDequeue of type Employee created and elements added using addAll() method by passing a collection. A addLast() method called by passing an employee object that inserts specified object at the end (tail) of this deque. An updated deque print that includes newly added string at the end of this deque.
1 ArrayDeque elements : [Emp1 Tech 12000, Emp3 Admin 21000]
2 ArrayDeque elements : [Emp1 Tech 12000, Emp3 Admin 21000, Emp2 Tech 31000]
Employee class
1 package com.java;
2
3 public class Employee implements Comparable<Employee> {
4
5 private final int id;
6 private final String name;
7 private final String dept;
8 private final Long salary;
9
10 public Employee(int id, String name, String dept, long salary) {
11 this.id = id;
12 this.name = name;
13 this.dept = dept;
14 this.salary = salary;
15 }
16 public int getId() {
17 return this.id;
18 }
19
20 public String getDept() {
21 return this.dept;
22 }
23
24 public String getName() {
25 return this.name;
26 }
27
28 public Long getSalary() {
29 return this.salary;
30 }
31
32 @Override
33 public int hashCode() {
34 return this.getId() + this.getName().hashCode();
35 }
36
37 @Override
38 public boolean equals(Object obj) {
39 return this.id == ((Employee)obj).id;
40 }
41
42 public String toString() {
43 return this.name + " " + this.dept + " " + this.salary;
44 }
45
46 @Override
47 public int compareTo(Employee o) {
48 return this.getId() - o.getId();
49 }
50 }
Related options for your search