Java Collection Create createCollection(Collection col)

Here you can find the source of createCollection(Collection col)

Description

Create and answer a collection that is the same type, or the closest equivalent of col.

License

Open Source License

Parameter

Parameter Description
col a parameter

Declaration

public static Collection createCollection(Collection col) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.util.*;

public class Main {
    /**/*from   w w  w  .  j  a va 2s. co m*/
     * Create and answer a collection that is the same type, or the closest
     * equivalent of col.
     * 
     * @param col
     * @return
     */
    public static Collection createCollection(Collection col) {
        Collection answer;
        try {
            answer = (Collection) col.getClass().newInstance();
        } catch (Exception e) {
            if (col instanceof List) {
                answer = new ArrayList();
            } else if (col instanceof SortedSet) {
                answer = new TreeSet();
            } else if (col instanceof Set) {
                answer = new HashSet();
            } else {
                answer = new ArrayList(); // should this throw an exception?
            }
        }
        return answer;
    }
}

Related

  1. create(Collection coll)
  2. createAuthorString(Collection users)
  3. createCollection( final Iterable object)
  4. createCollection(Object[] objects)
  5. createCollection(T... items)
  6. createCollection(X o)
  7. createCollectionFromString(String value)