下面类中除去的lambda我有如下所示其使用Stream
S中的下面的java类。我有一个名为person的基本班级,然后还有其他班级,我正在使用Stream
s。从在Java
有没有办法通过它我可以避开lambda表达式并删除它们?
public class StreamMapDemo
{
public static void main(String[] args)
{
List<Person> personList = new ArrayList<>();
personList.add(new Person("Alice", "[email protected]", Gender.FEMALE, 16));
personList.add(new Person("Bob", "[email protected]", Gender.MALE, 15));
personList.add(new Person("Carol", "[email protected]", Gender.FEMALE, 23));
personList.add(new Person("David", "[email protected]", Gender.MALE, 19));
personList.add(new Person("Eric", "[email protected]", Gender.MALE, 26));
personList.stream() // Stream<Person>
.map(p -> p.getEmail()) // Stream<String>
.forEach(System.out::println);
System.out.println("\n----------------------\n");
personList.stream() // Stream<Person>
.map(p -> p.getName().toUpperCase()) // Stream<String>
.forEach(System.out::println);
System.out.println("\n----------------------\n");
personList.stream() // Stream<Person>
.mapToInt(p -> p.getAge()) //IntStream
.forEach(age -> System.out.println(age));
}
}
输出
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
----------------------
ALICE
BOB
CAROL
DAVID
ERIC
----------------------
16
15
23
19
26
使用常规循环转换的代码。 –
删除lamda或流? – azro
如果您说*为什么要删除lambda表达式可能会有所帮助。这里有些人不清楚,如果你想删除只是lambda或流。而且人们不得不想知道lambda表达式有什么问题。 – RealSkeptic