Stream流中map方法
- 一、Stream流中map方法
- 1、代码举例
- 2、实体类
- 3、效果
- 二、其他
一、Stream流中map方法
1、代码举例
public void test() throws ParseException {List<People> peopleList = new ArrayList<People>();People people1 = new People("张三", 18);People people2 = new People("王五", 19);peopleList.add(people1);peopleList.add(people2);Stream<String> peopleStream = peopleList.stream().map(//使用匿名接口Function重写抽象方法apply,参数一为传入类型,参数二为传出类型new Function<People, String>() {@Overridepublic String apply(People people) {people.setName("姓名:" + people.getName());return people.getName();}});peopleStream.forEach(s -> System.out.println(s));System.out.println("========================================================");Stream<People> peopleStream2 = peopleList.stream().map(//lambdas->addAge(s));peopleStream2.forEach(s -> System.out.println(s));}public People addAge(People people){people.setAge(people.getAge()+1);return people;}
2、实体类
public class People {private String name;private int age;public People(String name, int age) {this.name = name;this.age = age;}public People() {}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;}@Overridepublic String toString() {return "People{" +"name='" + name + '\'' +", age=" + age +'}';}
}
3、效果
二、其他
Stream中of方法传入可变参数
Stream中map元素类型转化方法