RV307
RV307

Reputation: 15

How do I use a HashMap from another class or function?

Here is a working code example. Thanks to @markspace for providing me with it. The code here makes a hashmap and inserts values in it.

 public class RpgPersonExample
{
   public static void main( String[] args )
   {
      Map<String, RpgPerson> team = new HashMap<>();
      team.put( "Sgt. Mayhem", new RpgPerson( 100, 10, 0 ) );
      team.put( "Wizard", new RpgPerson( 100, 1, 10 ) );
      team.put( "Dragon", new RpgPerson( 1000, 100, 100 ) );

      System.out.println( team.get( "Dragon" ) );
      System.out.println( team.get( "Wizard" ) );
   }

}

class RpgPerson
{

   private int hits;
   private int strength;
   private int magic;

   public RpgPerson( int hits, int strength, int magic )
   {
      this.hits = hits;
      this.strength = strength;
      this.magic = magic;
   }

   @Override
   public String toString()
   {
      return "RpgPerson{" + "hits=" + hits + ", strength=" +
              strength + ", magic=" + magic + '}';
   }

}

What I want to do though is to use it from another class. If I use

       System.out.println( team.get( "Dragon" ) );

from the main function it works fine. But how should I do i I want to do it from another class?

Public class Test {
     public static void example(){
         System.out.println( RpgPerson.team.get( "Dragon" ) );
     }
}

The class Test does obviously not work but what should I do if I want it to work?

Upvotes: 1

Views: 7656

Answers (4)

codemania23
codemania23

Reputation: 1071

if i got your question right, you want to use your hashmap elsewhere in your program. This is simple and can be achieved by declaring the hashmap as public and making it a class variable. Now, since its public and.a class variable, it can be used in any function of the class, or outside with the help of that class's reference.

But if you want it to be used in the same class's void main, you have to declare it as PUBLIC STATIC because static functions can only access static variables. if you declare it as static, you can use it anywhere outside by just saying CLASSNAME.team.get("whatever"); hope this helped!!

Upvotes: 0

haley
haley

Reputation: 1175

You need a new class. You could make it look something like this:

public class Team {
    private Map<String, RpgPerson> team;

    public Team() {
          team = new HashMap<String, RpgPerson>();
    }

    public void addPersonToTeam(String name, RpgPerson person) {
          team.put(name, person);
    }

    public Map<String, RpgPerson> getTeam() {
          return team;
    }
}

Then you can make a new team and use this in whatever class you need.

Upvotes: 0

Amir Afghani
Amir Afghani

Reputation: 38531

The simplest thing you can do is pass into the example method an argument that references the Map:

 public static void example(Map<String, RpgPerson> team) {
     System.out.println(team.get("Dragon"));
 }

Upvotes: 1

ToasteR
ToasteR

Reputation: 888

Declare the Map named team as a static class variable:

public class RpgPersonExample
{
   public static Map<String, RpgPerson> team = new HashMap<>();
   public static void main( String[] args )
   {
     // ...
   }
}

Access it like this:

 System.out.println( RpgPersonExample.team.get( "Dragon" ) );

Upvotes: 0

Related Questions