Treemap FirstKey()导入错误

pok*_*guy 1 java treemap

我试图为我的treemap(内存)使用firstKey()方法.我的代码看起来像:

import java.util.*;

//Code in the middle.

System.out.println(memory.firstKey());
Run Code Online (Sandbox Code Playgroud)

但它给了我这个错误:

GameLogic.java:276: cannot find symbol
symbol  : method firstKey()
location: interface java.util.Map<java.lang.Integer,java.lang.Character>
System.out.println(memory.firstKey());
                                     ^
Run Code Online (Sandbox Code Playgroud)

所有建议都赞赏.如果我也使用lastKey(),也会发生同样的错误.

san*_*hat 8

我想你已经宣布了

Map map = new TreeMap();
Run Code Online (Sandbox Code Playgroud)

你需要改为做

TreeMap map = new TreeMap(); //OR SortedMap map = new TreeMap() 
map.firstKey();
Run Code Online (Sandbox Code Playgroud)

因为firstKey()是一种存在TreeMapMap合同(或界面)中未定义的方法