The LinkedList class in Java provides two methods offer and add to add an element to the list.
There is a subtle difference between both of them.
The add method
The add method is inherited from the List interface. It returns a boolean value to indicate whether the element was successfully added to the list or not.
Also, you will get an IllegalStateException if the list is at its capacity.
The offer method
The offer method is defined in the Queue Interface. It was added to the Queue interface in Java 1.5
It returns a boolean value indicating whether the element was successfully added to the queue or not.
| Method | Interface | Returns | Exception | When to Use |
|---|---|---|---|---|
| add(e) | List | boolean | IllegalStateException if the list is full | Using the LinkedList as a List to check if the element was successfully added |
| offer(e) | Queue | boolean | boolean false if the queue is full | Using LinkedList as a Queue or for interchanging implementations of the Queue interface |
Provide Feedback For This Article
We take your feedback seriously and use it to improve our content. Thank you for helping us serve you better!
😊 Thanks for your time, your feedback has been registered!
Comments & Discussion
Facing issues? Have questions? Post them here! We're happy to help!