package stream;

import lombok.Getter;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class StreamExample13 {
    public static void main(String[] args) {
        List<Person> people = Arrays.asList(
                new Person("Alice"),
                new Person("Bob"),
                new Person("Charlie")
        );

        // 使用 Collectors.joining 连接名字，添加前缀和后缀
        String result = people.stream()
                .map(Person::getName) // 提取名字
                .collect(Collectors.joining(", ", "[", "]")); // 使用逗号和空格作为分隔符，添加前缀和后缀

        System.out.println("Names: " + result);// Names: [Alice, Bob, Charlie]
    }

    @Getter
    static class Person {
        private final String name;

        public Person(String name) {
            this.name = name;
        }

        @Override
        public String toString() {
            return name;
        }
    }
}