Java Program to Implement LinkedList API
Linked List is a part of the Collection framework That is present in java.util package. This class is an implementation of the LinkedList data structure which is a linear data structure in which the elements are not stored in contiguous locations and every element is a separate object with a data part and address part.
What is a Linked List API?
- Linked List API aims at Implementing Linked List Collection which as a part of collection framework inherited from java.util package.
- This API is a double-linked list implementation of list and deque interfaces.
- This API implements all optional list operations and permits all elements(including null).
- All the operations perform as could be expected for a doubly-linked list. Operations that index into the list will traverse the list from the beginning or the end, whichever is closer to the specified index.
- Below is the Source Code for the java program to Implement LinkedList Collection API.
- We create a Collection method LinkedListImpl<E> to invoke Linked List object.
- We create a boolean method add() to Append the specified element to the end of this list.
- We create a void add() method to Insert the specified element at the specified position in this list.
- We create a boolean addAll() method to Appends all of the elements in the specified collection to the end of this list, in the order that they are returned by the specified collection’s Iterator.
- We create a void addFirst() method to Insert the specified element at the beginning of this list.
- We create a void addLast() method to Appends the specified element to the end of this list.
- We create clear() method to Removes all of the elements from this list.
- We create a clone() method which Returns a shallow copy of this ArrayList instance.
- We create a contains() method which Returns Returns true if this list contains the specified element
- We create a descendingIterator() which Returns an iterator over the elements in this deque in reverse sequential order.
- We create element() method which Retrieves, but does not remove, the head (first element) of this list.
- We create getFirst() method which Returns the first element in this list.
- We create getLast() method which Returns the last element in this list.
- We create indexOf() method with object as parameter which Returns the index of the first occurrence of the specified element in list or -1 if this list does not contain the element.
- We create isEmpty() to return true if this list contains no elements.
- We create iterator() method for arrayList which Returns an iterator over the elements in this list in proper sequence.
- We create lastIndexOf() to Returns the index of the last occurrence of the specified element in this or else -1.
- We create listIterator() method for ArrayList listIterator<E> to Returns a list iterator over the elements in this list.
- We write listIterator(int index) to Returns a list iterator over the elements in this list.
- We write offer() method to Add the specified element as the tail (last element) of this list.
- We write offerFirst(E e) to Inserts the specified element at the front of this list.
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.