user768990
user768990

Reputation: 89

Nesting Maps in Java

I want to store many details (like name, email, country) of the particular person using the same key in hashtable or hashmap in java?

hashMap.put(1, "Programmer");        
hashMap.put(2, "IDM");       
hashMap.put(3,"Admin");
hashMap.put(4,"HR"); 

In the above example, the 1st argument is a key and 2nd argument is a value, how can i add more values to the same key?

Upvotes: 0

Views: 9400

Answers (3)

Jake
Jake

Reputation: 4660

The following class is very generic. You can nest ad infinitum. Obviously you can add additional fields and change the types for the HashMap. Also note that the tabbing in the toString method should be smarter. The print out is flat.

import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;


public  class HierarchicalMap
{

    private String key;
    private String descriptor;
    private Map<String,HierarchicalMap>values=new HashMap<String,HierarchicalMap>();

    public String getKey()
    {
            return key;
    }
    public void setKey(String key)
    {
            this.key = key;
    }
    public void addToSubMap(String key, HierarchicalMap subMap)
    {
            values.put(key, subMap);
    }
    public String getDescriptor()
    {
            return descriptor;
    }
    public void setDescriptor(String descriptor)
    {
            this.descriptor = descriptor;
    }

    public HierarchicalMap getFromSubMap(String key)
    {
            return values.get(key);
    }

    public Map<String,HierarchicalMap> getUnmodifiableSubMap()
    {
            return Collections.unmodifiableMap(values);
    }


    public String toString()
    {
            StringBuffer sb = new StringBuffer();
            sb.append("HierarchicalMap: ");
            sb.append(key);
            sb.append(" | ");
            sb.append(descriptor);
            Iterator<String> itr=values.keySet().iterator();
            while(itr.hasNext())
            {
                     String key= itr.next();
                     HierarchicalMap subMap=this.getFromSubMap(key);
                     sb.append("\n\t");
                     sb.append(subMap.toString());

            }
            return sb.toString();

    }

Upvotes: 0

Atanu Roy
Atanu Roy

Reputation: 1

I think what you are asking let us assume you we want to store String page, int service in the key and an integer in the value. Create a class PageService with the required variables and define your HashMap as Hashmap hmap = .....

Inside pageService, what you need to do is override the equals() and hashcode() methods. Since when hashmap is comparing it checks for hashcode and equals. Generating hashcode and equals is very easy in IDEs. For example in eclipse go to Source -> generate hashcode() and equals()

public class PageService {

 private String page;
 private int service;
 public PageService(String page, int service) {
    super();
    this.page = page;
    this.service = service;
 }
 public String getPage() {
    return page;
 }
 public void setPage(String page) {
    this.page = page;
 }
 public int getService() {
    return service;
 }
 public void setService(int service) {
    this.service = service;
 }


 @Override
 public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result + ((page == null) ? 0 : page.hashCode());
    result = prime * result + service;
    return result;
 }

 @Override
 public boolean equals(Object obj) {
    if (this == obj)
        return true;
    if (obj == null)
        return false;
    if (getClass() != obj.getClass())
        return false;
    PageService other = (PageService) obj;
    if (page == null) {
        if (other.getPage() != null)
            return false;
    } else if (!page.equals(other.getPage()))
        return false;
    if (service != other.getService())
        return false;
    return true;
 }
}

Upvotes: 0

David Mason
David Mason

Reputation: 2957

You can achieve what you're talking about using a map in each location of your map, but it's a little messy.

Map<String, Map> people = new HashMap<String, Map>();
HashMap<String, String> person1 = new HashMap<String, String>();
person1.put("name", "Jones");
person1.put("email", "[email protected]");
//etc.
people.put("key", person1);

//...

people.get("key").get("name");

It sounds like what you might really want, though, is to define a Person class that has multiple properties:

class Person
{
    private String name;
    private String email;

    public String getName()
    {
        return name;
    }

    public void setName(String name)
    {
        this.name = name;
    }

    //plus getters and setters for other properties
}


Map<String, Person> people = new HashMap<String, Person>();
person1 = new Person();
person1.setName("Jones");
people.put("key", person1);

//...

people.get("key").getName();

That's the best I can do without any information about why you're trying to store values in this way. Add more detail to your question if this is barking up the wrong tree.

Upvotes: 2

Related Questions