如何从另一个类获取存储在哈希图中的数据

时间:2021-03-03 15:01:50

标签: java hashmap

我对 Java 开发还很陌生并且仍在学习,现在我一直在从另一个类获取 HashMap 的数据。

基本上我有一个包含我的哈希图的类,这个类只有哈希图和列表来存储我的数据,然后另一个类将获取这个哈希图并将一些数据存储到它,之后另一个类需要访问哈希图以获取一些存储在其中的值。

目前我只知道三种方式来访问那些HashMap

  1. 我想通过使用 extend 关键字将 hashmap 类扩展到我的其他类?还没试过
  2. 通过使用我之前使用的 static 关键字,基本上滥用了 static 关键字,我现在也在努力避免这种情况。
  3. 最后是通过获取该类的实例?这就是我现在正在使用的并且遇到问题

为了解释这个问题,我有这个数据类,它将包含我所有的哈希图

public class getLocation {
   HashMap<String, Location> location = new HashMap<>();

   public HashMap<String, Location> getloc(){
        return location;
    }
}

然后这是我的一个会放一些数据的类

public class set1 {
   // As far as I know this will create a new instance, therefore the hashmap will be a new empty one
   private getLocation getLoc = new getLocation();

   // Putting some data
   public void setPosition() {
       getLoc.getloc().put("Position_1", Location);
   }
}

终于到了第三节课

public class setBlock {
   // Again getting a new instance, therefore an empty hashmap again..
   private getLocation getLoc = new getLocation();

   public void getBlock() {
       System.out.println(getLoc.getloc().get("Position_1"));
   }
   
}

我认为这基本上只是返回 null,因为我正在创建一个新实例,因此它始终为空。有没有合适的方法来做到这一点?之前我只是在我拥有的每个 HashMap 上使用 static 关键字,但我了解到这不是一个好习惯,因此我正在努力学习正确的方法。

我在网上阅读的几乎所有内容都只是创建一个哈希图,然后在另一个类上调用它并将其数据设置在那里,就是这样,我需要的是在另一个类上再次访问它。

对不起,如果我在解释事情时变得多余,我只想尽我所能解释它。

如果您对如何正确执行此操作或其他操作有任何建议,请帮助我

1 个答案:

答案 0 :(得分:0)

一般来说,您不想想要公开您的数据存储(在这种情况下是您的哈希图)。相反,您希望其他对象以特定方式与您的 HashMap 交互。

这就是我认为您想要实现的目标:此类的对象将存储位置供玩家提供给定玩家的位置。

// Aside from "Start with Capital letter", the name of a Class should give
// (more than) a hint about its purpose. "getLocation" is a bad, bad, bad name
// because 1) it will not be obvious it's a class and 2) can be confused by humans
// for a method.
public class Storage {

    HashMap<Player, Location> locationStorage;

    // This is the constructor for Storage-objects.
    // It's called with the `new` keyword and creates itself 
    // a new HashMap. 
    // Each Storage-object will have its own HashMap `locationStorage`
    public Storage() {
        this.locationStorage = new HashMap<>();
    }
    
    // This is a so-called Getter-Method, that exposes the HashMap
    // to other classes in a defined way; it will retrieve the Location-Object
    // associated with the `key` in the Storage's `locationStorage`.
    public Location getLocation(Player player) {
        return this.locationStorage.get(player);
    }

    // This is a so-called Setter-Method, that exposes a way to add
    // a value to our HashMap `locationStorage`
    public void setLocation(Player player, Location loc) {
        this.locationStorage.put(player, loc);
    }

}

让我们进一步假设我们有一个 Player 类,如下所示:

public class Player {

    private Location playerLocation;
    // ... other variables
    
    public Location getLocation() {
        return this.playerLocation;
    }

}

然后我们可以有一个包含您的 main() 方法的类:

public class MainGame {

    public static void main(String[] args) {

        // using the keyword `new` we can instantiate an Object of the following class
        Storage storage = new Storage();
        // Let's assume you have a Player class that that holds a Location
        Player player = new Player();

        // Storing the Location object of the Player object in our Storage object
        storage.setLocation(player, player.getLocation());

        // retrieving the Location of the player Player
        Location currentLocation = storage.getLocation(player);
    }
}
相关问题