@ SuppressWarnings ( "unchecked" ) public static Collection < Object > getCollectionObj ( Class < ? > clazz ) { if ( clazz . isInterface ( ) ) { if ( clazz . isAssignableFrom ( List . class ) ) return new ArrayList <> ( ) ; else if ( clazz . isAssignableFrom ( Set . class ) ) return new HashSet <> ( ) ; else if ( clazz . isAssignableFrom ( Queue . class ) ) return new ArrayDeque <> ( ) ; else if ( clazz . isAssignableFrom ( SortedSet . class ) ) return new TreeSet <> ( ) ; else if ( clazz . isAssignableFrom ( BlockingQueue . class ) ) return new LinkedBlockingDeque <> ( ) ; else return null ; } else { Collection < Object > collection = null ; try { collection = ( Collection < Object > ) clazz . newInstance ( ) ; } catch ( Exception e ) { System . err . println ( "new collection instance exception, " + e . getMessage ( ) ) ; } return collection ; } }
Returns a collection object instance by class