beginnersbook.com

  • Home
  • All Tutorials
    • Learn Servlet
    • Learn JSP
    • Learn JSTL
    • Learn C
    • Learn C++
    • Learn MongoDB
    • Learn XML
    • Learn Python
    • Learn Perl
    • Learn Kotlin
  • Core Java
  • OOPs
  • Collections
  • Java I/O
  • JSON
  • DBMS

Java ArrayList trimToSize() Method example

By Chaitanya Singh | Filed Under: Java Collections

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]

Enjoyed this post? Try these related posts

  1. Java ArrayList indexOf() Method example
  2. How to join/combine two ArrayLists in java
  3. How to synchronize ArrayList in java with example
  4. Search elements in Vector using index – Java example
  5. How to serialize ArrayList in java
  6. How to serialize HashMap in java

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 *

ArrayList Tutorial

  • Java ArrayList

Java ArrayList Methods

  • add(Object obj)
  • add(int index, Object element)
  • addAll(Collection c)
  • addAll(int index, Collection c)
  • contains()
  • get()
  • indexOf()
  • ensureCapacity()
  • isEmpty()
  • lastIndexOf()
  • remove()
  • remove(Object obj)
  • trimToSize()
  • set()
  • clone()
  • clear()
  • size()

Copyright © 2012 – 2021 BeginnersBook . Privacy Policy . Sitemap