DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Getting Started With Agentic Workflows in Java and Quarkus
  • Building AI-Powered Java Applications With Jakarta EE and LangChain4j
  • Alternative Structured Concurrency
  • Jakarta EE 12: Entering the Data Age of Enterprise Java

Trending

  • S3 Vectors: How to Build a RAG Without a Vector Database
  • Build Self-Managing Data Pipelines With an LLM Agent
  • Visualizing Matrix Multiplication as a Linear Combination
  • AWS Managed Database Observability: Monitoring DynamoDB, ElastiCache, and Redshift Beyond CloudWatch
  1. DZone
  2. Coding
  3. Java
  4. Creating a Custom List With Java Implementing Iterator.

Creating a Custom List With Java Implementing Iterator.

In this post, we are going to talk about developing a custom data structure and how to create a list using Java.

By 
Juan Manuel Kelsy Romero user avatar
Juan Manuel Kelsy Romero
·
Updated Jul. 03, 19 · Tutorial
Likes (14)
Comment
Save
Tweet
Share
32.9K Views

Join the DZone community and get the full member experience.

Join For Free

In this post, we are going to talk about developing a custom data structure and how to create a list using Java.

Before we code, we need to understand a few concepts.

List: A list is a data structure which represents a collection or arrangement of data (nodes), and every node has the pointer to the next node, which means the nodes can't be accessed by the index. The below image shows a list with three elements (nodes), and the "First" and "Last" pointers to make possible the access to the list data. 

Linked List with three elements

Node: A node is a structure which represents each element in the list. A node has at least one pointer to the next element and a field that contains the specific information or data. In a list with one element the first and last pointer point to the same element.

A Node

Next, Prev, First, Last (Pointers):  These are pointers which allow access to the nodes in a list. 

Empty List: A list without elements, the "first" and "last" nodes are null.

Empty List

Let's Code

First the Node class:

package app;

public class Node <T> {
    private Node next;
    private Node prev;
    private T data;

  //getters and setters omited..  
}

In the Node class, the important thing to highlight is the use of generic <T> to pass the data type of the node and make the list reusable.

After that, we need implement the CustomList class: 

package app;

import java.util.Iterator;

public class CustomList<T> implements Iterable<Node> {
    private Node first;
    private Node last;

    public CustomList() {
        first = last = null;
    }

    public boolean isEmpty(){
        return first == null;
    }

    public void push(T data) {
        Node tempo = new Node();
        tempo.setData(data);
        tempo.setNext(null);

        if (first == null) {
            tempo.setPrev(null);
            first = last = tempo;
        } else {
            tempo.setPrev(last);
            last.setNext(tempo);
            last = tempo;
        }
    }

    @Override
    public Iterator<Node> iterator() {
        return new ListIterator(first);
    }
}

The CustomList class implements Iterable, which allows us to use "for" with the list.

In the next step, we will create our own implementation of ListIterator:

package app;

import java.util.Iterator;

public class ListIterator implements Iterator<Node> {
    private Node current;

    public ListIterator(Node first) {
        current = first;
    }

    @Override
    public boolean hasNext() {
        return current != null;
    }

    @Override
    public Node next() {
        Node tempo = current;
        current = current.getNext();
        return tempo;
    }    
}

It is necessary to pass the first node of the list in the constructor to access the contents of the list using the pointers (next, prev) depending on your needs. We must also overload the next() and hasNext() methods.

Finally, we use our class to store Person and Car:

package app;

public class App {
    public static void main(String[] args) throws Exception {

        CustomList<Person> list1 = new CustomList<>();

        list1.push(new Person("Clark", "Kent", 35));
        list1.push(new Person("Bruce", "Wayne", 40));
        list1.push(new Person("Linda", "Carter", 30));

        for (Node node : list1) {
            System.out.println(node.getData().toString());
        }

        CustomList<Car> list2 = new CustomList<>();
        list2.push(new Car(200, "Car 1"));
        list2.push(new Car(100, "Car 2"));

        for (Node node : list2) {
            System.out.println(node.getData().toString());
        }        
    }
}

In the above code, we noticed that we can iterate over the lists with the for structure.

That's all folks, we've implemented our custom List with Java by implementing Iterator.

Java (programming language)

Opinions expressed by DZone contributors are their own.

Related

  • Getting Started With Agentic Workflows in Java and Quarkus
  • Building AI-Powered Java Applications With Jakarta EE and LangChain4j
  • Alternative Structured Concurrency
  • Jakarta EE 12: Entering the Data Age of Enterprise Java

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook