BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Java ArrayList trimToSize() Method example

By Chaitanya Singh | Filed Under: java

trimToSize() method is used for memory optimization. It trims the capacity of ArrayList to the current list size. For e.g. An arraylist is having capacity of 15 but there are only 5 elements in it, calling trimToSize() method on this ArrayList would change the capacity from 15 to 5.

public void trimToSize()

Example

Here I have defined the ArrayList of capacity 50. After adding 10 elements I called trimToSize() method which would have reduced the capacity from 50 to 10 (current size of arraylist).

package beginnersbook.com;
import java.util.ArrayList;
public class TrimExample {
  public static void main(String args[]) {
    ArrayList<Integer> arraylist = new ArrayList<Integer>(50);
    arraylist.add(1);
    arraylist.add(2);
    arraylist.add(3);
    arraylist.add(4);
    arraylist.add(5);
    arraylist.add(6);
    arraylist.add(7);
    arraylist.add(1);
    arraylist.add(1);
    arraylist.add(1);
    arraylist.trimToSize();
    System.out.println(arraylist);
  }
}

Output:

[1, 2, 3, 4, 5, 6, 7, 1, 1, 1]

Comments

  1. Lathy says

    December 11, 2015 at 11:18 AM

    Hi,
    I ran your code. It prints the same result whether trimToSize() method added or removed.
    I even checked the size of the arraylist with and without trimToSize() method but in both case it remains same.
    Can you please check and update.

    Reply
    • Tony says

      October 10, 2017 at 6:14 PM

      @Lathy, There is a difference between size and capacity of an ArrayList. Size is the number of data items stored in an ArrayList. Capacity is the length of the Array that is used internally by the ArrayList. For obvious reasons, Capacity is always maintained greater than or equal to the Size. The trimToSize() method is used in situations where you know the ArrayList size is not likely to increase and you do not want to waste more memory space. Look up the implementation of ArrayList for more info.

      Reply
  2. Sudarshan says

    April 13, 2018 at 1:25 PM

    Good Day Guys,

    Can we restrict the size of the Arraylist ?
    eg : I should add only 10 objects to the arraylist.

    Reply
    • Chaitanya Singh says

      April 19, 2018 at 12:12 PM

      Hey Sudarshan, You cannot restrict the size of an ArrayList. Why you want to do that? You could have used array instead of ArrayList if you want the size to be limited.
      However if you want to achieve this in ArrayList then you have to limit the size by yourself in the code, something like this would work:

      ArrayList list = new ArrayList();
      if(list.get(10) != null)  //If there exists a 11th element
      {
           throw new ArrayIndexOutOfBoundsException();
      }
      Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Java Tutorial

Java Introduction

  • Java Index
  • Java Introduction
  • History of Java
  • Features of Java
  • C++ vs Java
  • JDK vs JRE vs JVM
  • JVM - Java Virtual Machine
  • First Java Program
  • Variables
  • Data Types
  • Operators

Java Flow Control

  • Java If-else
  • Java Switch-Case
  • Java For loop
  • Java while loop
  • Java do-while loop
  • Continue statement
  • break statement

Java Arrays

  • Java Arrays

OOPs Concepts

  • OOPs Concepts
  • Constructor
  • Java String
  • Static keyword
  • Inheritance
  • Types of inheritance
  • Aggregation
  • Association
  • Super Keyword
  • Method overloading
  • Method overriding
  • Overloading vs Overriding
  • Polymorphism
  • Types of polymorphism
  • Static and dynamic binding
  • Abstract class and methods
  • Interface
  • Abstract class vs interface
  • Encapsulation
  • Packages
  • Access modifiers
  • Garbage Collection
  • Inner classes
  • Static import
  • Static constructor

Java Exception Handling

  • Exception handling
  • Java try-catch
  • Java throw
  • Java throws
  • Checked and Unchecked Exceptions
  • Jav try catch finally
  • Exception Examples
  • Exception Propagation

Collections Framework

  • Collections in Java
  • Java ArrayList
  • Java LinkedList
  • Java Vector
  • Java HashSet
  • Java LinkedHashSet
  • Java TreeSet
  • Java HashMap
  • Java TreeMap
  • Java LinkedHashMap
  • Java Queue
  • Java PriorityQueue
  • Java Deque
  • Comparable interface
  • Comparator interface
  • Collections Interview Questions

MORE ...

  • Java Scanner Class
  • Java 8 Features
  • Java 9 Features
  • Java Conversion
  • Java Date
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations
  • Java main method
  • Java Interview Q

Copyright © 2012 – 2022 BeginnersBook . Privacy Policy . Sitemap