[Java8] Set을 List로 변환 (Set to List)

 

✍️ 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)

[Java8] Map을 List로 변환 (Map to List)

[Java8] Map을 Set으로 변환 (Map to Set)