Java Stack 类中的 empty() 与 isEmpty()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24986945/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 16:13:49  来源:igfitidea点击:

empty() vs isEmpty() in Java Stack class

java

提问by Aafreen Sheikh

Why does Stackin Java have an empty()method along with the usual isEmpty()? All abstract classes that Stackextends have an isEmpty()method.

为什么Stack在 Java 中有一个empty()方法和通常的一样isEmpty()?所有Stack扩展的抽象类都有一个isEmpty()方法。

采纳答案by Adrian Shum

I believe OP's question is more on : why there are duplicated methods, given empty()and isEmpty()are doing the same thing?

我相信 OP 的问题更多是关于:为什么有重复的方法,给出empty()并且isEmpty()正在做同样的事情?

If you take a closer look, in Vector, Stackand HashTable, there are more examples of methods doing similar thing with different names.

如果你仔细观察,在VectorStack和 中HashTable,有更多的方法使用不同的名称来做类似的事情。

Here is the brief history:

以下是简史:

At the time of JDK 1.0, there was no "Collection" framework in Java. Stack, Vector, HashTablewere some of the basic data structures provided by Java.

在 JDK 1.0 的时候,Java 中还没有“Collection”框架。 Stack, Vector,HashTable是 Java 提供的一些基本数据结构。

Later in JDK 1.2, Collectionframework was added to JDK, and standard interfaces (like List, Map) were introduced.

后来在 JDK 1.2 中,将Collection框架添加到 JDK 中,并引入了标准接口(如ListMap)。

However in these new standard collection interfaces, methods were named in a different convention. The change in naming convention was most probably influenced by Java Bean standard introduced also in JDK 1.2. These method names were different from those in old Stack, Vectorand HashTableclasses. For example, it was named empty()in original class but was named isEmpty()of Collectioninterface.

然而,在这些新的标准集合接口中,方法以不同的约定命名。命名约定的变化很可能受到 JDK 1.2 中引入的 Java Bean 标准的影响。这些方法的名称是来自那些旧的不同StackVector以及HashTable类。例如,它empty()在原始类中命名isEmpty(),但以Collection接口命名。

In order to make Stack, Vectorand HashTablecompatible with Collection framework, Stack, Vectorand HashTablehas implemented its corresponding Collection interfaces. On the same time, old methods were kept for the sake of backward compatibility.

为了使StackVectorHashTable与收集框架兼容StackVectorHashTable已实施了相应的集合接口。同时,为了向后兼容,保留了旧方法。

Hence the "duplicated" methods you see now.

因此,您现在看到的“重复”方法。

回答by Manoj Mohanty

both return boolean only difference is isEmpty is synchronized points to vector whereas empty is not synchronized points to stack

两者都返回布尔值,唯一的区别是 isEmpty 是同步指向向量,而空不是同步指向堆栈