1 回答
TA贡献1803条经验 获得超3个赞
如果您可以添加您的运行器代码或至少您的方法,那将有所帮助。无论如何,我试图重现您的问题,但它似乎没有任何问题或任何东西。main()
在这里,我像你一样使用了相同的类实现,我只是添加了一个get方法,将映射返回到该方法:UserImplmain
import java.util.*;
import java.util.HashMap;
public class UserImpl implements User {
HashMap<String, Double> videoRecords = new HashMap<>();
@Override
public void updateVideoRecord(String currentVideo, double seconds) {
videoRecords.put(currentVideo, seconds);
}
public HashMap<String, Double> getRecords() {
return videoRecords;
}
}
它从这个“Mock”接口实现,因为在您的实现中,您正在重写方法。updateVideoRecord()
显然,在我创建类的对象中,将一个新条目放入HashMap并在放置之前和之后打印。MainUserImpl
import java.util.*;
public class Main {
public static void main(String[] args) {
UserImpl userImpl = new UserImpl();
HashMap<String, Double> records = userImpl.getRecords();
System.out.println("The size of the map is " + records.size());
System.out.println("Initial Mappings are: " + records);
userImpl.updateVideoRecord("theCurrentVideo", 360);
System.out.println("The size of the map is " + records.size());
System.out.println("Initial Mappings are: " + records);
}
}
最后,在这里您可以看到输出看起来完全符合预期,因此我没有看到您的问题。因此,如果你能更详细地阐述你的问题,也许我可以提供更好的帮助。如果没有,那么我希望这有助于您解决问题。
kareem@Kareems-MBP:Desktop$ javac Main.java
kareem@Kareems-MBP:Desktop$ java Main
The size of the map is 0
Initial Mappings are: {}
The size of the map is 1
Initial Mappings are: {theCurrentVideo=360.0}
添加回答
举报