本文介绍了Java“类型参数X不在其范围之内;应该实现Comparable X.(LocalDateTime作为地图中的键)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经实现了自己的地图,称为AVLTreeMap.这是课程签名.

I have implemented my own Map called AVLTreeMap. Here is the class signature.

public class AVLTreeMap<K extends Comparable<K>, V>

如您所见,我在此地图中使用的是可比较的键.问题是我想使用 LocalDateTime 类型的键实例化地图.

As you can see, I am using comparable keys in this map. The problem is that I want to instantiate a map with a LocalDateTime type key.

private AVLTreeMap<LocalDateTime, CallRecord> callRecords;

我收到错误 Type参数'java.time.LocalDateTime'不在其范围内;应该实现"java.lang.Comparable< java.time.LocalDateTime>" .

据我所知,LocalDateTime实现了 Comparable< ChronoLocalDateTime<?>> ,在这一点上,我对于如何使用创建地图非常困惑LocalDateTime 键集.

As far as I can tell, LocalDateTime implements Comparable<ChronoLocalDateTime<?>> and at this point, I am pretty confused as to how I can create my map with a LocalDateTime key set.

有什么想法吗?

推荐答案

您对可比较的通用签名有点差,

Your generic signature for a Comparable is a little off, this

public class AVLTreeMap<K extends Comparable<K>, V>

应该是

public class AVLTreeMap<K extends Comparable<? super K>, V>

这篇关于Java“类型参数X不在其范围之内;应该实现Comparable X.(LocalDateTime作为地图中的键)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 04:29