Showing posts from October, 2019

Create your own LinkedList in Java

As a Java developer, we need to frequently use the collection framework. Have you ever tried to create your own any of the collection classes in Java? Here we go, we will see how to create our own LinkedList in Java. We will create a very simple class with add and get methods. Also, the LinkedList class of collection framework is doubly LinkedList but we will singly LinkedList. Line 36 to 42, we have created an inner class i.e Node with a generic type T. This class has 2 fields: item field is a generic type that will hold an actual value of LinkedList node. next field is a Node type that holds the address of the next node of the LinkedList. We have also defined a constructor to set value for the item field. In lines 4 and 5, we have created an instance variable of type Node in MyLinkedList class i.e. first and last. The field first will always hold the first node of the LinkedList and the field last will always hold the node of LinkedList. Now the question is, why