[java] Collections.shuffle(List<?> list) 리스트 내부 아이템 섞기

2022. 1. 4. 13:44IT개발/Java

반응형

List 아이템 섞기

List<String> students = Arrays.asList("홍길동", "김철수", "이영희", "홍길순");
Collections.shuffle(students);

 

Map => List 변환후, 아이템 섞기

Map<Integer, String> studentsById = new HashMap<>();
studentsById.put(1, "홍길동");
studentsById.put(2, "김철수");
studentsById.put(3, "이영희");
studentsById.put(4, "홍길순");

List<Map.Entry<Integer, String>> shuffledStudentEntries = new ArrayList<>(studentsById.entrySet());
Collections.shuffle(shuffledStudentEntries);

List<String> shuffledStudents = shuffledStudentEntries.stream()
.map(Map.Entry::getValue)
.collect(Collectors.toList());

Set => List 변환후, 아이템 섞기

Set<String> students = new HashSet<>(Arrays.asList("홍길동", "김철수", "이영희", "홍길순"));
List<String> studentList = new ArrayList<>(students);
Collections.shuffle(studentList);

 

반응형