当前位置: 代码迷 >> 综合 >> 入门lambda(四) Streams map() example
  详细解决方案

入门lambda(四) Streams map() example

热度:87   发布时间:2023-12-29 16:59:25.0

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<Developer> 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<String> nameList = developerList.stream().map(o->o.getName()).collect(Collectors.toList());System.out.println(nameList);}
}

 

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

使用stream().map() 可以迅速转换成 List<Award> 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;}@Overridepublic 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*agepublic 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;}@Overridepublic 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<Developer> 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<Award> 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);}
}

 

  相关解决方案