Methods in LinkedList

Let’s learn Methods in LinkedList. In the previous tutorial, We have seen the implementation and example of LinkedList in Java. Here, We are going to discuss the important methods present in the LinkedListclass.

 

How to Declare LinkedList –

We can declare the LinkedList same like ArrayList class.

LinkedList<String> stringList = new LinkedList<String> ; // Generic ArrayList to store only String 

LinkedList<String> stringList = new LinkedList<>(); // Using  Diamond  operator from Java 1.5

 

How to add element into LinkedList

We have seen, how can we add the elements into LinkedList. We can add the elements into LinkedList using add() method.

stringList.add(“Item”);

 

How to add element at given Index into LinkedList

We have seen, how can we add the elements into LinkedList. We can add the elements into LinkedList at given Index using add(int index, Object item) method.

stringList.add(1,”Item2″);

 

How to Add Elements at First and Last Position in LinkedList

We can add elements at first and Last position direcly in the LinkedList. Using addFirst() and addLast() method.

stringList.addFirst(“Item3”);

stringList.addLast(“Item4”);

 

Checking Index of an Item in Java LinkedList

You can use indexOf() method of LinkedList in Java to find out the index of a particular object.
int index = stringList.indexOf(“Item”); //location of Item object in List

 

How to search in LinkedList for an element?

Sometimes we need to check whether an element exists in LinkedList in Java or not for this purpose we can use contains() method of Java. This method returns the boolean value, it returns ‘true’ if element present else ‘false’.
boolean isPresent = stringList.contains(“Item”);

 

How to check if LinkedList is Empty in Java

We can use isEmpty() method of Java LinkedList to check whether LinkedList is empty. isEmpty() method returns true if this LinkedList contains no elements. You can also use size() method of List to check if List is empty or not. If returned size is zero then LinkedList is empty.

boolean result = stringList.isEmpty();  //isEmpty() will return true if List is empty

 

How to remove an element from LinkedList 

There are two ways to remove any elements from LinkedList in Java. You can either remove an element based on its index or by providing object itself. Remove(int index) and remove(Object o) method is used to remove any element from LinkedList.

stringList.remove(0);  

stringList.remove(item); 

 

Copying data from one LinkedList to another LinkedList in Java

Many times you need to create a copy of LinkedList for this purpose you can use an addAll(Collection c) method of LinkedList in Java to copy all elements from on LinkedList to another LinkedList.

LinkedList<String> copyOfStringList = new LinkedList<String>();

copyOfStringList.addAll(stringList);

 

Replace an element at a particular index in LinkedList

You can use a set(int index, E element) method of Java LinkedList to replace any element from a particular index. Below code will replace the first element of stringList from “Item” to “Item2”.

stringList.set(0,”Item2″); 

 

Remove all elements from LinkedList 

LinkedList in Java provides a clear() method which removes all of the elements from this list.

stingList.clear();

 

So these all above methods are the important method of the LinkedList class. Now let’s see some important facts about LinkedList.

 

Selenium Tutorial:

If you liked this video, then please subscribe to our YouTube Channel for more video tutorials.

0 Comment

Leave a Reply