Java 将新对象存储为hashmap的值?

Java 将新对象存储为hashmap的值?,java,hashmap,Java,Hashmap,我试图找到一种方法,将一个类的新实例作为值存储在Java hashmap中。这个想法是由一位Java讲师给我的,目的是创建一个数据存储结构,可以用于我正在开发的程序 他向我推荐的想法是使用一个hashmap,该hashmap将计算机的名称存储为键,值将是InfoStor.class类的一个新实例。InfoStor包含getName()、setName()、getMemory()等方法 我已经为一个基本测试设置了类和方法,看看它是否有效。我遇到的问题是,一旦在hashmap中创建了一个新条目,我就

我试图找到一种方法,将一个类的新实例作为值存储在Java hashmap中。这个想法是由一位Java讲师给我的,目的是创建一个数据存储结构,可以用于我正在开发的程序

他向我推荐的想法是使用一个hashmap,该hashmap将计算机的名称存储为键,值将是InfoStor.class类的一个新实例。InfoStor包含getName()、setName()、getMemory()等方法

我已经为一个基本测试设置了类和方法,看看它是否有效。我遇到的问题是,一旦在hashmap中创建了一个新条目,我就无法理解如何使用InfoStor中的方法

这是我到目前为止的代码

VMware.class

import java.util.HashMap;

public class VMware {

    public static void main(String[] args) {                       
        HashMap <String, Object> mapper = new HashMap();            
        mapper.put("NS01", new InfoStor("NS01"));            
        //mapper.get("NS01").            
    }            
}
public class InfoStor {

    private String vmName;
    private String platform;
    private Integer memory;

    public InfoStor (String name) {
        vmName = name;
    }

    String getName(){
        return vmName;
    }

    void setPlatform(String p){
        platform = p;
    }

    String getPlatform(){
        return platform;
    }

    void setMemory(Integer m){
        memory = m;
    }

    Integer getMemory(){
        return memory;
    }
}
我试图完成的是这样的事情(基本想法)


我走错方向了吗?非常感谢您的帮助。

利用java中添加的泛型。它们有助于编译时类型检查,并且不需要强制转换

  HashMap <String, Object> mapper = new HashMap();
  //you will be able to retrieve an object and then cast it to your InfoStore
  InforStore isN01 = (InfoStore)mapper.get("N01");

  //this will unfortunately be accepted, even thought it's a bug
  mapper.put("N02", new Integer(0));

  ________________________

  HashMap <String, InfoStore> mapper = new HashMap();
  //you will be able to retrieve an object and then cast it to your InfoStore
  InforStore isN01 = mapper.get("N01"); //no cast
HashMap映射器=新的HashMap();
//您将能够检索对象,然后将其强制转换到InfoStore
InforStore isN01=(InfoStore)mapper.get(“N01”);
//这将不幸地被接受,即使它是一个bug
mapper.put(“N02”,新整数(0));
________________________
HashMap映射器=新建HashMap();
//您将能够检索对象,然后将其强制转换到InfoStore
InforStore isN01=mapper.get(“N01”)//无演员阵容

如果您这样声明hashmap:

HashMap<String, InfoStor> mapper = new HashMap<String, InfoStor>();
否则,如果您坚持使用原始代码中使用的
HashMap
,则需要在调用方法之前强制转换它:

Object obj = mapper.get("somekey");
((InfoStor)obj).getMemory(); // cast is required
obj.getMemory(); // this will not compile

您应该仔细阅读Java泛型。

问题在于您的代码只指定映射中的值是
对象。您知道的不止这些,因此请告诉编译器以下信息:

HashMap<String, InfoStor> mapper = new HashMap<String, InfoStor>();
mapper.put("NS01", new InfoStor("NS01"));
...

InfoStor value = mapper.get("NS01");
Integer memory = value.getMemory();
HashMap映射器=新的HashMap();
mapper.put(“NS01”,新InfoStor(“NS01”);
...
InfoStor value=mapper.get(“NS01”);
整数内存=value.getMemory();
请注意,通常情况下,使用变量类型的接口并不总是更好,您可以在构造函数调用中使用菱形运算符,让编译器使用类型推断来填充类型参数:

Map<String, InfoStor> mapper = new HashMap<>();
mapper.put("NS01", new InfoStor("NS01"));
...

InfoStor value = mapper.get("NS01");
Integer memory = value.getMemory();
Map-mapper=newhashmap();
mapper.put(“NS01”,新InfoStor(“NS01”);
...
InfoStor value=mapper.get(“NS01”);
整数内存=value.getMemory();

你走对了方向

将地图初始化为:

HashMap <String, InfoStor> mapper = new HashMap<String, InfoStor>();

你可以用数组来烹饪一些东西…例如,如果你可以在数组中存储对象,然后用这个想法在散列图中实现它…我不知道你是如何设计的,但我曾经陷入其中,像这样度过了难关

例如

公主班{

int age;

public princess(int age){
    this.age=age;
}
public int getAge(){
    return this.age;
}
}

公共类hashmaptest{

public static void main(String[] args) {
  princess[] p=new princess[10];
  HashMap scores = new HashMap();
  scores.put("a",new princess(6));
  scores.put("b",new princess(7));

  p[0]=(princess)scores.get("a");
   System.out.println(p[0].getAge());
  p[0]=null;
   p[0]=(princess)scores.get("b");

  System.out.println(p[0].getAge());



}

}

您应该使用
HashMap
而不仅仅是
HashMap
,这样您就不必在调用
getMemory()
之前返回到
InfoStor
。如果内存不能为空,我会使用int,并在构建对象时强制设置它(平台也是如此),我还会检查构造函数中的内容。你知道你有Jon Skeet的答案吗?:-)完美的这就是我做错的地方。就像我想要的那样。谢谢。几年后,也许会有帮助。通常,您针对一个接口进行实例化。在您的示例中,它将是Map=newhashmap()。这样代码就更灵活了。Check@karlihnos:我很清楚这一点,但我关注的是OP不清楚的部分,而不是引入不必要的差异。我会把它作为一个选项添加进去。是的,这是他们在我的java类中没有讨论过的东西,我只对它们进行了一点点的研究。听起来需要对它们进行更多的研究。这个答案还不清楚。强制转换是正确的,但是初始化大小为10的数组以将对象存储到数组的第一个元素中没有意义。公认的答案是解决问题的正确方法。
InfoStor var = mapper.get("NS01");
System.out.println(var.getMemory());
int age;

public princess(int age){
    this.age=age;
}
public int getAge(){
    return this.age;
}
public static void main(String[] args) {
  princess[] p=new princess[10];
  HashMap scores = new HashMap();
  scores.put("a",new princess(6));
  scores.put("b",new princess(7));

  p[0]=(princess)scores.get("a");
   System.out.println(p[0].getAge());
  p[0]=null;
   p[0]=(princess)scores.get("b");

  System.out.println(p[0].getAge());



}