Created
February 24, 2015 20:16
-
-
Save cramertj/6a221fb8e167ea16cab4 to your computer and use it in GitHub Desktop.
Shuffle String in One Line
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.lang.reflect.InvocationTargetException; | |
import java.lang.reflect.Method; | |
import java.util.Arrays; | |
import java.util.Collections; | |
import java.util.List; | |
import org.apache.commons.lang3.ArrayUtils; | |
public class Main { | |
public static void main(String[] args) { | |
String original = "original"; | |
String shuffled; | |
List<Character> list; | |
try { | |
shuffled = (null == Collections.class.getDeclaredMethod( | |
"shuffle", List.class ).invoke( | |
null, | |
list = Arrays.asList( ArrayUtils.toObject( | |
original.toCharArray()) ))) ? | |
String.copyValueOf( ArrayUtils.toPrimitive( | |
( Character[] ) list.toArray() ) ) : null; | |
System.out.println(shuffled); | |
} catch ( Exception e ) { | |
e.printStackTrace(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment