为了账号安全,请及时绑定邮箱和手机立即绑定

无法将值放入 HashMap<String,双精度>

无法将值放入 HashMap<String,双精度>

蝴蝶刀刀 2022-08-17 16:02:33
任何想法为什么我无法进入这个HashMap?putpublic class UserImpl implements User{   HashMap<String, Double> videoRecords = new HashMap<>();   @Override   public void updateVideoRecord(String currentVideo, double seconds) {       videoRecords.put(currentVideo, seconds);   }}IntelliJ的调试器显示两者并传递值,但HashMap不会更新。以下是我用来检测HashMap不接受这些值的内容:currentVideosecondsvideoRecords@Overridepublic void updateVideoRecord(String currentVideo, double seconds) {      System.out.println(this.videoRecords);    this.videoRecords.put(currentVideo, seconds);    System.out.println(this.videoRecords);}有趣的是,如果我在此方法中初始化HashMap,则值将成功放入其中。
查看完整描述

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}


查看完整回答
反对 回复 2022-08-17
  • 1 回答
  • 0 关注
  • 75 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信