Explain Codes LogoExplain Codes Logo

How to initialize List<String> object in Java?

java
list-initialization
java-8
stream-api
Anton ShumikhinbyAnton Shumikhin·Dec 29, 2024
TLDR

For a mutable List<String>, initialize this way:

List<String> list = new ArrayList<>(); //An empty plot for you to fill

Need a prefilled list? Here:

List<String> list = new ArrayList<>(Arrays.asList("One", "Two", "Three")); //A cozy home with 3 residents

If you need an immutable list, create it as such:

List<String> immutableList = List.of("One", "Two", "Three"); //A neighborhood that never changes

Selection guide: Choose your List implementation wisely!

Mutable lists can be changed anytime. Immutable lists remain the same post creation.

Here are some common List<String> implementations suited to various needs:

  • ArrayList: Excellent for frequent get operations.
  • LinkedList: Designed for heavy insertion or deletion.
  • Vector: An ArrayList with a degree in thread synchronization.

Kickstart your List with values

  • Use Arrays.asList for a fixed-size list pre-filled with elements. Adding or removing elements however ends in tears and UnsupportedOperationException.
  • Make your fixed-size list dynamic with some magic: new ArrayList<>(Arrays.asList(...));. Now, no one cries when you add or remove elements.
  • For greater flexibility, harness the power of the Stream API in JDK 8 and beyond:
    List<String> dynamicList = Stream.of("A", "B", "C").collect(Collectors.toList()); //A list that changes its size like Houdini
  • With JDK 9, you've got the power to create an immutable list with List.of(). To make it mutable later, use this clever cheat code:
    List<String> mutableList = new ArrayList<>(List.of("X", "Y", "Z")); //Immortality potion for your immutable list

Know your List type: Performance specifics

  • ArrayList: Fast read operations, less stellar for random insertions/deletions.
  • LinkedList: Swift insertions/deletions, slower get operations.
  • Vector: Doesn't break into a sweat under multithreaded operations but can take a few extra milliseconds to deliver.

Initializing Lists: Next level stuff

Dynamic initialization with Java 8 Stream API

With the Stream API, you've got an array of options for complex initializations:

List<String> listFromStream = Stream.of("Java", "Kotlin", "Scala") .collect(Collectors.toList());

List initialization using third-party libraries

Fancy something different? Libraries like Guava offer unique patterns for list initialization.

Don't fall for the banana in the tailpipe

Don't turn red with embarrassment! Remember, you can't instantiate a List interface directly (it's an interface, not a class). And don't mistake the list created with Arrays.asList() for a regular ArrayList!

Matter of performance: Choose suitably

When initializing lists, performance matters a lot. Consider these factors:

  • ArrayList offers low memory overhead but may be slowed down by heavy insertions/deletions.
  • LinkedList is perfect for frequent modifications, at the cost of more memory used.
  • Vector is safe for multithreaded environments, but the additional thread safety can slightly slow down operations.
  • Capacity matters too. ArrayList starts with a default capacity but grows on demand; Vector allows setting an initial capacity and increment capacity.