✍️ Set to List
@Getter
@AllArgsConstructor
@ToString
public class Member {
private int id;
private String name;
}
1. Java 8 Stream API
Set<Member> memberSet = new HashSet<>();
memberSet.add(new Member(1, "침착맨"));
memberSet.add(new Member(2, "주호민"));
memberSet.add(new Member(3, "김풍"));
List<Member> memberList = memberSet.stream().collect(Collectors.toList());
System.out.println(memberList);
// [Member(id=1, name=침착맨), Member(id=2, name=주호민), Member(id=3, name=김풍)]
2. Constructor
Set<Member> memberSet = new HashSet<>();
memberSet.add(new Member(1, "침착맨"));
memberSet.add(new Member(2, "주호민"));
memberSet.add(new Member(3, "김풍"));
List<Member> memberList = new ArrayList<>(memberSet);
System.out.println(memberList);
// [Member(id=1, name=침착맨), Member(id=3, name=김풍), Member(id=2, name=주호민)]
🌱 관련
[Java8] List를 Map으로 변환 (List to Map)
[Java8] List를 Set으로 변환 (List to Set)
[Java8] Set을 List로 변환 (Set to List)
[Java8] Set을 Map으로 변환 (Set to Map)
'Java > Java 8' 카테고리의 다른 글
[Java8] Map을 List로 변환 (Map to List) (0) | 2022.08.18 |
---|---|
[Java8] Set을 Map으로 변환 (Set to Map) (0) | 2022.08.18 |
[Java8] List를 Map으로 변환 (List to Map) (0) | 2022.08.16 |
[Java8] List를 Set으로 변환 (List to Set) (0) | 2022.08.16 |
[Java8] Chapter 6-5. CompletableFuture, 작업의 조합과 예외 처리 (0) | 2022.03.21 |