[英]Is it possible to use the provided Java Collections methods, like max, min, sort, ect…, on a Stack?
I was working on a problem involving Stacks on HackerRank ( See Here ). 我正在处理一个涉及HackerRank上的堆栈的问题( 请参阅此处 )。 One of the parts of the question asked to provide the max value within the Stack.
问题的其中一部分要求在堆栈中提供最大值。 I thought an easy way to do this was to just write an extended Stack class with a max() method (see below).
我认为一种简单的方法是使用max()方法编写扩展的Stack类(请参见下文)。 That worked but I thought an even easier way might be to just take advantage of Java's Collections methods.
那行得通,但我认为一个更简单的方法可能是仅利用Java的Collections方法。 So I built the craftyMax() method (also seen below).
因此,我构建了craftyMax()方法(也见下文)。
class MyStack<T> extends Stack<T> {
public T craftyMax() {
return Collections.max(this);
}
public T max() {
Integer max = Integer.MIN_VALUE;
for (T item: this) {
max = Math.max((Integer)item, max);
}
return (T) max;
}
}
Of course this did not work as the compiler replied with: 当然这不起作用,因为编译器回答:
Solution.java:6: error: no suitable method found for max(MyStack<T#1>)
return Collections.max(this);
^
method Collections.<T#2>max(Collection<? extends T#2>) is not applicable
(inferred type does not conform to upper bound(s)
inferred: T#1
upper bound(s): Comparable<? super T#1>,Object)
method Collections.<T#3>max(Collection<? extends T#3>,Comparator<? super T#3>) is not applicable
(cannot infer type-variable(s) T#3
(actual and formal argument lists differ in length))
where T#1,T#2,T#3 are type-variables:
T#1 extends Object declared in class MyStack
T#2 extends Object,Comparable<? super T#2> declared in method <T#2>max(Collection<? extends T#2>)
T#3 extends Object declared in method <T#3>max(Collection<? extends T#3>,Comparator<? super T#3>)
Note: Solution.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
1 error
Since, I have tried a few different things and been looking around but I can't seem to find if what I am trying to do here is possible or not. 从那以后,我尝试了几种不同的方法并四处张望,但似乎找不到我在这里尝试做的事情是否可行。 So my question is:
所以我的问题是:
Is it possible to use the provided Java Collections methods, like max, min, sort, ect..., on / within a Stack? 是否可以在Stack上/之内使用提供的Java Collections方法,例如max,min,sort,ect ...? Or am I expecting a little too much?
还是我期望过高?
public static <T extends Object & Comparable<? super T>> T max(Collection<? extends T> coll)
only works for Collection
s whose element type implements the Comparable
interface. 仅适用于其元素类型实现
Comparable
接口的Collection
。
Therefore, your code will work with the proper type bound: 因此,您的代码将与正确的类型绑定一起使用:
class MyStack<T extends Comparable<T>> extends Stack<T> {
public T craftyMax() {
return Collections.max(this);
}
}
I'm not sure about your second method ( max()
), though. 不过,我不确定您的第二个方法(
max()
)。 You are casting T
to Integer
. 您正在将
T
强制转换为Integer
。 If you are certain T
is an Integer
, why not define MyStack
as class MyStack extends Stack<Integer>
? 如果您确定
T
是一个Integer
,为什么不将MyStack
定义为class MyStack extends Stack<Integer>
呢?
You can only call Collections.max
with a single argument on types that implement the Comparable
interface. 您只能在实现
Comparable
接口的类型上使用单个参数调用Collections.max
。 Change your type declaration to 将类型声明更改为
class MyStack<T extends Comparable<T>> extends Stack<T> {
and it should work. 它应该工作。 Or, as an alternative, you can take the two-argument max that takes a
Comparator
as the second argument. 或者,您也可以采用将
Comparator
作为第二个参数的两个参数的最大值。
you can override the comparator to match your functionality like below. 您可以覆盖比较器以匹配您的功能,如下所示。
if (!MyStack.isEmpty()) {
Integer max = Collections.min(MyStack, new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
return o2.compareTo(o1);
}
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.