Initialize an ArrayList in Java

Cheav Sovannarith
Jul 22, 2019 · 1 min read

How we initialize ArrayList in java

Photo by A. L. on Unsplash

ArrayList is a class which extends AbstractList and implements the List interface. ArrayList supports dynamic arrays that can grow as needed.

Standard Java arrays are of a fixed length. After arrays are created, they cannot grow or shrink, which means that you must know in advance how many elements an array will hold.

  1. With add()

2. With Double Brace

3. With Shared Reference

This will create a immutable list

4. From Array

This produce a fixed size list

5. From a Stream interface

6. From Immutable list factory method in java 9

This will create a fixed immutable list up to 10 elements

the new factory methods for Collections introduced in Java 9 creating unmodified collections.

Hope this article help you a lots 🐵!

Java Epic

About Java

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade