Java LinkedList offer(E e) Example

We have already learnt the usage of add(E e) and addLast(E e) methods of java.util.LinkedList class which adds the specified element at the end of the LinkedList. offer(E e) also adds the specified element to the tail of the list


public boolean offer(E e) 

Adds the specified element to the tail of the list and will return true if the insertion is successful.


The following example shows the usage of java.util.LinkedList.offer(e) method.

import java.util.Iterator;
import java.util.LinkedList;

public class OfferMethodLinkedListExample {
	public static void main(String args[])
		// Create LinkedList object
        LinkedList list1 = new LinkedList();
        //adding elements to list1
        list1.add("String 1");
        list1.add("String 2");
        list1.add("String 3");
        list1.add("String 4");
        //addding element to the tail of the list
        boolean insertion= list1.offer("String 88");
        //printing all the values of the LinkedList
        System.out.println("**Contents of the list**");
        Iterator iterator = list1.iterator();
        System.out.println("Is the insertion successful ?? "+insertion);


**Contents of the list**
String 1
String 2
String 3
String 4
String 88
Is the insertion successfuly?? true

Leave a Reply

Your email address will not be published. Required fields are marked *