Explain Functional Interfaces and Streams in Java 8

Functional Interfaces and Streams in Java 8


Overview of Streams

Streams are a new abstraction introduced in Java 8 that allow for functional-style operations on collections of elements. They provide a way to process sequences of elements (like lists or sets) in a declarative manner.

Using Functional Interfaces with Streams

Functional interfaces play a crucial role in the Stream API, as they are used to define the behavior of operations such as filtering, mapping, and reducing.

1. Using Predicate with Streams

The filter() method uses a Predicate to determine which elements to include in the resulting stream.

<span>List</span><span><</span><span>String</span><span>></span> <span>names</span> <span>=</span> <span>Arrays</span><span>.</span><span>asList</span><span>(</span><span>"Alice"</span><span>,</span> <span>"Bob"</span><span>,</span> <span>"Charlie"</span><span>,</span> <span>"David"</span><span>);</span>
<span>List</span><span><</span><span>String</span><span>></span> <span>filteredNames</span> <span>=</span> <span>names</span><span>.</span><span>stream</span><span>()</span>
<span>.</span><span>filter</span><span>(</span><span>name</span> <span>-></span> <span>name</span><span>.</span><span>startsWith</span><span>(</span><span>"A"</span><span>))</span>
<span>.</span><span>collect</span><span>(</span><span>Collectors</span><span>.</span><span>toList</span><span>());</span>
<span>System</span><span>.</span><span>out</span><span>.</span><span>println</span><span>(</span><span>filteredNames</span><span>);</span> <span>// Output: [Alice]</span>
    <span>List</span><span><</span><span>String</span><span>></span> <span>names</span> <span>=</span> <span>Arrays</span><span>.</span><span>asList</span><span>(</span><span>"Alice"</span><span>,</span> <span>"Bob"</span><span>,</span> <span>"Charlie"</span><span>,</span> <span>"David"</span><span>);</span>
    <span>List</span><span><</span><span>String</span><span>></span> <span>filteredNames</span> <span>=</span> <span>names</span><span>.</span><span>stream</span><span>()</span>
                                          <span>.</span><span>filter</span><span>(</span><span>name</span> <span>-></span> <span>name</span><span>.</span><span>startsWith</span><span>(</span><span>"A"</span><span>))</span>
                                          <span>.</span><span>collect</span><span>(</span><span>Collectors</span><span>.</span><span>toList</span><span>());</span>
    <span>System</span><span>.</span><span>out</span><span>.</span><span>println</span><span>(</span><span>filteredNames</span><span>);</span> <span>// Output: [Alice]</span>
List<String> names = Arrays.asList("Alice", "Bob", "Charlie", "David"); List<String> filteredNames = names.stream() .filter(name -> name.startsWith("A")) .collect(Collectors.toList()); System.out.println(filteredNames); // Output: [Alice]

Enter fullscreen mode Exit fullscreen mode

2. Using Function with Streams

The map() method applies a function to each element in the stream, transforming it into another form.

<span>List</span><span><</span><span>Integer</span><span>></span> <span>lengths</span> <span>=</span> <span>names</span><span>.</span><span>stream</span><span>()</span>
<span>.</span><span>map</span><span>(</span><span>String:</span><span>:</span><span>length</span><span>)</span>
<span>.</span><span>collect</span><span>(</span><span>Collectors</span><span>.</span><span>toList</span><span>());</span>
<span>System</span><span>.</span><span>out</span><span>.</span><span>println</span><span>(</span><span>lengths</span><span>);</span> <span>// Output: [5, 3, 7, 5]</span>
    <span>List</span><span><</span><span>Integer</span><span>></span> <span>lengths</span> <span>=</span> <span>names</span><span>.</span><span>stream</span><span>()</span>
                                              <span>.</span><span>map</span><span>(</span><span>String:</span><span>:</span><span>length</span><span>)</span>
                                              <span>.</span><span>collect</span><span>(</span><span>Collectors</span><span>.</span><span>toList</span><span>());</span>
    <span>System</span><span>.</span><span>out</span><span>.</span><span>println</span><span>(</span><span>lengths</span><span>);</span> <span>// Output: [5, 3, 7, 5]</span>
List<Integer> lengths = names.stream() .map(String::length) .collect(Collectors.toList()); System.out.println(lengths); // Output: [5, 3, 7, 5]

Enter fullscreen mode Exit fullscreen mode

3. Using Consumer with Streams

The forEach() method takes a consumer that defines what to do with each element in the stream.

<span>names</span><span>.</span><span>stream</span><span>()</span>
<span>.</span><span>forEach</span><span>(</span><span>name</span> <span>-></span> <span>System</span><span>.</span><span>out</span><span>.</span><span>println</span><span>(</span><span>name</span><span>));</span> <span>// Prints each name</span>
    <span>names</span><span>.</span><span>stream</span><span>()</span>
         <span>.</span><span>forEach</span><span>(</span><span>name</span> <span>-></span> <span>System</span><span>.</span><span>out</span><span>.</span><span>println</span><span>(</span><span>name</span><span>));</span> <span>// Prints each name</span>
names.stream() .forEach(name -> System.out.println(name)); // Prints each name

Enter fullscreen mode Exit fullscreen mode

4. Using Supplier with Streams

A supplier can be used to provide initial values for operations like collecting results.

<span>Supplier</span><span><</span><span>List</span><span><</span><span>String</span><span>>></span> <span>listSupplier</span> <span>=</span> <span>ArrayList:</span><span>:</span><span>new</span><span>;</span>
<span>List</span><span><</span><span>String</span><span>></span> <span>collectedNames</span> <span>=</span> <span>names</span><span>.</span><span>stream</span><span>()</span>
<span>.</span><span>filter</span><span>(</span><span>name</span> <span>-></span> <span>name</span><span>.</span><span>length</span><span>()</span> <span>></span> <span>3</span><span>)</span>
<span>.</span><span>collect</span><span>(</span><span>listSupplier</span><span>,</span> <span>List:</span><span>:</span><span>add</span><span>,</span> <span>List:</span><span>:</span><span>addAll</span><span>);</span>
<span>System</span><span>.</span><span>out</span><span>.</span><span>println</span><span>(</span><span>collectedNames</span><span>);</span> <span>// Output: [Alice, Charlie]</span>
    <span>Supplier</span><span><</span><span>List</span><span><</span><span>String</span><span>>></span> <span>listSupplier</span> <span>=</span> <span>ArrayList:</span><span>:</span><span>new</span><span>;</span>
    <span>List</span><span><</span><span>String</span><span>></span> <span>collectedNames</span> <span>=</span> <span>names</span><span>.</span><span>stream</span><span>()</span>
                                            <span>.</span><span>filter</span><span>(</span><span>name</span> <span>-></span> <span>name</span><span>.</span><span>length</span><span>()</span> <span>></span> <span>3</span><span>)</span>
                                            <span>.</span><span>collect</span><span>(</span><span>listSupplier</span><span>,</span> <span>List:</span><span>:</span><span>add</span><span>,</span> <span>List:</span><span>:</span><span>addAll</span><span>);</span>
    <span>System</span><span>.</span><span>out</span><span>.</span><span>println</span><span>(</span><span>collectedNames</span><span>);</span> <span>// Output: [Alice, Charlie]</span>
Supplier<List<String>> listSupplier = ArrayList::new; List<String> collectedNames = names.stream() .filter(name -> name.length() > 3) .collect(listSupplier, List::add, List::addAll); System.out.println(collectedNames); // Output: [Alice, Charlie]

Enter fullscreen mode Exit fullscreen mode

5. Using BinarayOperator with Streams

The reduce() method uses a binary operator to combine elements into a single result.

<span>Optional</span><span><</span><span>Integer</span><span>></span> <span>totalLength</span> <span>=</span> <span>names</span><span>.</span><span>stream</span><span>()</span>
<span>.</span><span>map</span><span>(</span><span>String:</span><span>:</span><span>length</span><span>)</span>
<span>.</span><span>reduce</span><span>(</span><span>0</span><span>,</span> <span>Integer:</span><span>:</span><span>sum</span><span>);</span>
<span>System</span><span>.</span><span>out</span><span>.</span><span>println</span><span>(</span><span>totalLength</span><span>.</span><span>get</span><span>());</span> <span>// Output: 20</span>
    <span>Optional</span><span><</span><span>Integer</span><span>></span> <span>totalLength</span> <span>=</span> <span>names</span><span>.</span><span>stream</span><span>()</span>
                                                       <span>.</span><span>map</span><span>(</span><span>String:</span><span>:</span><span>length</span><span>)</span>
                                                       <span>.</span><span>reduce</span><span>(</span><span>0</span><span>,</span> <span>Integer:</span><span>:</span><span>sum</span><span>);</span>
    <span>System</span><span>.</span><span>out</span><span>.</span><span>println</span><span>(</span><span>totalLength</span><span>.</span><span>get</span><span>());</span> <span>// Output: 20</span>
Optional<Integer> totalLength = names.stream() .map(String::length) .reduce(0, Integer::sum); System.out.println(totalLength.get()); // Output: 20

Enter fullscreen mode Exit fullscreen mode

Conclusion

The integration of functional interfaces with the Stream API allows developers to write clean and efficient code for processing collections. By leveraging predicates, functions, consumers, suppliers, and binary operators, you can perform complex data manipulations with ease.

原文链接:Explain Functional Interfaces and Streams in Java 8

© 版权声明
THE END
喜欢就支持一下吧
点赞8 分享
Happiness is not something you postpone for the future; it is something you design for the present.
幸福不应该留到未来品尝,幸福是你专门为当下的自己所准备的
评论 抢沙发

请登录后发表评论

    暂无评论内容