Example usage for java.util ArrayList trimToSize

List of usage examples for java.util ArrayList trimToSize

Introduction

In this page you can find the example usage for java.util ArrayList trimToSize.

Prototype

public void trimToSize() 

Source Link

Document

Trims the capacity of this ArrayList instance to be the list's current size.

Usage

From source file:MainClass.java

public static void main(String[] a) {

    ArrayList list = new ArrayList();

    list.add("A");
    list.ensureCapacity(10);//from   ww  w .ja  va 2  s .  c o  m
    list.trimToSize();
}

From source file:Main.java

public static void main(String args[]) {

    ArrayList<String> arrList = new ArrayList<>(Arrays.asList("a", "a", "a", "from java2s.com"));

    // Trim the arraylist
    arrList.trimToSize();

    System.out.println(arrList);// w  w w. ja  v a 2s  .  c  om
}

From source file:MainClass.java

public static void main(String[] args) {
    ArrayList myList = new ArrayList(5);
    for (int i = 0; i < 5; i++) {
        myList.add(new Integer(i));
    }//w  w  w .  j  a  v  a2 s  .  c o  m
    System.out.println("List contains " + myList.size() + " elements");

    Integer int2 = new Integer(2);
    System.out.println("List contains Integer(2): " + myList.contains(int2));
    System.out.println("Integer(2) is at index " + myList.indexOf(int2));

    myList.set(2, new Integer(99));
    System.out.println("Get element at index 2: " + myList.get(2));

    myList.ensureCapacity(15);
    for (int i = 10; i < 15; i++) {
        myList.add(new Integer(i));
    }

    myList.subList(10, 15).clear();
    myList.trimToSize();

    System.out.println(myList);
}

From source file:Main.java

public static <T> List<T> asConstList(Collection<T> in) {
    ArrayList<T> ret = new ArrayList(in);
    ret.trimToSize();
    return Collections.unmodifiableList(ret);
}

From source file:Main.java

public static <T> List<T> toUnmodifiableList(Iterable<? extends T> i) {
    ArrayList<T> result = toArrayList(i);
    result.trimToSize();
    return Collections.unmodifiableList(result);
}

From source file:Main.java

public static <E> ArrayList<E> createArrayList(Iterable<? extends E> iter) {

    if (iter instanceof Collection<?>) {
        return new ArrayList<E>((Collection<? extends E>) iter);
    }//from  ww  w . java 2 s .  c o m

    ArrayList<E> list = new ArrayList<E>();

    iterableToCollection(iter, list);

    list.trimToSize();

    return list;
}

From source file:Main.java

/**
 *Description:// w  ww.  ja  v a 2 s .c  om
 */
public static <T> ArrayList<T> createArrayList(Iterable<? extends T> c) {
    ArrayList<T> list;
    if ((c instanceof Collection)) {
        list = new ArrayList((Collection) c);
    } else {
        list = new ArrayList();
        iterableToCollection(c, list);
        list.trimToSize();
    }
    return list;
}

From source file:Main.java

public static <T> ArrayList<T> createArrayList(Iterable<? extends T> c) {
    ArrayList<T> list;

    if (c instanceof Collection<?>) {
        list = new ArrayList<T>((Collection<? extends T>) c);
    } else {//w  w  w.jav  a  2  s .co  m
        list = new ArrayList<T>();

        iterableToCollection(c, list);

        list.trimToSize();
    }

    return list;
}

From source file:Main.java

public static <T> List<T> merge(T[]... many) {
    ArrayList<T> ret = new ArrayList();
    for (T[] list : many) {
        if (list != null) {
            for (T val : list) {
                if (!ret.contains(val)) {
                    ret.add(val);
                }//  www. java  2s  .  c  o m
            }
        }
    }
    ret.trimToSize();
    return ret;
}

From source file:Main.java

public static <T> List<T> merge(List<T>... many) {
    ArrayList<T> ret = new ArrayList();
    for (List<T> list : many) {
        if (list != null) {
            for (T val : list) {
                if (!ret.contains(val)) {
                    ret.add(val);
                }//from   w  ww.  j ava2 s  .c  om
            }
        }
    }
    ret.trimToSize();
    return ret;
}