Open In App

Java.util.LinkedList.offer(), offerFirst(), offerLast() in Java

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

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




// Java code to demonstrate the application
// of offer() in linked list
import java.util.*;
public class LinkedListOfferLast {
 
public static void main(String[] args)
    {
 
        // Declaring LinkedLists
        LinkedList<Integer> list = new LinkedList<Integer>();
        LinkedList prioList = new LinkedList();
 
        // adding elements
        list.add(12);
        list.add(4);
        list.add(8);
        list.add(10);
        list.add(3);
        list.add(15);
 
        // declaring threshold
        int thres = 10;
 
        // printing the list
        System.out.println("The initial Linked list is : " + list);
 
        while (!list.isEmpty()) {
 
            int t = list.poll();
 
            // adding >=10 numbers at front rest at back
            if (t >= 10)
                prioList.offerFirst(t);
            else
                prioList.offerLast(t);
        }
 
        // The resultant list is
        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">// Java code to demonstrate the working</code></div><div class="line number2 index1 alt1"><code class="comments">// of offerFirst(E e) in linked list</code></div><div class="line number3 index2 alt2"><code class="keyword">import</code> <code class="plain">java.util.*;</code></div><div class="line number4 index3 alt1"><code class="keyword">public</code> <code class="keyword">class</code> <code class="plain">LinkedListOfferFirst {</code></div><div class="line number5 index4 alt2"> </div><div class="line number6 index5 alt1"><code class="keyword">public</code> <code class="keyword">static</code> <code class="keyword">void</code> <code class="plain">main(String[] args)</code></div><div class="line number7 index6 alt2"><code class="undefined spaces">    </code><code class="plain">{</code></div><div class="line number8 index7 alt1"> </div><div class="line number9 index8 alt2"><code class="undefined spaces">        </code><code class="comments">// Declaring a LinkedList</code></div><div class="line number10 index9 alt1"><code class="undefined spaces">        </code><code class="plain">LinkedList list = </code><code class="keyword">new</code> <code class="plain">LinkedList();</code></div><div class="line number11 index10 alt2"> </div><div class="line number12 index11 alt1"><code class="undefined spaces">        </code><code class="comments">// adding elements</code></div><div class="line number13 index12 alt2"><code class="undefined spaces">        </code><code class="plain">list.add(</code><code class="string">"Geeks"</code><code class="plain">);</code></div><div class="line number14 index13 alt1"><code class="undefined spaces">        </code><code class="plain">list.add(</code><code class="value">4</code><code class="plain">);</code></div><div class="line number15 index14 alt2"><code class="undefined spaces">        </code><code class="plain">list.add(</code><code class="string">"Geeks"</code><code class="plain">);</code></div><div class="line number16 index15 alt1"><code class="undefined spaces">        </code><code class="plain">list.add(</code><code class="value">8</code><code class="plain">);</code></div><div class="line number17 index16 alt2"> </div><div class="line number18 index17 alt1"><code class="undefined spaces">        </code><code class="comments">// printing the list</code></div><div class="line number19 index18 alt2"><code class="undefined spaces">        </code><code class="plain">System.out.println(</code><code class="string">"The initial Linked list is : "</code> <code class="plain">+ list);</code></div><div class="line number20 index19 alt1"> </div><div class="line number21 index20 alt2"><code class="undefined spaces">        </code><code class="comments">// offering a new element</code></div><div class="line number22 index21 alt1"><code class="undefined spaces">        </code><code class="comments">// adds element at head.</code></div><div class="line number23 index22 alt2"><code class="undefined spaces">        </code><code class="plain">list.offerFirst(</code><code class="string">"Astha"</code><code class="plain">);</code></div><div class="line number24 index23 alt1"> </div><div class="line number25 index24 alt2"><code class="undefined spaces">        </code><code class="comments">// printing the new list</code></div><div class="line number26 index25 alt1"><code class="undefined spaces">        </code><code class="plain">System.out.println(</code><code class="string">"LinkedList after insertion using offerFirst() : "</code> <code class="plain">+ list);</code></div><div class="line number27 index26 alt2"><code class="undefined spaces">    </code><code class="plain">}</code></div><div class="line number28 index27 alt1"><code class="plain">}</code></div></div></td></tr></tbody></table></div>


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




// Java code to demonstrate the working
// of offerLast(E e) in linked list
import java.util.*;
public class LinkedListOfferLast {
 
public static void main(String[] args)
    {
 
        // Declaring a LinkedList
        LinkedList list = new LinkedList();
 
        // adding elements
        list.add("Geeks");
        list.add(4);
        list.add("Geeks");
        list.add(8);
 
        // printing the list
        System.out.println("The initial Linked list is : " + list);
 
        // offering a new element
        // adds element at end.
        list.offerLast("Astha");
 
        // printing the new list
        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




// Java code to demonstrate the application
// of offer() in linked list
import java.util.*;
public class LinkedListOfferLast {
 
public static void main(String[] args)
    {
 
        // Declaring LinkedLists
        LinkedList<Integer> list = new LinkedList<Integer>();
        LinkedList prioList = new LinkedList();
 
        // adding elements
        list.add(12);
        list.add(4);
        list.add(8);
        list.add(10);
        list.add(3);
        list.add(15);
 
        // declaring threshold
        int thres = 10;
 
        // printing the list
        System.out.println("The initial Linked list is : " + list);
 
        while (!list.isEmpty()) {
 
            int t = list.poll();
 
            // adding >=10 numbers at front rest at back
            if (t >= 10)
                prioList.offerFirst(t);
            else
                prioList.offerLast(t);
        }
 
        // The resultant list is
        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]

 

 



Last Updated : 04 Oct, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads