는, 그 목록 안에 하위 목록의 순서를 셔플한다.
대신 모든 하위 목록을 임의로 이동하려면 각 하위 목록에 Collection.shuffle
(으)로 전화해야합니다.
요구 사항은 모든 하위 목록의 모든 요소를 섞고도 충분하지 않을 것입니다 위의 코드, 하위 목록 사이의 요소를 섞어 정말 인 경우 편집 된 질문 후
final List<List<String>> list = Arrays.asList(
Arrays.asList("A", "B", "C"),
Arrays.asList("X", "Y", "Z"),
Arrays.asList("1", "2", "3")
);
// 1. Will shuffle the order of the sub-lists
Collections.shuffle(list);
// 2.a. Will shuffle all the sub-lists
list.forEach(sublist -> Collections.shuffle(sublist));
// 2.b. Or the same, with method reference instead of lambda
list.forEach(Collections::shuffle);
편집. 당신이 요청으로
아래의 코드는 할 것이다, 그러나 (이 경우 3
에) 모든 하위 목록이 같은 크기이 있다고 가정합니다 :
// 1. Add all values in single dimension list
List<String> allValues = list.stream()
.flatMap(List::stream)
.collect(toList());
// 2. Shuffle all those values
Collections.shuffle(allValues);
// 3. Re-create the multidimensional List
List<List<String>> shuffledValues = new ArrayList<>();
for (int i = 0; i < allValues.size(); i = i + 3) {
shuffledValues.add(allValues.subList(i, i+3));
}
어떤 결과가 예상됩니까? – bcsb1001
모든 요소가 무작위로 셔플하게되는 arraylist –
나는 적응 한 질문을위한 해결책으로 나의 대답을 확장했다. – Ward