The push() Function in Java
-
Use the
stack.push()
Function in Java -
Use the
LinkedList.push()
Function in Java -
Use the
ArrayList.add()
Function in Java -
Use a User-Defined
push()
Function for Arrays in Java

If we talk about the basic definition of push()
function, it will be a function that inserts an element to the end of some structure. This function is associated with Last In First Out structures like stacks, linked lists, and more. Java doesn’t have a push()
function for arrays in it.
Since the push()
function is not associated with arrays, we can use different data structures that already support this function.
This article will discuss the push()
function in Java.
Use the stack.push()
Function in Java
We can use the push()
function from the stack class. For this, we will be importing the java.util
package for using the stack class.
With this function, we can add elements to the end of the stack. The stack can be of some desired type.
We will be creating the stack method of the string type. We will add the elements one by one using the push()
function.
See the code below.
import java.util.*;
public class Push_Example
{
public static void main(String args[])
{
Stack<String> st = new Stack<String>();
st.push("Ram");
st.push("shayam");
st.push("sharma");
System.out.println("Stack Elements: " + st);
st.push("monu");
st.push("sonu");
// Stack after adding new elements
System.out.println("Stack after using the push function: " + st);
}
}
Output:
Stack Elements: [Ram, shayam, sharma]
Stack after using the push function: [Ram, shayam, sharma, monu, sonu]
Use the LinkedList.push()
Function in Java
In Java, the push()
function is associated with Linked Lists also. For this also, we will be importing the java.util
package.
We can define a new linked list using the LinkedList
method. Now, we can add elements one by one using the push()
function.
For example,
import java.util.*;
public class Push_Example
{
public static void main(String args[])
{
LinkedList<Integer> li = new LinkedList<>();
li.push(10);
li.push(11);
li.push(12);
li.push(13);
li.push(14);
System.out.println("LinkedList Elements: " + li);
// Push new elements
li.push(100);
li.push(101);
System.out.println("LinkedList after using the push function: " + li);
}
}
Output:
LinkedList Elements: [14, 13, 12, 11, 10]
LinkedList after using the push function: [101, 100, 14, 13, 12, 11, 10]
Use the ArrayList.add()
Function in Java
For ArrayLists, we can use the add()
function to emulate the push()
function. This function will add an element to the end of the given ArrayList.
For example,
import java.util.*;
public class Push_Example
{
public static void main(String args[])
{
ArrayList<Integer> li = new ArrayList<>();
li.add(10);
li.add(11);
li.add(12);
li.add(13);
li.add(14);
System.out.println("ArrayList Elements: " + li);
// Push new elements
li.add(100);
li.add(101);
System.out.println("ArrayList after using the add function: " + li);
}
}
Output:
ArrayList Elements: [10, 11, 12, 13, 14]
ArrayList after using the add function: [10, 11, 12, 13, 14, 100, 101]
Use a User-Defined push()
Function for Arrays in Java
There is no push()
function for arrays in Java. However, we can create a function to emulate this. This function will copy the array contents to a new array of longer length and add the new element to this array.
See the code below.
import java.util.*;
public class Push_Arr
{
private static String[] push(String[] array, String push) {
String[] longer = new String[array.length + 1];
for (int i = 0; i < array.length; i++)
longer[i] = array[i];
longer[array.length] = push;
return longer;
}
public static void main(String args[])
{
String[] arr = new String[]{"a", "b", "c"};
arr = Push_Arr.push(arr,"d");
System.out.println("Array after using the push function: ");
for(int i=0;i<arr.length;i++)
System.out.println(arr[i]);
}
}
Output:
ArrayList after using the add function:
a
b
c
d
This method gives the desired output but is rarely used because it runs a loop to copy the array elements, so it will take a lot of time and memory when dealing with larger arrays.
Related Article - Java Array
- Concatenate Two Arrays in Java
- Convert Byte Array in Hex String in Java
- Remove Duplicates From Array in Java
- Count Repeated Elements in an Array in Java
- Natural Ordering in Java
- Slice an Array in Java