How to shuffle characters in a string without using Collections.shuffle(…)?

后端 未结 14 2285
南旧
南旧 2020-11-27 07:19

How do I shuffle the characters in a string (e.g. hello could be ehlol or lleoh or ...). I don\'t want to use the Collections.shuffle(...) method, is there anyt

14条回答
  •  天命终不由人
    2020-11-27 08:03

            String shuffled;
            do {
                shuffled = Stream.of(text.split("")).sorted((o1, o2) -> ThreadLocalRandom.current().nextInt(3) - 1).collect(Collectors.joining());
            }while(shuffled.equals(text));
    

提交回复
热议问题