Using Java LinkedList as Stack

Remove Element to a Stack via pop Method

The Java Stack operates on ‘Last In First Out (LIFO)’ principle. In the past example, we used the LinkedList as Queue which operated on ‘First In First Out’. Let us say, the Hotel server is keeping the coffee cups on top of each one. When he serves the coffee to the tables, he will take out the cup from the top. This means, the person will take out cup which he placed last. This shows the LIFO principle. The same way Stack works in Java.

Java Collection does not provide Stack Interface. We can use the Deque interface which adds element in the front (or we can say Top) and removes element from the front using special methods of stack. In this example, we are going to use our Linked List of Products as stack.

Read More

Using Java Queue via LinkedList

Getting Queue Element From Head For Inspecting

The Queue operates on ‘First In First Out (FIFO)’ principle. This means an element added first to the Queue removed first from the queue. For example, in a railway ticket counter the person who enters the queue first will be served first and removed from the queue. Each new person coming to buy the ticket will stand in the tail end of the queue. The same Queue will work in Java. In simple words, elements are added to the tail end of the Queue and removed from the head of the Queue.

In this example, we will see how to use Java Linked as a Queue.

Read More

Java LinkedList vs ArrayList – Performance

After Adding Obj4 to LinkedList

In this example, we will learn about Java’s LinkedList Collection and will compare that with the ArrayList. We will study how each structure holds its merits & demerits in terms data structure change and data fetch.

The Java’s LinkedList Collection implements both List and Dequeue. In the past example, we saw an ArrayList which implements only the List interface. As LinkedList also implements the List, we can get same behaviour offered by ArrayList in LinkedList as well. In this example, we will see the List implementation over the LinkedList and compare its performance with ArrayList.

Read More