Custom List Implementation in Java

Java provides many classes with functionalities expected in any kind of List, such as ArrayList or LinkedList. Still, you may need to design your own custom list implementation for a very specific requirement, or simply as a coding question in a Java interview.

In this tutorial, we will see an example implementation of List in java. Feel free to customize the behavior of the list, by adding or removing methods from CustomList class. If you have some ideas to make this implementation better, please share them with us.

1. Java List Implementation Example

In this class CustomList.java, we are creating a List implementation with the following features:

  • The list will dynamically grow from 16 to infinite size (at least theoretically).
  • The list will be initialized with a minimum capacity of 16 elements at the time of creation.
  • The list will provide methods for fetching, adding, removing and printing the list at any stage in its lifecycle.
import java.util.Arrays;
import java.util.Objects;
import java.util.stream.Collectors;

public class CustomList<E> {

  private int size = 0;
  private static final int DEFAULT_CAPACITY = 16;
  private Object elements[];

  public CustomList() {
    elements = new Object[DEFAULT_CAPACITY];
  }

  public void add(E e) {
    if (size == elements.length) {
      ensureCapacity();
    }
    elements[size++] = e;
  }

  @SuppressWarnings("unchecked")
  public E get(int i) {
    if (i >= size || i < 0) {
      throw new IndexOutOfBoundsException("Index: " + i + ", Size " + i);
    }
    return (E) elements[i];
  }

  public E remove(int i) {
    if (i >= size || i < 0) {
      throw new IndexOutOfBoundsException("Index: " + i + ", Size " + i);
    }
    Object item = elements[i];
    int numElts = elements.length - (i + 1);
    System.arraycopy(elements, i + 1, elements, i, numElts);
    size--;
    return (E) item;
  }

  public int size() {
    return size;
  }

  private void ensureCapacity() {
    int newSize = elements.length * 2;
    elements = Arrays.copyOf(elements, newSize);
  }

  @Override
  public String toString() {
    return "CustomList: [" + Arrays.stream(elements)
        .filter(o -> Objects.nonNull(o))
        .map(o -> o.toString())
        .collect(Collectors.joining(",")) + "]";
  }
}

2. Test

Let’s quickly test out our CustomList implementation.

CustomList<String> namesList = new CustomList<>();

namesList.add("Lokesh");
namesList.add("Alex");

System.out.println(namesList);  //[Lokesh,Alex]

System.out.println(namesList.get(0)); //Lokesh

namesList.remove(1);  

System.out.println(namesList);  //[Lokesh]

The program output:

CustomList: [Lokesh,Alex]
Lokesh
CustomList: [Lokesh]

As verified in the above output, our list implementation can provide all necessary features.

Happy Learning !!

Sourcecode on Github

Comments

Subscribe
Notify of
guest
1 Comment
Most Voted
Newest Oldest
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.

Our Blogs

REST API Tutorial

Dark Mode

Dark Mode