LinkedList Java Tutoriral

In this article, we show how to work with LinkedLists in Java.
LinkedLists in Java can be seen as a step-up from ArrayLists.
A LinkedList differs from an ArrayList in that it is more efficient when you need to insert or delete items in the middle of the list. In an ArrayList, when you insert an item in the middle of the ArrayList, the ArrayList has to copy all the items past the insertion point one slot over to free a slot for the item you're inserting. With a LinkedList, all that has to change is the pointers in the preceding and the following objects.
This is because a linked list is a collection in which every object in the list maintains with it a pointer to the following object in the list and another pointer to the preceding object in the list. So the list is managed entirely by these pointers.
We show how to declare (create) LinkedLists, add elements to the LinkedList, get the size of the LinkedList and many more operations.
So we show the code below of how to various operations with LinkedLists.
So the above code shows a lot when dealing with LinkedLists.
So in order to declare and use a LinkedList in Java, it is part of the java.util package. Therefore, you can have to include either the line, import java.util.*; (which includes all files of the java.util package) or include the line, java.util.LinkedList;
Without this import line, the code will not work.
We then create a public class named Team.
We then create our main() method, which is where the program begins to execute the code.
Creating a LinkedList
To declare (create) a LinkedList, you have to specify the Wrapper type of the class, whether it's String, Integer, or
Character. You cannot declare primitive types in the LinkedList. LinkedLists are declared with the Wrapper classes of those primitives.
So to create a LinkedList of Strings, you have to put in So to create a LinkedList of Strings named team, the declaration is shown below.
So we now have a LinkedList of type String (a LinkedList of Strings) named team.
To add elements to the array, we take the array name and use the add() method to add elements to the array.
So in this case, with the LinkedList named team, we take team and followed it with a dot and the add() method. Inside of
the add method is the String that we want to add to the array.
Java also has an addFirst() method, which adds the argument to the method to the beginning of the LinkedList, so that it is the
first element.
If you want to add an element to any position in the LinkedList, you use the add() method and accept 2 parameters. The first
parameter is the position that you want to add the element to in the LinkedList. The second is data you want to add to that position. So if you
wanted to add "Tom" to 2nd element in the array, the code would be, add(2, "Tom");
So in the code below, we add 5 string elements to the team LinkedList.
So you can see that LinkedLists are very dynamic. You don't have to declare a fixed preset values of the number of elements
in the LinkedList. You simply create the LinkedList and then you add elements to it as you go along.
Next we show how to access and output the elements of the LinkedList.
To access the individual elements of a LinkedList, you take the name of the LinkedList and invoke the get() method. Inside
of the get method, you specify the index number of the array that you want to access.
To output the individual elements of an array, you can use the code shown below.
So the above lines gets the individual elements of the array. You can see how we use the LinkedList name, followed by
a dot, and the get() method, with the index of the LinkedList as the argument to the get() method.
So this is how we can access and output any individual element of an array.
Even though you can output all the elements of a LinkedList with the above method, if you are want to output all the
elements of an array, there is a more efficient way of doing it.
One way is to use a regular for loop and looping through the complete LinkedList.
This is shown with the following code.
By getting the length of the LinkedList with the size() method, we can know how many elements there are in the LinkedList.
Therefore, we can loop through every element in the Array.
We then output each element in the Array in the body of the for loop.
Even though this for loop works very well, there's an even easier way of doing it using an enhanced for loop.
This is shown below.
You can see how this is much simpler than the previous for loop.
To get the size (the number of elements in the LinkedList), we use the size() method.
If you take the array name, followed by a dot, and the size() method, you can get the size (the number of elements in the
LinkedList).
This is shown below.
In our example, since we have 5 elements in the array, the size of the array is 5.
To update elements in the LinkedList, we use the set() function.
The set() function takes 2 parameters. The first parameter is the element of the LinkedList that you want to update.
The second parameter is the new value that you want to set that element to.
This is shown below.
We update the first 2 elements of the array.
So you can see above that we update the values of the first 2 elements of the LinkedList.
To delete individual elements of the LinkedList, we can use the remove() method.
You take the name of the LinkedList, followed by a dot, and the index number of the LinkedList that you want to remove.
Below we delete all the elements of the array.
So we delete the first element of the array.
Now the second element becomes the first element in the array.
If you now want to delete the second element of the original LinkedList, you would use team.remove(0) again. So it's a little
tricker. If you want to delete multiple elements, you would use the remove() method taking in 2 parameters. The first parameter would be in the
index you want to start deleting from in the array and the second parameter is the index where you want to stop deleting to. So it deletes
the elements from index start to index finish, according to the statement, remove(int fromIndex, int toIndex). So if you want to delete elements
the first 4 elements of a LinkedList, the code would be, remove(0,3). This is the equivalent of using the expression, remove(0); 4 times.
So if you are deleting individual elements of the array, you would use the above method.
However, if you want to delete all elements of the LinkedList, there is a more efficient way of doing it using the clear()
method.
If you take the LinkedList name, followed by a dot, and the clear() method, all elements in the LinkedList get erased.
This is shown below.
This clears all elements of the LinkedList.
Java has a built-in function IsEmpty() to check if a LinkedList is empty or not.
You simply take the name of the LinkedList followed by a dot and the isEmpty() function to check if it is empty or not.
This is shown below.
So is the isEmpty() function returns a boolean value of either true or false.
Java also has a built-in function, contains(), so that we can check to see if an array contains a certain value.
So, for example, we can check to see if the LinkedList contains "Michael".
We would use the name of the LinkedList followed by a dot and the contains() function with the String "Michael" as the argument to the contains() function.
This is shown below.
The contains() function returns the boolean value of either true or false.
So these are many of the functions used with Java LinkedLists.
Related Resources
Adding Elements to the LinkedList
Accessing and Outputting the Elements of a LinkedList
Getting the Size of the LinkedList
How to Update Elements in the LinkedList
How to Delete Elements of the LinkedList
Checking if a LinkedList is Empty
Checking to See if a LinkedList Contains a Certain Value