java se面试题
In this post, we are going to discuss some important Java SE 8 Interview Questions with Answers. I will write one more post to discuss the remaining Java SE 8 Interview Questions.
在本文中,我们将讨论一些重要的Java SE 8面试问题和答案。 我将再写一篇文章,讨论其余的Java SE 8面试问题。
In this section, we will pick up each question from the previous section and answer it with an in-detailed description. If you need any more information and examples, please go through previous Java SE 8 posts available in JournalDEV.
在本节中,我们将从上一节中选取每个问题,并以详细的说明回答。 如果您需要更多信息和示例,请阅读JournalDEV中提供的Java SE 8以前的文章。
Oracle Corporation has introduced a lot of new concepts in Java SE 8 to introduce the following benefits:
Oracle Corporation在Java SE 8中引入了许多新概念,以带来以下好处:
Recently, we can observe drastic changes in Hardware. Nowadays, all systems are using Multi-Core CPUs (2,4,8,16-Core, etc.) to deploy and run their applications. We need new Programming Constructs in Java to utilize these Multi-Core Processors efficiently to develop Highly Concurrently and Highly Scalable applications.
最近,我们可以观察到硬件的巨大变化。 如今,所有系统都在使用多核CPU(2、4、8、16核等)来部署和运行其应用程序。 我们需要Java中的新编程构造来有效利用这些多核处理器来开发高度并发和高度可扩展的应用程序。
Oracle Corporation has introduced a lot of FP(Functional Programming) concepts as part of Java SE 8 to utilize the advantages of FP.
作为Java SE 8的一部分,Oracle Corporation引入了许多FP(功能性编程)概念,以利用FP的优势。
We can get the following benefits from Java SE 8 New Features:
我们可以从Java SE 8新功能中获得以下好处:
Lambda Expression is an anonymous function which accepts a set of input parameters and returns results.
Lambda Expression是一个匿名函数,它接受一组输入参数并返回结果。
Lambda Expression is a block of code without any name, with or without parameters and with or without results. This block of code is executed on demand.
Lambda表达式是没有任何名称,带有或不带有参数以及带有或不带有结果的代码块。 此代码块按需执行。
A Lambda Expression contains 3 parts:
Lambda表达式包含3个部分:
A Lambda Expression can contain zero or one or more parameters. It is optional.
Lambda表达式可以包含零个或一个或多个参数。 它是可选的。
“->” is known as Lambda Arrow operator. It separates the parameters list and body.
“->”被称为Lambda Arrow运算符。 它将参数列表和主体分开。
The type of “Journal Dev” is java.lang.String. The type of “true” is Boolean. In the same way, what is the type of a Lambda Expression?
The Type of a Lambda Expression is a Functional Interface.
“ Journal Dev”的类型为java.lang.String。 “ true”的类型是布尔值。 同样,Lambda表达式的类型是什么?
Lambda表达式的类型是功能接口 。
Example:- What is the type of the following Lambda Expression?
示例:-以下Lambda表达式的类型是什么?
() -> System.out.println("Hello World");
This Lambda Expression does not have parameters and does return any results. So it’s type is “java.lang.Runnable” Functional Interface.
此Lambda表达式没有参数,并且会返回任何结果。 所以它的类型是“ java.lang.Runnable”功能接口。
A Functional Interface is an interface, which contains one and only one abstract method. Functional Interface is also known as SAM Interface because it contains only one abstract method.
功能接口是一个接口,它仅包含一个抽象方法。 功能接口也称为SAM接口,因为它仅包含一种抽象方法。
SAM Interface stands for Single Abstract Method Interface. Java SE 8 API has defined many Functional Interfaces.
SAM接口代表单一抽象方法接口。 Java SE 8 API定义了许多功能接口。
Yes, it is possible to define our own Functional Interfaces. We use Java SE 8’s @FunctionalInterface annotation to mark an interface as Functional Interface.
是的,可以定义我们自己的功能接口。 我们使用Java SE 8的@FunctionalInterface批注将接口标记为Functional Interface。
We need to follow these rules to define a Functional Interface:
我们需要遵循以下规则来定义功能接口:
It is not mandatory to define a Functional Interface with @FunctionalInterface annotation. If we don’t want, We can omit this annotation. However, if we use it in Functional Interface definition, Java Compiler forces to use one and only one abstract method inside that interface.
使用@FunctionalInterface注释定义功能接口不是强制性的。 如果我们不想,我们可以省略该注释。 但是,如果我们在“功能接口”定义中使用它,则Java编译器会强制在该接口内部使用一种且仅一种抽象方法。
Why do we need Functional Interfaces? The type of a Java SE 8’s Lambda Expression is a Functional Interface. Whereever we use Lambda Expressions that means we are using Functional Interfaces.
为什么需要功能接口? Java SE 8的Lambda表达式的类型是功能接口。 无论我们在哪里使用Lambda表达式,都意味着我们在使用功能接口。
When our Java project wants to perform the following operations, it’s better to use Java 8 Stream API to get lot of benefits:
当我们的Java项目想要执行以下操作时,最好使用Java 8 Stream API来获得很多好处:
S.No. | Collection API | Stream API |
---|---|---|
1. | It’s available since Java 1.2 | It is introduced in Java SE 8 |
2. | It is used to store Data (A set of Objects). | It is used to compute data (Computation on a set of Objects). |
3. | We can use both Spliterator and Iterator to iterate elements. We can use forEach to performs an action for each element of this stream. | We can’t use Spliterator or Iterator to iterate elements. |
4. | It is used to store unlimited number of elements. | Stream API is used to process on the elements of a Collection. |
5. | Typically, it uses External Iteration concept to iterate Elements such as Iterator. | Stream API uses internal iteration to iterate Elements, using forEach methods. |
6. | Collection Object is constructed Eagerly. | Stream Object is constructed Lazily. |
7. | We add elements to Collection object only after it is computed completely. | We can add elements to Stream Object without any prior computation. That means Stream objects are computed on-demand. |
8. | We can iterate and consume elements from a Collection Object at any number of times. | We can iterate and consume elements from a Stream Object only once. |
序号 | 集合API | 流API |
---|---|---|
1。 | 从Java 1.2开始可用 | 在Java SE 8中引入 |
2。 | 它用于存储数据(一组对象)。 | 它用于计算数据(一组对象的计算)。 |
3。 | 我们可以同时使用Spliterator和Iterator来迭代元素。 我们可以使用forEach为该流的每个元素执行一个动作。 | 我们不能使用Spliterator或Iterator来迭代元素。 |
4。 | 它用于存储无限数量的元素。 | Stream API用于处理Collection的元素。 |
5, | 通常,它使用外部迭代概念来迭代诸如Iterator的元素。 | Stream API使用forEach方法使用内部迭代来迭代Elements。 |
6。 | 收集对象是急切地构造的。 | 流对象是惰性构造的。 |
7 | 仅在完全计算完之后,才向Collection对象添加元素。 | 我们可以将元素添加到流对象,而无需任何事先计算。 这意味着Stream对象是按需计算的。 |
8。 | 我们可以多次迭代和使用Collection对象中的元素。 | 我们只能迭代和使用一次Stream对象中的元素。 |
Spliterator stands for Splitable Iterator. It is newly introduced by Oracle Corporation as part Java SE 8.
Like Iterator and ListIterator, It is also one of the Iterator interface.
Spliterator代表可拆分迭代器。 它是Oracle Corporation作为Java SE 8的一部分新引入的。
像Iterator和ListIterator一样,它也是Iterator接口之一。
S.No. | Spliterator | Iterator |
---|---|---|
1. | It is introduced in Java SE 8. | It is available since Java 1.2. |
2. | Splitable Iterator | Non-Splitable Iterator |
3. | It is used in Stream API. | It is used for Collection API. |
4. | It uses Internal Iteration concept to iterate Streams. | It uses External Iteration concept to iterate Collections. |
5. | We can use Spliterator to iterate Streams in Parallel and Sequential order. | We can use Iterator to iterate Collections only in Sequential order. |
6. | We can get Spliterator by calling spliterator() method on Stream Object. | We can get Iterator by calling iterator() method on Collection Object. |
7. | Important Method: tryAdvance() | Important Methods: next(), hasNext() |
序号 | 分流器 | 迭代器 |
---|---|---|
1。 | 它是在Java SE 8中引入的。 | 从Java 1.2开始可用。 |
2。 | 可拆分迭代器 | 不可拆分迭代器 |
3。 | 它在Stream API中使用。 | 它用于Collection API。 |
4。 | 它使用内部迭代概念来迭代流。 | 它使用外部迭代概念来迭代集合。 |
5, | 我们可以使用Spliterator以并行和顺序顺序迭代流。 | 我们只能使用Iterator来按顺序对Collection进行迭代。 |
6。 | 我们可以通过在Stream Object上调用spliterator()方法来获得Spliterator。 | 我们可以通过在Collection Object上调用iterator()方法来获得Iterator。 |
7 | 重要方法:tryAdvance() | 重要方法:next(),hasNext() |
Optional:
Optional is a final Class introduced as part of Java SE 8. It is defined in java.util package.
可选的:
可选的是作为Java SE 8的一部分引入的最终类。它在java.util包中定义。
It is used to represent optional values that are either exist or not exist. It can contain either one value or zero value. If it contains a value, we can get it. Otherwise, we get nothing.
它用于表示存在或不存在的可选值。 它可以包含一个值或零值。 如果它包含一个值,我们可以得到它。 否则,我们什么也得不到。
It is a bounded collection that is it contains at most one element only. It is an alternative to the “null” value.
它是一个有界集合,它最多仅包含一个元素。 它是“ null”值的替代方法。
Main Advantage of Optional is:
Optional的主要优点是:
Type Inference means determining the Type by compiler at compile-time.
类型推断是指在编译时由编译器确定类型。
It is not a new feature in Java SE 8. It is available in Java 7 and before Java 7 too.
它不是Java SE 8中的新功能。它在Java 7和Java 7之前可用。
Before Java 7:-
Let us explore Java arrays. Define a String of Array with values as shown below:
在Java 7之前:
让我们探索Java数组。 定义一个数组字符串,其值如下所示:
String str[] = { "Java 7", "Java 8", "Java 9" };
Here we have assigned some String values at the right side, but not defined its type. Java Compiler automatically infers its type and creates a String of Array.
在这里,我们在右侧分配了一些String值,但未定义其类型。 Java编译器会自动推断其类型并创建一个Array字符串。
Java 7:
Oracle Corporation has introduced “Diamond Operator” new feature in Java SE 7 to avoid unnecessary Type definition in Generics.
Java 7:
Oracle Corporation在Java SE 7中引入了“ Diamond Operator”新功能,以避免在泛型中不必要的Type定义。
Map> customerInfoByCity = new HashMap<>();
Here we have not defined Type information at the right side, simply defined Java SE 7’s Diamond Operator.
这里我们没有在右侧定义类型信息,只是定义了Java SE 7的Diamond运算符。
Java SE 8:
Oracle Corporation has enhanced this Type Inference concept a lot in Java SE 8. We use this concept to define Lambda Expressions, Functions, Method References etc.
Java SE 8:
Oracle Corporation在Java SE 8中大大增强了该类型推断概念。我们使用此概念来定义Lambda表达式,函数,方法引用等。
ToIntBiFunction add = (a,b) -> a + b;
Here Java Compiler observes the type definition available at the left-side and determines the type of Lambda Expression parameters a and b as Integers.
Java编译器在这里观察左侧可用的类型定义,并将Lambda Expression参数a和b的类型确定为Integers。
That’s all about Java 8 Interview Questions.
这就是关于Java 8面试问题的全部内容。
I have discussed some Java SE 8 Interview Questions in this post and will discuss some more Java SE 8 Interview Questions in my coming posts.
我在这篇文章中讨论了一些Java SE 8面试问题,在以后的文章中还将讨论其他Java SE 8面试问题。
Please drop me a comment if you like my post or have any issues/suggestions.
如果您喜欢我的帖子或有任何问题/建议,请给我评论。
翻译自: https://www.journaldev.com/8697/javase8-interview-questions-part1
java se面试题