Java LinkedList offerFirst() Method
-- Learning technology, pursuing dreams!
Tutorials
Java Tutorial
- Java Tutorial
- Java Introduction
- Java Development Environment Setup
- Java Basic Syntax
- Java Comments
- Java Objects and Classes
- Java Primitive Data Types
- Java Variable Types
- Java Variable Naming Rules
- Java Modifiers
- Java Operators
- Java Loop Structures
- Java Conditional Statements
- Java switch case
- Java Number & Math Class
- Java Character Class
- Java String Class
- Java StringBuffer
- Java Arrays
- Java Date Time
- Java Regular Expressions
- Java Methods
- Java Constructors
- Java Stream, File, IO
- Java Scanner Class
- Java Exception Handling
Java Object-Oriented
- Java Inheritance
- Java Override/Overload
- Java Polymorphism
- Java Abstract Classes
- Java Encapsulation
- Java Interfaces
- Java Enumerations
- Java Package
- Java Reflection
Java Advanced Tutorial
- Java Data Structures
- Java Collections Framework
- Java ArrayList
- Java LinkedList
- Java HashSet
- Java HashMap
- Java Iterator
- Java Object
- Java NIO Files
- Java Generics
- Java Serialization
- Java Networking
- Java Sending Email
- Java Multithreading
- Java Applet Basics
- Java Documentation Comments
- Java Examples
- Java 8 New Features
- Java MySQL Connection
- Java 9 New Features
- Java Quiz
- Java Common Libraries
Explore Further
Web Service
Network Services
Programming Languages
Web Design & Development
Scripting
Programming
Development Tools
Scripting Languages
Software
Computer Science
Java LinkedList offerFirst() Method
The offerFirst() method is a convenient method provided by the LinkedList class in Java, used to insert the specified element at the front (head) of the linked list. This method is part of the Deque (double-ended queue) interface, which the LinkedList class implements.
Method Syntax
public boolean offerFirst(E e)
Method Parameters
| Parameter | Type | Description |
|---|---|---|
| e | E | The element to be added to the head of the linked list |
Return Value
This method always returns true, because LinkedList can grow dynamically and can theoretically add elements infinitely (limited by memory size).
Method Characteristics
- Non-blocking operation: Unlike
addFirst(),offerFirst()does not throw exceptions - Unlimited capacity:
LinkedListhas no fixed capacity limit - Efficient operation: The time complexity for inserting elements at the head of the linked list is O(1)
Usage Example
Example
import java.util.LinkedList;
public class OfferFirstExample {
public static void main(String[] args){
// Create a LinkedList
LinkedList fruits = new LinkedList();
// Add elements using offerFirst()
fruits.offerFirst("Apple");
fruits.offerFirst("Banana");
fruits.offerFirst("Cherry");
// Display the list
System.out.println("LinkedList: " + fruits);
// Result:
// LinkedList: [Cherry, Banana, Apple]
}
}
YouTip