Java错误 无法解析 ‘List’ 中的 ‘map’ 方法。

huangapple go评论95阅读模式
英文:

Java error Cannot resolve method 'map' in 'List'

问题

以下是您要翻译的内容:

我正在尝试为CRUD应用程序编写Java后端。我正在使用Java 1.8。我使用了如下的findByName(name)实现 [如下所示]。Intellij指出存在错误,因为“无法解析'List'中的方法'map'”。这是否意味着Java没有map方法?我该如何解决这个问题?我之前尝试在某些地方添加类似于stream的内容,但没有帮助。我还希望在findByName中返回设备,并且使用一些尝试的解决方案,错误从map方法移动到了返回设备的部分。如果能得到帮助,我将不胜感激,因为这让我彻夜难眠,无法解决问题。

return deviceRepository.findByName(name).map(device -> {
    LOG.info("从数据库读取名称为 " + name + " 的设备。");
    return device;
}).orElseThrow(() -> new DeviceNotFoundException("无法在数据库中找到名称为 " + name + " 的设备。"));
}
英文:

I'm trying to get working backend in Java for CRUD app. I am using Java 1.8. I used the following implementation of findByName(name) [below]. Intellij says that there is error, because "Cannot resolve method 'map' in 'List'". Does it means that Java does have map method? How can I overcome this. I have been trying to add before something like stream etc. but it does not help. I also would like to have device returned in findByName and with some tried solution the error was moving from map method to return device. I would be grateful for help, it is keeping me awake long in the night because I cannot finish.

return deviceRepository.findByName(name).map(device -> {
        LOG.info("Reading device with name " + name + " from database.");
        return device;
    }).orElseThrow(() -> new DeviceNotFoundException("The device with the name " + name + " couldn't be found in the database."));
}

答案1

得分: 2

> 这是否意味着 Java 有 map 方法?

不是的。这意味着 List 接口没有 map 方法。你可以查阅javadocs来自行确认这一点。

> 我该如何克服这个问题?

所以看起来你的代码意图是在列表中进行搜索,然后要么写入日志消息并返回一个元素,要么抛出异常。

但是这里有一个相当根本性的问题。findByName(name) 方法显然可以返回多个“设备”。因此你的代码必须决定在这种情况下该做什么。

以下是一个可能的解决方案:

List<Device> found = deviceRepository.findByName(name);
if (found.size() == 0) {
    throw new DeviceNotFoundException("在数据库中找不到名为 " + name + " 的设备。");
} else if (found.size() > 1) {
    throw new DeviceNotFoundException("在数据库中找到多个名为 " + name + " 的设备。");
} else {
    LOG.info("从数据库中读取名为 " + name + " 的设备。");
    return found.get(0);
}

其他可能的选择包括忽略多个设备具有相同名称的情况(只返回第一个),或者更改 findByName 的 API 以仅返回一个设备。

英文:

> Does it means that Java does have map method?

No. It means that the List interface does not a map method. You can check the javadocs for yourself to confirm this.

> How can I overcome this.

So it looks like your code is intended to search the list, then either write a log message and return an element, or throw an exception.

There is a pretty fundamental problem with this. The findByName(name) method can clearly return more than one "device". So your code has to decide what to do in that case.

Here is one possible solution:

List<Device> found = deviceRepository.findByName(name);
if (found.size() == 0) {
    throw new DeviceNotFoundException("The device with the name " + name + 
                                      " couldn't be found in the database.");
} else if (found.size() > 1) {
    throw new DeviceNotFoundException("Multiple devices with name " + name + 
                                      " found in the database.");
} else {
    LOG.info("Reading device with name " + name + " from database.");
    return found.get(0);
}

Other possibilities would be to ignore the case where multiple devices have the same name (and just return the first), or change the findByName API to return only one device.

答案2

得分: 1

Java的列表没有map方法,从未有过。而且一个列表拥有一个名为 orElseThrow 的方法是非常奇怪的,那可能是想要的是 ifEmptyThrow 吗,或许是这样吗?那也不存在,但至少有一些道理。或者 findByName 是一个可能会找到结果也可能不会找到结果的操作,map 操作如果没有结果的话应该是一个空操作,orElseThrow 则在没有找到结果时被调用?

deviceRepositoryfindByName 都是不被识别的,这并没有帮助。

如果 findByName 返回一个 List<Device>,Java 并不会简化尝试流式处理以进行“如果为空”的操作。

如果 findByName 应该返回1个或0个元素,如果它返回一个 Optional,你粘贴的代码将会起作用。这使我认为它并不是这样的情况。

那么剩下的就是:不要把你的鞋后跟当作通用工具。虽然这是双好鞋子,适合走路,但不适合钉子。这是如何做的。简单明了。

List<Device> devices = deviceRepository.findByName(name);
if (devices.isEmpty()) throw new DeviceNotFoundException("The device with name " + name + " couldn't be found in the database");
LOG.info("Reading device with name {} from the database", name);
英文:

Java's lists do not have a map method and never did. It's also extremely odd for a list to have a method named orElseThrow - is that supposed to be ifEmptyThrow, perhaps? That doesn't exist either, but at least that makes sense. Or is findByName an operation that may or may not find a result, and map is supposed to be a no-op if there is no result, and orElseThrow is to be invoked if no result is found?

'deviceRepository', nor findByName, is recognizable, which is not helping.

If findByName returns a List&lt;Device&gt;, java doesn't make it simple to try to stream your way into 'if empty'.

If findByName is supposed to return 1 or 0 elements, if it returns an Optional, your code as pasted will work. Which makes me assume it doesn't.

That leaves: Stop treating the back of your shoe as a universal tool. It's a nice shoe and all, and great for walking, but not for nails. This is how to do it. Plain. Simple.

List&lt;Device&gt; devices = deviceRepository.findByName(name);
if (devices.isEmpty()) throw new DeviceNotFoundException(&quot;The device with name + &quot; name + &quot; couldn&#39;t be found in the database&quot;);
LOG.info(&quot;Reading device with name {} from the database&quot;, name&quot;);

huangapple
  • 本文由 发表于 2020年9月27日 11:02:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/64084375.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定