Java sort linked list of objects

This Tutorial Explains What is a Linked List Data Structure in Java and How to Create, Initialize, Implement, Traverse, Reverse and Sort a Java Linked List:

In Java, a LinkedList is a data structure that stores elements in a non-contiguous location. It is a linear data structure.

Each data item is called a Node and each node has a data part and an address part. The address part stores the link to the next node in the LinkedList.

=> Visit Here To See The Java Training Series For All.

What You Will Learn:

  • LinkedList In Java
    • Java LinkedList Class
    • How To Create A Linked List In Java
    • Linked List Implementation In Java
    • Traverse/Print Linked List In Java
      • Using for loop
      • Using forEach Loop
      • Using Iterator
    • LinkedList Methods
    • Reverse Linked List In Java
    • Sort A Linked List In Java
    • Remove Duplicates
    • Circular Linked List In Java
    • Java 8 LinkedList
    • Frequently Asked Questions
  • Conclusion
    • Recommended Reading

LinkedList In Java

Given below is the general Layout of LinkedList:

As shown in the above representation of LinkedList, each item in the LinkedList is the Node. Each node has two parts, the first part stores the data and the second part has a reference or pointer or address of the next node in the LinkedList.

This arrangement is necessary as the data in LinkedList is stored in non-contiguous locations, unlike Arrays.

The Head of the LinkedList is a pointer that contains the address of the first element in the LinkedList. The last node in the LinkedList is the tail. As shown in the figure above, the address part of the last node in the LinkedList is set to Null indicating the end of the LinkedList.

The above diagram represents a Singly-linked List that stores the address of only the next node in the LinkedList.

There is another version known as Doubly Linked List whose each node has three parts:

  1. Address or reference or pointer to the previous element in the LinkedList.
  2. Data part
  3. Address or reference or pointer to the next element in the LinkedList.

The previous address of the first element in the LinkedList will be set to Null while the next pointer of the Last element in the LinkedList is set to Null.

Representation Of Doubly Linked List:

As shown in the above representation, each node in the doubly linked list has pointers to its previous and next node [thus represented without arrows]. The previous pointer of the first node points to null while the next pointer of the last node points to null.

In this LinkedList tutorial, we will deal mostly with the singly linked list. We will discuss the doubly linked list in our next tutorial.

Java LinkedList Class

In Java, the linked list is implemented by the LinkedList class. This class belongs to the java.util package. The LinkedList class implements the List and Deque interfaces and inherits the AbstractList class.

Given below is the class hierarchy of the LinkedList class.

The above diagram shows the hierarchy of the LinkedList class. As shown, LinkedList class implements the List and Deque interfaces.

As already mentioned, LinkedList class is a part of the java.util package. Hence you should be able to use the LinkedList class in your program by including one of the following statements in your program.

import java.util.*;

Or

import java.util.LinkedList;

So based on the above hierarchy, a typical definition of LinkedList class is as follows:

public class LinkedList extends AbstractSequentialList implements List, Deque, Cloneable, Serializable

Enlisted below are some of the characteristics of the LinkedList class that you should remember:

  • This class is not synchronized.
  • It allows duplicate values.
  • Retains the insertion order.
  • As elements are not required to be shifted while moving, the manipulation of elements in it is faster.
  • This class can be used to implement a stack, queue, and list.

How To Create A Linked List In Java

Before we move on to creating a linkedlist in Java, lets first discuss a linked list node in Java.

As already discussed, a linked list consists of nodes. Thus in Java, we can represent a LinkedList as a class with its Node as a separate class. Hence this class will have a reference to the Node type.

This is shown as below:

class LinkedList { Node head; // list head //node - linkedlist class Node { int data; Node next; Node[int d] { data = d; } //constructor to create a new node } }

To create an object of type LinkedList, there are two main constructors as follows:

#1] LinkedList[]

The general syntax for this constructor is:

LinkedList linkedList = new LinkedList[];

The above statement creates an empty LinkedList.

For example,

LinkedList l_list = new LinkedList[];

This will create an empty linked list named l_list.

#2] LinkedList[Collection c]

The general syntax is:

LinkedList linkedList = new LinkedList [Collection

Chủ Đề