How do I trim the capacity of an ArrayList?

For minimizing the storage used by an ArrayList to the number of it’s elements we can use the ArrayList.trimToSize() method call. In the code below we create an instance of ArrayList with the initial capacity set to 100. Later we only add five data into the list. To make the capacity of the ArrayList minimized we call the trimToSize() method.

package org.kodejava.example.util;

import java.util.ArrayList;

public class ListTrimToSize {
    public static void main(String[] args) {
        // Create an ArrayList with the initial capacity of 100.
        ArrayList<String> list = new ArrayList<>(100);
        list.add("A");
        list.add("B");
        list.add("C");
        list.add("D");
        list.add("E");
        list.add("F");

        System.out.println("Size: " + list.size());

        // Trim the capacity of the ArrayList to the size
        // of the ArrayList. We can do this operation to reduce
        // the memory use to store data by the ArrayList.
        list.trimToSize();
    }
}

How do I remove trailing white space from a string?

The trim() method of a String class removes both leading and trailing white space from a string. In this example we use a regular expression to remove only the trailing white spaces from a string.

ppackage org.kodejava.example.lang;

public class TrailingSpace {
    public static void main(String[] args) {
        String text = "     tattarrattat     ";
        System.out.println("Original      = " + text);
        System.out.println("text.length() = " + text.length());

        // Using a regular expression to remove only the trailing white space in
        // a string
        text = text.replaceAll("\\s+$", "");
        System.out.println("Result        = " + text);
        System.out.println("text.length() = " + text.length());
    }
}
Original      =      tattarrattat     
text.length() = 22
Result        =      tattarrattat
text.length() = 17

How do I remove leading white space from a string?

The trim() method of a String class removes both leading and trailing white space from a string. In this example we use a regular expression to remove only the leading white spaces from a string.

package org.kodejava.example.lang;

public class LeadingSpace {
    public static void main(String[] args) {
        String text = "     tattarrattat     ";
        System.out.println("Original      = " + text);
        System.out.println("text.length() = " + text.length());

        // Using regular expression to remove only the leading white
        // space in string
        text = text.replaceAll("^\\s+", "");
        System.out.println("Result        = " + text);
        System.out.println("text.length() = " + text.length());
    }
}
Original      =      tattarrattat     
text.length() = 22
Result        = tattarrattat     
text.length() = 17

How do I remove leading and trailing white space from string?

package org.kodejava.example.lang;

public class TrimStringExample {
    public static void main(String[] args) {
        String text = "     tattarrattat     ";
        System.out.println("Original      = " + text);
        System.out.println("text.length() = " + text.length());

        // The trim() method will result a new string object with a leading and
        // trailing while space removed.
        text = text.trim();
        System.out.println("Result        = " + text);
        System.out.println("text.length() = " + text.length());
    }
}

The result of the code snippet above:

Original      =      tattarrattat     
text.length() = 22
Result        = tattarrattat
text.length() = 12