下面類中除去的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