Notification

No New notification

Learn to code with PrepInsta

Check PrepInsta Coding Blogs, Core CS, DSA etc

One Subscription, For Everyone

Get 200+ Courses under one Subscription

  • aptitude
  • aws_cloud
  • competitive-coding
  • dbms
  • logical_reasoning
  • operating-system
  • verbal
  • tcs_nqt
  • data_science
  • ethical_hacking
  • dsa
  • verbal
  • basic_coding
  • python
  • cpp
  • java
  • basic_coding
  • python
Explore PrepInsta Prime

Never Miss an OffCampus Update

Get OffCampus Updates on Social Media from PrepInsta

Java ArrayList trimToSize() Method

ArrayList trimToSize()

What is ArrayList in Java ?

Arraylist is a part of collection framework in java. It contains several inbuilt function like trimToSize()method which can be found in the java.util.ArrayList Package. It implements the List interface of the collections framework.

Java ArrayList trimToSize() Method :

The Java ArrayList class’s trimToSize() method reduces an instance’s capacity to match the list’s current size. Using this method, you can reduce the size of an ArrayList instance to the number of elements it has.

Syntax  :

public void trimToSize() 
java ArrayList trimeToSize() Method

Example 1 :  

Run

import java.util.*; public class Main { public static void main(String[] args) { //ArrayList object creation of size 8. ArrayList<String> list = new ArrayList<>(8); //Adding elements to the list list.add("A"); list.add("B"); list.add("C"); list.add("D"); list.add("E"); list.add("F"); //Printing the initial list. System.out.println("ArrayList : " + list); //Trimming the list to size list.trimToSize(); //Printing the list after trimToSize() System.out.println("ArrayList after trimToSize() : " + list); } } 

Output :

ArrayList : [A, B, C, D, E, F] ArrayList after trimToSize() : [A, B, C, D, E, F] 

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription