Skip to content

Instantly share code, notes, and snippets.

@cramertj
Created February 24, 2015 20:16
Show Gist options
  • Save cramertj/6a221fb8e167ea16cab4 to your computer and use it in GitHub Desktop.
Save cramertj/6a221fb8e167ea16cab4 to your computer and use it in GitHub Desktop.
Shuffle String in One Line
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