mardi 11 juin 2019

One Line Java String Random Permutation

I'd like a nice one line way of generating a random permutation of a Java String. This is what I have so far, using Java 8 Streams

public static void main(String[] args) {

    List<Character> charList = "abcd".chars().mapToObj(i -> (char) i).collect(Collectors.toList());
    Collections.shuffle(charList);
    String string =  charList.stream().map(String::valueOf).collect(Collectors.joining()); 
    System.out.println(string);
}

Any way to make this code shorter / simpler would be appreciated.




Aucun commentaire:

Enregistrer un commentaire