[英]Printing max element in a Deque<Integer>
I have a Deque from Integers, which I'm using as a Que.我有一个来自 Integers 的 Deque,我将其用作 Que。 I have to add and remove elements from it and in some cases I need to print the max value in the Que.我必须从中添加和删除元素,在某些情况下,我需要在 Que 中打印最大值。
The thing that I've tried is:我尝试过的事情是:
System.out.println(deque.stream().max(Integer::compareTo));
But it prints - Optional[the current max int]
.但它会打印 - Optional[the current max int]
。
Thank you!谢谢!
That is because the max
method of java.util.Stream
returns your value wrapped in Optional
.这是因为java.util.Stream
的max
方法返回包裹在Optional
中的值。 This is the signature of the max method Optional<T> max(Comparator<? super T> comparator);
这是 max 方法的签名Optional<T> max(Comparator<? super T> comparator);
The example below would provide you the expected behaviour:下面的示例将为您提供预期的行为:
Optional<Integer> optionalOfMax = deque.stream().max(Integer::compareTo);
System.out.println(optionalOfMax.orElseThrow());
You can do it as follows:你可以这样做:
deque.stream().max(Integer::compareTo).ifPresent(System.out::println);
Note that deque.stream().max(Integer::compareTo)
returns Optional<Integer>
.请注意, deque.stream().max(Integer::compareTo)
返回Optional<Integer>
。
Alternatively,或者,
deque.stream().flatMapToInt(x -> IntStream.of(x)).max().ifPresent(System.out::println);
Stream#flatMapToInt
returns an IntStream
consisting of the results of replacing each element of this stream with the contents of a mapped stream produced by applying the provided mapping function to each element. Stream#flatMapToInt
返回一个IntStream
,其中包含将此 stream 的每个元素替换为通过将提供的映射 ZC1C425268E68385D1AB5074C17A9 应用到每个元素而生成的映射 stream 的内容的结果。
You can just use the code you have written and extend it by .get()
.您可以只使用您编写的代码并通过.get()
对其进行扩展。
The following code以下代码
public static void main(String[] args) {
// create a Deque that holds Integers
Deque<Integer> myDQ = new ArrayDeque<Integer>();
// add some of them
myDQ.add(12);
myDQ.add(13);
myDQ.add(14);
myDQ.add(15);
myDQ.add(16);
myDQ.add(20);
myDQ.add(17);
myDQ.add(18);
myDQ.add(19);
// and print
System.out.println(
myDQ.stream()
.max(Integer::compareTo) // the largest one
.get() // not as Optional but only as Integer
);
}
just prints 20
.只打印20
。
You can unbox the Integer wrappers in your queue and use IntStream.max() , which returns an OptionalInt:您可以在队列中拆箱 Integer 包装器并使用IntStream.max() ,它返回一个 OptionalInt:
deque.stream().mapToInt(Integer::intValue)
.max()
.ifPresent(System.out::println);
This will do nothing if max()
returns OptionalInt.empty()
, which happens when the deque is empty.如果max()
返回OptionalInt.empty()
,这将不起作用,这在双端队列为空时发生。 If you want to check for emptiness, you can do, for example:如果要检查是否为空,可以执行以下操作,例如:
deque.stream().mapToInt(Integer::intValue)
.max()
.ifPresentOrElse(System.out::println,
() -> throw new RuntimeException("Attempt to get max of empty collection"));
The max
-Method returns an java.util.Optional
. max
-方法返回java.util.Optional
。 If you just want to return a int-Value you can use the orElse
-Method from java.util.Optional
to return the given value or, if not present, another default.如果您只想返回一个 int-Value,您可以使用orElse
中的java.util.Optional
-方法返回给定值,或者如果不存在,则返回另一个默认值。
System.out.println(deque.stream().max(Integer::compareTo).orElse(0));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.