Java is an object-oriented programming language that provides various data structures for efficient data storage and manipulation. One of the most commonly used data structures in Java is the List. A List is an ordered collection of elements and can contain duplicate values.

There are several ways to create a new List in Java, each with its own advantages and disadvantages. In this post, we’ll cover the most commonly used methods for creating lists.

1. Using the ArrayList Class

The most popular way to create a List in Java is to use the ArrayList class. ArrayList is an implementation of the List interface and provides dynamic arrays, which can grow or shrink as needed. Here’s an example of how to create a new ArrayList:

import java.util.ArrayList;

public class Main {
  public static void main(String[] args) {
    ArrayList numbers = new ArrayList<>();
    numbers.add(1);
    numbers.add(2);
    numbers.add(3);
    System.out.println(numbers);
  }
}

Output: [1, 2, 3]

2. Using the LinkedList Class

Another popular way to create a List in Java is to use the LinkedList class. LinkedList is also an implementation of the List interface, but instead of using arrays, it uses a linked list to store its elements. LinkedLists are faster for inserting and removing elements, but slower for accessing elements by index. Here’s an example of how to create a new LinkedList:

import java.util.LinkedList;

public class Main {
  public static void main(String[] args) {
    LinkedList colors = new LinkedList<>();
    colors.add("Red");
    colors.add("Green");
    colors.add("Blue");
    System.out.println(colors);
  }
}

Output: [Red, Green, Blue]

3. Using the Vector Class

Another option for creating a List in Java is to use the Vector class. Vector is similar to ArrayList, but it is synchronized, meaning that multiple threads can access it without encountering any concurrency issues. However, this synchronization comes at the cost of performance. Here’s an example of how to create a new Vector:

import java.util.Vector;

public class Main {
  public static void main(String[] args) {
    Vector vowels = new Vector<>();
    vowels.add('a');
    vowels.add('e');
    vowels.add('i');
    System.out.println(vowels);
  }
}

Output: [a, e, i]

4. Using the Collections Class

The Collections class in Java provides a convenient method for creating a List from an existing collection of elements. The Collections.list() method creates a List from the specified collection. Here’s an example of how to create a new List using the Collections class:

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Main {
  public static void main(String[] args) {
    List evenNumbers = new ArrayList<>();
    evenNumbers.add(2);
    evenNumbers.add(4);
    evenNumbers.add(6);
    List newList = Collections.list(evenNumbers);
    System.out.println(newList);
  }
}

Output: [2, 4, 6]

5. Using the Arrays Class

The Arrays class in Java provides a convenient method for creating a List from an array of elements. The Arrays.asList() method creates a List from the specified array. Here’s an example of how to create a new List using the Arrays class:

import java.util.Arrays;
import java.util.List;

public class Main {
  public static void main(String[] args) {
    String[] daysOfWeek = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"};
    List newList = Arrays.asList(daysOfWeek);
    System.out.println(newList);
  }
}

Output: [Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday]

6. Using the CopyOnWriteArrayList Class

The CopyOnWriteArrayList class in Java provides a thread-safe implementation of a List. It is ideal for use in environments where multiple threads may be accessing the List simultaneously. Here’s an example of how to create a new CopyOnWriteArrayList:

import java.util.concurrent.CopyOnWriteArrayList;

public class Main {
  public static void main(String[] args) {
    CopyOnWriteArrayList decimals = new CopyOnWriteArrayList<>();
    decimals.add(0.1f);
    decimals.add(0.2f);
    decimals.add(0.3f);
    System.out.println(decimals);
  }
}

Output: [0.1, 0.2, 0.3]

7. Using the Stack Class

The Stack class in Java provides a Last-In-First-Out (LIFO) implementation of a List. It is ideal for use in situations where you need to maintain a history of elements, such as a browser’s back button. Here’s an example of how to create a new Stack:

import java.util.Stack;

public class Main {
  public static void main(String[] args) {
    Stack browserHistory = new Stack<>();
    browserHistory.push("Google");
    browserHistory.push("Youtube");
    browserHistory.push("Facebook");
    System.out.println(browserHistory);
  }
}

Output: [Facebook, Youtube, Google]

Conclusion

In this post, we learned about the different ways to create a new List in Java. Whether you need a fast, dynamic array or a synchronized list, there is a class in Java that can meet your needs. Choose the one that best fits your use case, and start creating lists today!

Leave a Reply