Java8-Map

1、Staff实体

public class Staff {

    private String name;

    private int age;

    private String address;

    public Staff() {
    }

    public Staff(String name, int age, String address) {
        this.name = name;
        this.age = age;
        this.address = address;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    @Override
    public String toString() {
        return "Staff{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", address='" + address + '\'' +
                '}';
    }
}

2、将字符串列表转换为大写

public class TextJava8Map1 {

    public static void main(String[] args) {

        List<String> alpha = Arrays.asList("a", "b", "c", "d");
        // 在Java8之前
        List<String> alphaUpper = new ArrayList<>();
        for (String s :alpha) {
            alphaUpper.add(s.toUpperCase());
        }
        System.out.println(alpha);
        System.out.println(alphaUpper);

        // Java8 map
        List<String> collect1 = alpha.stream()
                .map(String::toUpperCase)
                .collect(Collectors.toList());
        System.out.println(collect1);
    }
}

3、获取List对象列表中某个属性的集合(Developer在博客-Java8比较器)

public class TextJava8Map2 {

    public static void main(String[] args) {

        List<Developer> developers = Arrays.asList(
                new Developer("java", new BigDecimal(23231), 32),
                new Developer("c++", new BigDecimal(32432), 30),
                new Developer("spring", new BigDecimal(23121), 34)

        );
        // 在Java8之前
        List<String> names1 = new ArrayList<>();
        for (Developer developer : developers) {
            names1.add(developer.getName());
        }
        System.out.println(names1);

        // Java8 map
        List<String> names2 = developers.stream()
                .map(developer -> developer.getName())
                .collect(Collectors.toList());
        System.out.println(names2);


    }
}

4、将一个List对象列表转换为另一个List对象列表

public class TextJava8Map3 {

    public static void main(String[] args) {

        List<Developer> developers = Arrays.asList(
                new Developer("java", new BigDecimal(23231), 32),
                new Developer("c++", new BigDecimal(32432), 30),
                new Developer("spring", new BigDecimal(23121), 34)

        );
        // 在Java8之前
        List<Staff> staffs = convertToStaff(developers);
        System.out.println(staffs);

        // Java8 map
        List<Staff> result = developers.stream()
                .map(developer -> {
                    Staff staff = new Staff();
                    staff.setName(developer.getName());
                    staff.setAge(developer.getAge());
                    if ("c++".equals(developer.getName())) {
                        staff.setAddress("us");
                    }
                    return staff;
                })
                .collect(Collectors.toList());
        System.out.println(result);
    }

    private static List<Staff> convertToStaff(List<Developer> developers) {

        List<Staff> staffs = new ArrayList<>();
        for (Developer developer :developers) {
            Staff staff = new Staff();
            staff.setName(developer.getName());
            staff.setAge(developer.getAge());
            if ("java".equals(developer.getName())) {
                staff.setAddress("us");
            }
            staffs.add(staff);
        }

        return staffs;
    }
}

猜你喜欢

转载自www.cnblogs.com/fengkunangel/p/10428653.html