@ SuppressWarnings ( "all" ) public static < T > Set < T > toTypedSet ( Object data , Class < T > type ) { Set < T > setResult = new HashSet <> ( ) ; if ( data == null ) { return setResult ; } if ( data instanceof Collection ) { Collection collection = ( Collection ) data ; for ( Object o : collection ) { setResult . add ( Reflection . toType ( o , type ) ) ; } return setResult ; } if ( data . getClass ( ) . isArray ( ) ) { for ( Object o : ArrayUtil . toObjectArray ( data ) ) { setResult . add ( Reflection . toType ( o , type ) ) ; } return setResult ; } setResult . add ( Reflection . toType ( data , type ) ) ; return setResult ; }
Adapt the data object to the given typed hash set it is safe to make a cast even if the given data is not a set or the element is not the given type .