Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Storing a new object as the value of a hashmap?

Tags:

java

hashmap

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.

like image 532
ianc1215 Avatar asked Aug 23 '12 20:08

ianc1215


People also ask

Can we store object as value in HashMap?

As to HashMaps, yes you can put any types of objects in them as values. To obtain objects from the Map, you typically use the get() method of the Map, or access the values() of the Map with an Iterator (or better, in a for loop).

Can we store object in HashMap in Java?

The casting is correct but there is no point in initializing an array of size 10 in order to store the object into the first element of the array. The accepted answer is the correct way to solve the problem.

How do you store a custom object in a HashMap key?

To put it simply, we have to ensure that the hashCode() method returns: the same value for the object as long as the state doesn't change (Internal Consistency) the same value for objects that are equal (Equals Consistency) as many different values as possible for objects that are not equal.


1 Answers

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();
like image 62
Jon Skeet Avatar answered Oct 13 '22 03:10

Jon Skeet