Pages

Footer Pages

Spring Boot

Java String API

Java Conversions

Kotlin Programs

Kotlin Conversions

Java Threads Tutorial

Java 8 Tutorial

Wednesday, December 22, 2021

Java List or ArrayList Max Size (With Examples)

1. Overview

In this tutorial, We'll understand what is the limit of ArrayList or List implementations that can hold the maximum size.

But this is completely different from finding maximum value from ArrayList.

Before finding the max size of ArrayList, we need to understand that ArrayList collection API is implemented based on the ordinary arrays of java.

If the new ArrayList<Integer>(5) then a new ArrayList with the size of 5 is created and it can store 5 integer values.

Java List or ArrayList Max Size




2. Java Example to Find Default Max size of ArrayList


As we know the size of the ArrayList is can be retrieved using the ArrayList.size() method.

The below example is to get the default size of the ArrayList.

Example
package com.javaprogramto.java8.arraylist;

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

public class ArrayListMaxSizeExample {

	public static void main(String[] args) {

		// default size is 10
		List<Integer> intList = new ArrayList<>();
		for (int i = 0; i < 10; i++) {
			intList.add(i);
		}

		System.out.println("Int list size (default) - " + intList.size());

	}

}

Output
Int list size (default) - 10
We used ArrayList.add() method to add integer valeus to arraylist.


3. Java Example to Find Default Max size of ArrayList


ArrayList.size() method returns the size of the list as integer value.
Integer max value is 2^31 and this value is equal to the 2147483647.

In the below example, we have created an ArrayList with the size of Integer.MAX_VALUE.

Let us compile and run this program to see the output.

Example
package com.javaprogramto.java8.arraylist;

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

public class ArrayListMaxSizeExample2 {

	public static void main(String[] args) {

		System.out.println("Integer.MAX_VALUE - " + Integer.MAX_VALUE);
		System.out.println("Math.pow(2, 31) - " + (int) Math.pow(2, 31));

		// default size is 10
		int maxValue = Integer.MAX_VALUE - 1;
		List<Integer> intList = new ArrayList<>(maxValue);
		
		for (int i = 0; i < maxValue; i++) {
			intList.add(i);
		}

		System.out.println("Int list size - " + intList.size());

	}

}

Output
OpenJDK 64-Bit Server VM warning: Ignoring option MaxPermSize; support was removed in 8.0
Integer.MAX_VALUE - 2147483647
Math.pow(2, 31) - 2147483647
Exception in thread "main" java.lang.OutOfMemoryError: Requested array size exceeds VM limit
	at java.base/java.util.ArrayList.<init>(ArrayList.java:156)
	at com.javaprogramto.java8.arraylist.ArrayListMaxSizeExample2.main(ArrayListMaxSizeExample2.java:15)

That means you can not use the ArrayList with max size as Integer.MAX_VALUE. This itself is not able to create the array with the size of the max value. That's why we got the error "java.lang.OutOfMemoryError: Requested array size exceeds VM limit".


4. Conclusion


In this article, We've seen How much data can a List can hold at the maximum in java?




No comments:

Post a Comment

Please do not add any spam links in the comments section.