Member-only story
Hi everyone, welcome back. In this tutorial, we will be going over LinkedLists in Java. LinkedLists can be used to store a collection of data, similar to that of an ArrayList. In this tutorial, we will go over how to create a LinkedList and go over some functions that are specific to LinkedLists. With this introduction out of the way, let’s get into it.
Creating LinkedList
Let’s start by creating a LinkedList. Before we can create the LinkedList, we need to make sure we have our LinkedList import, which is included within the java.util package:
import java.util.LinkedList;
Now, we can successfully create our LinkedList:
LinkedList<Integer> numbers = new LinkedList<Integer>();
So now, we have created a LinkedList variable of the integer type named numbers. When we create our LinkedList, we need to specify the data type. As we can see in the example above, we have used the ‘Integer’ data type. We can use other data types as well, such as the ‘String’ data type.
Adding Elements Into The LinkedList
Now, let’s try to add elements into our LinkedList. We can do this by using the built in add() function that is provided. Let’s see an example: