Methods in LinkedList

boolean add(Object element)

It adds an element to the end of the list.

  1. list.add("java");
list.add("java");

Above line adds the string “java” to the end of the linked list.

void add(int index, Object item)

It adds an element at the given index of the list.

  1. list.add(2, "welcome");
list.add(2, "welcome");

This will add the string “welcome” at the 3rd position (2 index is 3rd position as index starts with 0)

boolean addAll(Collection c)

It adds all the elements of the specified collection c to the list.
It throws NullPointerException if the specified collection is null.

Example :

  1. LinkedList<String> list = new LinkedList<String>();
  2. ArrayList<String> arraylist= new ArrayList<String>();
  3. arraylist.add("welcome");
  4. arraylist.add("to java");
  5. list.addAll(arraylist);
LinkedList<String> list = new LinkedList<String>();
ArrayList<String> arraylist= new ArrayList<String>();
arraylist.add("welcome");
arraylist.add("to java");
list.addAll(arraylist);

Above code adds all the elements of ArrayList to the LinkedList.

boolean addAll(int index, Collection c) :

It adds all the elements of collection c to the list starting from a give index in the list.
It throws NullPointerException if the collection c is null and IndexOutOfBoundsException when the specified index is out of the range.

Example :

  1. list.add(5, arraylist);
list.add(5, arraylist);

above code adds all the elements of the ArrayList to the LinkedList starting from position 6 (index 5).

void addFirst(Object element)

It adds the element at the first position in the list.

  1. list.addFirst("Hello");
list.addFirst("Hello");

above code adds the string “Hello” at the beginning of the list.

void addLast(Object element) :

It inserts the specified element at the end of the list.

  1. list.addLast("Bye");
list.addLast("Bye");

above code adds the string “Bye” at the end of the linked list.

void clear() :

It removes all the elements of a list.

  1. list.clear();
list.clear();

Object clone() :

It returns the shallow copy of the main list.

Example :

  1. ArrayList<String> arraylist= new ArrayList<String>();
  2. arraylist.add("welcome");
  3. arraylist.add("to java");
  4. Object obj = arraylist.clone();
ArrayList<String> arraylist= new ArrayList<String>();
arraylist.add("welcome");
arraylist.add("to java");
Object obj = arraylist.clone();

Now both arraylist and obj contains the same elements

boolean contains(Object item)

It checks whether the given element is present in the list or not.
If the element is present then it returns true else it returns false.

Example :

  1. boolean retVal = list.contains("java");
boolean retVal = list.contains("java");

It will check whether the string “java” exist in the list or not.

Object get(int index)

It returns the element at the specified index from the list.

  1. Object obj = list.get(2);
Object obj = list.get(2);

It will fetch the 3rd element from the list.

Object getFirst()

It fetches the first element from the list.

  1. Object obj = list.getFirst();
Object obj = list.getFirst();

Object getLast()

It fetches the last element from the list.

  1. Object obj= list.getLast();
Object obj= list.getLast();

int indexOf(Object element) :

It returns the index of the specified element.

  1. list.indexOf("java");
list.indexOf("java");

int lastIndexOf(Object element)

It returns the index of last occurrence of the specified element.

  1. int pos = list.lastIndexOf("java);
int pos = list.lastIndexOf("java);

variable pos will have the integer value of index of last occurrence of “java”

Object poll()

It removes first element of the list and returns the same.

  1. Object obj = list.poll();
Object obj = list.poll();

Object pollFirst()

same as poll() method ,it removes the first element of the list.

  1. Object obj = list.pollFirst();
Object obj = list.pollFirst();

Object pollLast()

It returns and removes the last element of the list.

  1. Object obj = list.pollLast();
Object obj = list.pollLast();

Object remove()

It removes the first element of the list.

  1. list.remove();
list.remove();

Object remove(int index)

It removes the element from the list which is present at the specified index.

  1. list.remove(2);
list.remove(2);

It will remove the 3rd element from the list.

Object remove(Object obj)

It removes the specified object from the list.

  1. list.remove("java");
list.remove("java");

Object removeFirst()

It removes the first element from the list.

  1. list.removeFirst();
list.removeFirst();

Object removeLast()

It removes the last element of the list.

  1. list.removeLast();
list.removeLast();

Object removeFirstOccurrence(Object element)

It removes the first occurrence of the specified element.

  1. list.removeFirstOccurrence("java");
list.removeFirstOccurrence("java");

It will remove the first occurrence of the string “java” from the list.

Object removeLastOccurrence(Object element)

It removes the last occurrence of the given element.

  1. list.removeLastOccurrence("java”);
list.removeLastOccurrence("java”);

It will remove the last occurrence of string “java”.

Object set(int index, Object element)

It updates the element at the specified index with the give value.

  1. list.set(1, "java");
list.set(1, "java");

It will update the 2nd element with the string “java”.

int size()

It returns the number of elements in the list.

  1. list.size();
list.size();

About the Author

Karibasappa G C (KB)
Founder of javainsimpleway.com
I love Java and open source technologies and very much passionate about software development.
I like to share my knowledge with others especially on technology 🙂
I have given all the examples as simple as possible to understand for the beginners.
All the code posted on my blog is developed,compiled and tested in my development environment.
If you find any mistakes or bugs, Please drop an email to kb.knowledge.sharing@gmail.com

Connect with me on Facebook for more updates

Share this article on