Java.util.LinkedList.offer(), offerFirst(), offerLast() in Java
Last Updated :
04 Oct, 2021
Linked list also has a function that does the work of flexible addition of elements and helps addition both at front and back of the list, these functions literally “offer” the facility and named offer(). Three types are available and are discussed in this very article below.
1. offer(E e) : This method adds the specified element as the tail (last element) of this list.
Declaration :
public boolean offer(E e)
Parameters :
e: the element to add
Return Value :
This method returns true
Java
import java.util.*;
public class LinkedListOfferLast {
public static void main(String[] args)
{
LinkedList<Integer> list = new LinkedList<Integer>();
LinkedList prioList = new LinkedList();
list.add( 12 );
list.add( 4 );
list.add( 8 );
list.add( 10 );
list.add( 3 );
list.add( 15 );
int thres = 10 ;
System.out.println( "The initial Linked list is : " + list);
while (!list.isEmpty()) {
int t = list.poll();
if (t >= 10 )
prioList.offerFirst(t);
else
prioList.offerLast(t);
}
System.out.println( "The prioritized Linked list is : " + prioList);
}
}
|
Output :
The initial Linked list is : [Geeks, 4, Geeks, 8]
LinkedList after insertion using offer() : [Geeks, 4, Geeks, 8, Astha]
2. offerFirst(E e) : This method inserts the specified element at the front of this list.
Declaration :
public boolean offerFirst(E e)
Parameters :
e : the element to add
Return Value :
This method returns true
Java
<div id= "highlighter_831044" class = "syntaxhighlighter nogutter " ><table border= "0" cellpadding= "0" cellspacing= "0" ><tbody><tr><td class = "code" ><div class = "container" ><div class = "line number1 index0 alt2" ><code class = "comments" >
|
Output :
The initial Linked list is : [Geeks, 4, Geeks, 8]
LinkedList after insertion using offerFirst() : [Astha, Geeks, 4, Geeks, 8]
3. offerLast(E e) : This method inserts the specified element at the end of this list.
Declaration :
public boolean offerLast(E e)
Parameters :
e:the element to add
Return Value :
This method returns true
Java
import java.util.*;
public class LinkedListOfferLast {
public static void main(String[] args)
{
LinkedList list = new LinkedList();
list.add( "Geeks" );
list.add( 4 );
list.add( "Geeks" );
list.add( 8 );
System.out.println( "The initial Linked list is : " + list);
list.offerLast( "Astha" );
System.out.println( "LinkedList after insertion using offerLast() : " + list);
}
}
|
Output :
The initial Linked list is : [Geeks, 4, Geeks, 8]
LinkedList after insertion using offerLast() : [Geeks, 4, Geeks, 8, Astha]
Practical Application : This quality of “flexible addition” of these functions can be done in cases of priority addition in queues where elements having a greater no. than threshold have to be handled before the elements lesser than that. Small piece of code below discusses this.
Java
import java.util.*;
public class LinkedListOfferLast {
public static void main(String[] args)
{
LinkedList<Integer> list = new LinkedList<Integer>();
LinkedList prioList = new LinkedList();
list.add( 12 );
list.add( 4 );
list.add( 8 );
list.add( 10 );
list.add( 3 );
list.add( 15 );
int thres = 10 ;
System.out.println( "The initial Linked list is : " + list);
while (!list.isEmpty()) {
int t = list.poll();
if (t >= 10 )
prioList.offerFirst(t);
else
prioList.offerLast(t);
}
System.out.println( "The prioritized Linked list is : " + prioList);
}
}
|
Output :
The initial Linked list is : [12, 4, 8, 10, 3, 15]
The prioritized Linked list is : [15, 10, 12, 4, 8, 3]
Share your thoughts in the comments
Please Login to comment...