赞
踩
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>() { @Override public 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( //lambda s->addAge(s) ); peopleStream2.forEach(s -> System.out.println(s)); } public People addAge(People people){ people.setAge(people.getAge()+1); return people; }
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; } @Override public String toString() { return "People{" + "name='" + name + '\'' + ", age=" + age + '}'; } }
Stream中of方法传入可变参数
Stream中map元素类型转化方法
List<String> names = personList.stream()
.map(person -> person.getName())
.collect(Collectors.toList());
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。