简体   繁体   English

如何从该 HashMap 中的对象访问对象的 HashMap。 (爪哇)

[英]How to get access to a HashMap of objects from the Objects in that HashMap. (Java)

I have a hash map of some POJO Objects of a Class named User: HashMap<ObjectId, User>我有一个名为 User 的类的一些 POJO 对象的哈希映射: HashMap<ObjectId, User>

These objects (Users) are related to each other.这些对象(用户)相互关联。 (I need to search through other users to Update one's Parameters) (我需要搜索其他用户来更新自己的参数)

How can I have access to the HashMap within a user object?如何访问用户对象中的 HashMap?

import org.bson.types.ObjectId;
import org.bson.BsonDocument;
import java.util.ArrayList;
import java.util.List;

public class User {
    private ObjectId _id;
    private int grade;
    private String region;
    private ArrayList<ObjectId> _reg_by;
    private ObjectId regBy;

    public User(){

    }

    public ObjectId getId() {
        return _id;
    }
    public void setId(final ObjectId id) {
        this._id = id;
    }

    public int getGrade() {
        return grade;
    }
    public void setGrade(final int grade) {
        this.grade = grade;
    }

    public String getRegion() {
        return region;
    }
    public void setRegion(final String region) {
        this.region = region;
    }

    public ObjectId getRegBy() {
        if(regBy == null) {
            regBy = ((_reg_by.size() != 0) ?  _reg_by.get(0)  :  null);
        }
        return regBy;
    }
    public void setRegBy(final ObjectId regBy) {
        this.regBy = regBy;
    }

    public ArrayList<ObjectId> get_reg_by(){
        return _reg_by;
    }
    public void set_reg_by(ArrayList<ObjectId> _reg_by){
        this._reg_by = _reg_by;
    }

    private String updateRegion(){
        if(getRegBy() ==  null)
            return null;
        //TODO search for the person who registered him and use the region!
        // how to get access to others from here?!
    }

}

This is the User class where in regionUpdate() function I want to have this access这是用户类,在 regionUpdate() 函数中,我想拥有此访问权限

I creat this HashMap in my Main function.我在我的 Main 函数中创建了这个 HashMap。

HashMap<ObjectId, User> users = mongoHandler.getUsers();

I solved my problem by defining my HashMap as Static.我通过将我的 HashMap 定义为静态来解决我的问题。

public static HashMap<ObjectId, User> users

so I can easily have access to it from anywhere by using the following code:所以我可以使用以下代码从任何地方轻松访问它:

HashMap<ObjectId, User> users = Main.users or any method eg Main.users.getId(); HashMap<ObjectId, User> users = Main.users或任何方法,例如Main.users.getId();

Another solution could have been to create a property within your "User" class that contains a list of related users, and if you know that one user is related to another, added it to the each user as you build the list.另一种解决方案可能是在您的“用户”类中创建一个包含相关用户列表的属性,如果您知道一个用户与另一个用户相关,则在构建列表时将其添加到每个用户中。

public class User {
  ...    
  private List<User> relatedUsers = new ArrayList<User>();

  ...

  private void updateRelatedUsers() {
    for(User relatedUser : relatedUsers) {
      //do stuff to update the relatedUser object.
      relatedUser.setSomething(someValue);
    }
  }

  //Getter and setter
  public List<User> getRelatedUsers() {
    return relatedUsers;
  }
  public void setRelatedUsers(List<User> relatedUsers) {
    this.relatedUsers = relatedUsers;
  }

  ...
}

Add the users like so:像这样添加用户:

...
User myUser = creatUserHoweverYouDo();
User myRelatedUser = getMyRelatedUser(myUser);

myUser.getRelatedUsers().add(myRelatedUser);
...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM