一、stream filter過濾
List<Integer> numberList = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
List<Integer> evenNumbersList = numberList.stream()
.filter(n -> n % 2 == 0)
.collect(Collectors.toList());
System.out.println(evenNumbersList); //[2, 4, 6, 8, 10]
我們首先創建了一個包含1~10的整數列表(numberList),然後使用.stream()將其轉換為一個Stream對象。接著,我們使用.filter()方法過濾出列表中的偶數,並將結果存儲在一個新的列表(evenNumbersList)中,最後使用.collect()方法將結果轉換回List類型。
可以看到,Stream和Lambda表達式的結合使用,可以更加高效和簡潔地實現數據的過濾。
二、stream filter map
List<String> names = Arrays.asList("Alice", "Bob", "Charlie", "David", "Emily");
List<String> upperCaseNames = names.stream()
.filter(name -> name.startsWith("A"))
.map(String::toUpperCase)
.collect(Collectors.toList());
System.out.println(upperCaseNames); //[ALICE]
這裡我們定義了一個包含多個字元串的列表(names),然後使用.filter()方法過濾出以「A」開頭的字元串,並使用.map()方法將其轉換為大寫字母。最終我們得到一個只包含「ALICE」的List。
在這個例子中,使用map()方法返回的結果類型被顯式地指定為String類型。
三、stream流的filter
Stream<String> namesStream = Stream.of("Alice", "Bob", "Charlie", "David", "Emily");
Stream<String> upperCaseNamesStream = namesStream.filter(name -> name.startsWith("A"))
.map(String::toUpperCase);
System.out.println(upperCaseNamesStream.collect(Collectors.toList())); //[ALICE]
這個例子中,我們使用Stream.of()方法創建了一個包含多個字元串的流(namesStream)。同樣地,使用.filter()方法過濾出以「A」開頭的字元串,並使用.map()方法將其轉換為大寫字母。最終,使用collect()方法將結果轉換為List類型。需要注意的是,流只能被消費一次,如果我們嘗試在上面的代碼中列印upperCaseNamesStream,將會得到一個空的列表。
四、stream的filter方法詳解
Stream<Integer> numberStream = Stream.of(1, 2, 3, 4, 5, 6, 7, 8);
Stream<Integer> evenNumbersStream = numberStream.filter(n -> n % 2 == 0);
evenNumbersStream.forEach(n -> System.out.print(n + " ")); //2 4 6 8
在這個例子中,我們使用Stream.of()方法創建了一個包含多個數字的流(numberStream)。然後,我們使用.filter()方法過濾出偶數,並使用.forEach()方法將其列印出來。
需要注意的是,我們沒有將結果存儲在一個列表中,而是直接將其列印出來。因為Stream是惰性求值的,只有在終止操作(如forEach())被觸發時才會執行中間操作(如filter())。
五、list的filter方法
List<String> names = Arrays.asList("Alice", "Bob", "Charlie", "David", "Emily");
List<String> filteredNames = names.stream()
.filter(name -> name.length() <= 4)
.collect(Collectors.toList());
System.out.println(filteredNames); //[Bob, David, Emily]
這個例子中,我們定義了一個包含多個字元串的列表(names)。使用.stream()方法將其轉換為一個Stream對象,接著,使用.filter()方法過濾出長度小於或等於4的字元串,並使用.collect()方法將結果轉換為一個新的List對象。
六、stream filter 用法
List<String> names = Arrays.asList("Alice", "Bob", "Charlie", "David", "Emily");
Predicate<String> startsWithAPredicate = name -> name.startsWith("A");
Predicate<String> lengthLessThanFivePredicate = name -> name.length() < 5;
List<String> filteredNamesWithPredicates = names.stream()
.filter(startsWithAPredicate.and(lengthLessThanFivePredicate))
.collect(Collectors.toList());
System.out.println(filteredNamesWithPredicates); //[Alice]
這個例子中,我們首先定義了兩個Predicate對象(startsWithAPredicate和lengthLessThanFivePredicate),分別用於判斷字元串是否以「A」開頭和長度是否小於5。接著使用.filter()方法將這兩個Predicate組合起來,過濾出符合條件的字元串,並使用.collect()方法將結果轉換為一個新的List對象。
七、list stream過濾
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8);
List<Integer> evenNumbers = numbers.stream()
.filter(n -> n % 2 == 0)
.collect(Collectors.toList());
System.out.println(evenNumbers); //[2, 4, 6, 8]
這個例子中,我們定義了一個包含多個整數的列表(numbers),然後使用.stream()將其轉換為一個流對象,再使用.filter()方法過濾出偶數,並使用.collect()方法將結果轉換為一個新的List對象。
八、list使用stream過濾數據
List<Person> persons = new ArrayList<>();
persons.add(new Person("Alice", "Female"));
persons.add(new Person("Bob", "Male"));
persons.add(new Person("Charlie", "Male"));
persons.add(new Person("David", "Male"));
persons.add(new Person("Emily", "Female"));
List<Person> females = persons.stream()
.filter(p -> p.getGender().equals("Female"))
.collect(Collectors.toList());
System.out.println(females); //[Person{name='Alice', gender='Female'}, Person{name='Emily', gender='Female'}]
這個例子中,我們首先定義了一個包含多個Person對象的列表(persons)。其中,每一個Person對象包含一個姓名(name)和性別(gender)。我們使用.stream()方法將其轉換為一個流對象,接著使用.filter()方法過濾出性別為「Female」的Person對象,並使用.collect()方法將結果轉換為一個新的List對象。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/285257.html