11:40 am - Wednesday May 24, 2017

Java Program is used for memory optimization. It trims the capacity ofArrayList to the current list size using trimToSize() method

CANDID INDUSTRIAL TRAINING -- CHENNAI
Corporate Training for Experienced Candidate
Struts | Hibernate | Spring | Java / J2EE
SOAP | RestFull | Design Pattern | more...
Ph: +91 72000 69003
137b, 2nd st, shanthi nagar, Chrompet, Chennai -600044

 

BACK

Java ArrayList trimToSize() Method exampletrimToSize() method is used for memory optimization. It trims the capacity ofArrayList to the current list size.

Example Program


import java.util.ArrayList;

public class Trim {
	public static void main(String args[]) {
		ArrayList<Integer> at = new ArrayList<Integer>(50);
		at.add(1);
		at.add(2);
		at.add(3);
		at.add(4);
		at.add(5);
		at.add(6);
		at.add(7);
		at.add(1);
		at.add(1);
		at.add(1);
		at.trimToSize();
		System.out.println(at);
	}
}


Output:

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

Explanation:    

     public void trimTo  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)   .The java.util.ArrayList.trimToSize() method trims the capacity of this ArrayList instance to be the list’s current size. An application can use this operation to minimize the storage of an ArrayList instance.

Parameter:

Na

Return value:

This method does not return any value.

Exception:

Na

Filed in: Z-A All Java Codes

Comments are closed.