将一个新对象存储为散列表的值? [英] Storing a new object as the value of a hashmap?

查看:94
本文介绍了将一个新对象存储为散列表的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



他推荐的想法对我来说,就是使用一个存储计算机名称的哈希映射作为关键字,并且该值将是InfoStor.class类的新实例。 InfoStor包含诸如getName(),setName(),getMemory()...等方法。

我有类和方法几乎设置为基本测试看如果它能起作用或不起作用。我遇到的问题是,我无法弄清楚如何在使用hashmap创建新条目时使用InfoStor中的方法。



这是代码I到目前为止;

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)。
}
}

InfoStor.class

  public class InfoStor {

private String vmName;
私人字符串平台;
私有整数内存;

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;
}

整数getMemory(){
返回内存;


$ / code $ / pre

我想要完成的是这样的基本概念)。

  Object var = mapper.get(NS01); 
System.out.println(var.getMemory());

我会以错误的方式回答这个问题吗?任何帮助表示赞赏谢谢。

解决方案

问题是您的代码只指定地图中的值是对象。你知道的不仅仅是这些,所以告诉编译器:

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

InfoStor value = mapper.get(NS01);
整数内存= value.getMemory();

请注意,通常 类型 - 你可以使用菱形运算符来进行构造函数调用,让编译器使用类型推断来填充类型参数:

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

InfoStor value = mapper.get(NS01);
整数内存= value.getMemory();


I am trying to find a way to store a new instance of a class as the value in a Java hashmap. The idea was given to me by a Java instructor in order to create a data storage structure that could be used to for a program I am working on.

The idea he recommended to me was to use a hashmap that stored the name of a computer as the key and the value would be a new instance of the class InfoStor.class. InfoStor contains methods such as getName(), setName(), getMemory()...

I have the class and the method pretty much setup for a basic test to see if it would work or not. The problem I am running into is I cannot figure out how to use the methods inside of InfoStor once I have created a new entry in the hashmap.

This is the code I have so far;

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").            
    }            
}

InfoStor.class

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;
    }
}

What I am trying to accomplish is something like this (basic idea).

Object var = mapper.get("NS01");    
System.out.println(var.getMemory());

Am I going about this the wrong way? Any help is appreciated thanks.

解决方案

The problem is that your code only specifies that the values in the map are Object. You know more than that, so tell the compiler that information:

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

InfoStor value = mapper.get("NS01");
Integer memory = value.getMemory();

Note that it's generally though not always better to use interfaces for the variable types - and you can use the diamond operator for the constructor call, letting the compiler use type inference to fill in the type arguments:

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

InfoStor value = mapper.get("NS01");
Integer memory = value.getMemory();

这篇关于将一个新对象存储为散列表的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆