GVKun编程网logo

避免在Stream中出现NoSuchElementException(stream流中如何避免空指针)

13

如果您对避免在Stream中出现NoSuchElementException感兴趣,那么本文将是一篇不错的选择,我们将为您详在本文中,您将会了解到关于避免在Stream中出现NoSuchElement

如果您对避免在Stream中出现NoSuchElementException感兴趣,那么本文将是一篇不错的选择,我们将为您详在本文中,您将会了解到关于避免在Stream中出现NoSuchElementException的详细内容,我们还将为您解答stream流中如何避免空指针的相关问题,并且为您提供关于collections.max()异常,NoSuchElementException、java – 使用Stream避免NoSuchElementException、java – 在List中查找匹配时的NoSuchElementException、java – 在Selenium中避免NoSuchElementException的最好方法是什么?的有价值信息。

本文目录一览:

避免在Stream中出现NoSuchElementException(stream流中如何避免空指针)

避免在Stream中出现NoSuchElementException(stream流中如何避免空指针)

我有以下内容Stream

Stream<T> stream = stream();T result = stream.filter(t -> {    double x = getX(t);    double y = getY(t);    return (x == tx && y == ty);}).findFirst().get();return result;

但是,并非总是有一个结果会给我以下错误:

NoSuchElementException:没有值

那么,null如果没有值,我该如何返回?

答案1

小编典典

您可以使用Optional.orElse,它比检查要简单得多isPresent

T result = stream.filter(t -> {    double x = getX(t);    double y = getY(t);    return (x == tx && y == ty);}).findFirst().orElse(null);return result;

collections.max()异常,NoSuchElementException

collections.max()异常,NoSuchElementException

大佬们这是什么原因啊,,求解

 

 

java – 使用Stream避免NoSuchElementException

java – 使用Stream避免NoSuchElementException

我有以下流:
Stream<T> stream = stream();

T result = stream.filter(t -> {
    double x = getX(t);
    double y = getY(t);
    return (x == tx && y == ty);
}).findFirst().get();

return result;

但是,并不总是有一个结果给我以下错误:

NoSuchElementException: No value present

那么如果没有值存在,怎么可以返回一个null?

解决方法

您可以使用 Optional.orElse,它比检查isPresent简单得多:
T result = stream.filter(t -> {
    double x = getX(t);
    double y = getY(t);
    return (x == tx && y == ty);
}).findFirst().orElse(null);

return result;

java – 在List中查找匹配时的NoSuchElementException

java – 在List中查找匹配时的NoSuchElementException

以下老式,简单的代码工作正常(遍历字符串列表,并返回true,因为应该匹配):

public boolean isMatched() {
  List<String> stringList = Arrays.asList("str1","str2","str3");
  String matchString = "str1";

  for (String str : stringList) {
    if (StringUtils.equals(str,matchString)) {
        return true;
    }
  }

  return false;
}

现在,我想做同样的事情,但改用Java 8.到目前为止我有这个:

public boolean isMatched() {
  List<String> stringList = Arrays.asList("str1","str3");
  String matchString = "str1";
  return (stringList.stream().filter(str -> StringUtils.equals(matchString,str)).findFirst().get() != null);
}

使用第二种方法,我收到以下错误:

java.util.NoSuchElementException: No value present [junit] at java.util.Optional.get(Optional.java:135)

为什么我会收到此错误,我应该怎么做才能解决?

解决方法

Optional.get()仅在您知道其中存在实际值时使用.如果没有价值,它会抛出NSEE,正如您所发现的那样.

看起来你想要.findFirst().isPresent(),或者如下所述,anyMatch(谓词).

java – 在Selenium中避免NoSuchElementException的最好方法是什么?

java – 在Selenium中避免NoSuchElementException的最好方法是什么?

我在Selenium WebDriver中使用 Java编写了几个测试用例,并在网格(集线器和多个节点)上执行它们.我注意到,由于NoSuchElementException,几个测试用例失败.什么是避免NoSuchElementException和确保元素总是找到最好和最强大的方法?

解决方法

你永远不能确定该元素将被发现,实际上这是功能测试的目的 – 告诉你页面上是否有任何改变.但有一点肯定有帮助的是添加等待通常导致NoSuchElementException的元素
webdriverwait wait = new webdriverwait(webDriver,timeoutInSeconds);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id<locator>));

今天的关于避免在Stream中出现NoSuchElementExceptionstream流中如何避免空指针的分享已经结束,谢谢您的关注,如果想了解更多关于collections.max()异常,NoSuchElementException、java – 使用Stream避免NoSuchElementException、java – 在List中查找匹配时的NoSuchElementException、java – 在Selenium中避免NoSuchElementException的最好方法是什么?的相关知识,请在本站进行查询。

本文标签: