入门lambda(四) Streams map() example

stream().mapToDouble()  转换为double的Stream

stream().map() 可以转换任意类型的Stream

package stream.map;

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

/**
 * Created by Administrator on 2019/4/10/010.
 */
public class UseMapToConvertToStringList {
    public static void main(String[] args){
        List developerList = Arrays.asList(new Developer("cxy",new BigDecimal("10000"),21),
                new Developer("da",new BigDecimal("10000"),31),
                new Developer("fad",new BigDecimal("10000"),41),
                new Developer("ef",new BigDecimal("10000"),56),
                new Developer("ij",new BigDecimal("10000"),74),
                new Developer("ui",new BigDecimal("10000"),13));

        List nameList = developerList.stream().map(o->o.getName()).collect(Collectors.toList());
        System.out.println(nameList);
    }
}

 

比如,根据程序员的年龄,发放奖金,有了 List  deveLists  了;

使用stream().map() 可以迅速转换成 List awardLists

程序员类:

package stream.map;

import java.math.BigDecimal;

/**
 * Created by Administrator on 2019/4/9/009.
 */
public class Developer {
    private String name;
    private BigDecimal salary;
    private Integer age;

    public Developer(String name, BigDecimal salary, Integer age) {
        this.name = name;
        this.salary = salary;
        this.age = age;
    }

    public String getName() {
        return name;
    }

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

    public BigDecimal getSalary() {
        return salary;
    }

    public void setSalary(BigDecimal salary) {
        this.salary = salary;
    }

    public Integer getAge() {
        return age;
    }

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

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

Award 奖励类

package stream.map;

import java.math.BigDecimal;

/**
 * Created by Administrator on 2019/4/10/010.
 */
public class Award {
    private String name;
    private BigDecimal savings;  //savings = salary*age

    public Award() {
        super();
    }

    public Award(String name, BigDecimal savings) {
        this.name = name;
        this.savings = savings;
    }

    public String getName() {
        return name;
    }

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

    public BigDecimal getSavings() {
        return savings;
    }

    public void setSavings(BigDecimal savings) {
        this.savings = savings;
    }

    @Override
    public String toString() {
        return "Award{" +
                "name='" + name + '\'' +
                ", savings=" + savings +
                '}';
    }
}
package stream.map;

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

/**
 * 转成别的对象stream.不要一个一个的转了
 */
public class ConvertToOtherObj {
    public static void main(String[] args){
        List developerList = Arrays.asList(new Developer("cxy",new BigDecimal("10000"),21),
                new Developer("da",new BigDecimal("10000"),31),
                new Developer("fad",new BigDecimal("10000"),41),
                new Developer("ef",new BigDecimal("10000"),56),
                new Developer("ij",new BigDecimal("10000"),74),
                new Developer("ui",new BigDecimal("10000"),13));
         developerList.forEach(System.out::println);
             System.out.println("抓换后");
        List awardList = developerList.stream().map(o->{
            Award award = new Award();
            award.setName(o.getName());
            /*BigDecimal 做乘法需要 a.multiply(b)方法*/
            BigDecimal year = new BigDecimal(o.getAge());
            BigDecimal savings = o.getSalary().multiply(year);
            award.setSavings(savings);
            return award;
        }).collect(Collectors.toList());

        awardList.forEach(System.out::println);
    }
}

入门lambda(四) Streams map() example_第1张图片

 

你可能感兴趣的:(入门lambda(四) Streams map() example)