热门面试题与答案和在线测试
面向面试准备、在线测试、教程与实战练习的学习平台

通过聚焦学习路径、模拟测试和面试实战内容持续提升技能。

WithoutBook 将分主题面试题、在线练习测试、教程和对比指南整合到一个响应式学习空间中。

面试准备

Java 8 面试题与答案

Test your skills through the online practice test: Java 8 Quiz Online Practice Test

相关差异对比

Java 7 vs Java 8Java 8 vs Java 9

问题 26. What Is a Stream? How Does It Differ from a Collection?

  • In simple terms, a stream is an iterator whose role is to accept a set of actions to apply on each of the elements it contains.
  • The stream represents a sequence of objects from a source such as a collection, which supports aggregate operations. They were designed to make collection processing simple and concise. Contrary to the collections, the logic of iteration is implemented inside the stream, so we can use methods like map and flatMap for performing a declarative processing.
  • And yet another important distinction from collections is that streams are inherently lazily loaded and processed.
  • Another difference is that the Stream API is fluent and allows pipelining:  
    • int sum = Arrays.stream(new int[]{1, 2, 3})
        .filter(i -> i >= 2)
        .map(i -> i * 3)
        .sum();

这有帮助吗? 添加评论 查看评论
 

问题 27. What Is the Difference Between Map and flatMap Stream Operation?

  • There is a difference in signature between map and flatMap. Generally speaking, a map operation wraps its return value inside its ordinal type while flatMap does not.
  • For example, in Optional, a map operation would return Optional<String> type while flatMap would return String type.
  • So after mapping, one needs to unwrap (read “flatten”) the object to retrieve the value whereas, after flat mapping, there is no such need as the object is already flattened. The same concept is applied to mapping and flat mapping in Stream.
  • Both map and flatMap are intermediate stream operations that receive a function and apply this function to all elements of a stream.
  • The difference is that for the map, this function returns a value, but for flatMap, this function returns a stream. The flatMap operation “flattens” the streams into one.

这有帮助吗? 添加评论 查看评论
 

问题 28. What is Stream Pipelining in Java 8?

  • Stream pipelining is the concept of chaining operations together. This is done by splitting the operations that can happen on a stream into two categories: intermediate operations and terminal operations.
  • Each intermediate operation returns an instance of Stream itself when it runs, an arbitrary number of intermediate operations can, therefore, be set up to process data forming a processing pipeline.
  • There must then be a terminal operation which returns a final value and terminates the pipeline.

这有帮助吗? 添加评论 查看评论
 

问题 29. What does the flatmap() function do? why you need it?

The flatmap function is an extension of the map function. Apart from transforming one object into another, it can also flatten it.
For example, if you have a list of the list but you want to combine all elements of lists into just one list. In this case, you can use flatMap() for flattening. At the same time, you can also transform an object like you do use map() function.

public static void main(String[] args) {
            String[][] data = new String[][]{{"a", "b"}, {"c", "d"}, {"e", "f"}};
            //Stream<String[]>
            Stream<String[]> temp = Arrays.stream(data);
            //Stream<String>, GOOD!
            Stream<String> stringStream = temp.flatMap(x -> Arrays.stream(x));
            Stream<String> stream = stringStream.filter(x -> "a".equals(x.toString()));
            stream.forEach(System.out::println);
       }

这有帮助吗? 添加评论 查看评论
 

问题 30. What is the parallel Stream? How can you get a parallel stream from a List?

A parallel stream can parallel execute stream processing tasks. For example, if you have a parallel stream of 1 million orders and you are looking for orders worth more than 1 million, then you can use a filter to do that.
Unlike sequential Stream, the parallel Stream can launch multiple threads to search for those orders on the different part of Stream and then combine the result.
In short, the parallel Stream can paralyze execution.

这有帮助吗? 添加评论 查看评论
 

用户评价最有帮助的内容:

版权所有 © 2026,WithoutBook。