首页 > 解决方案 > 如何在 POJO 类中使用 HashMap 并检索它?

问题描述

我应该在我的 POJO 中哪里写 hashmap?

我试过我的构造函数,我不知道如何使用哈希图?特别是在我的 POJO 课上 -->

public class CurrentAttendance {

    private String name;
    private int userCount;
    private int currentAttendance;
    private long firstJoin;
    private Object timestamp;

    //empty constructor
    public CurrentAttendance() {
    }

    //entry values
    public CurrentAttendance(String name, int userCount, int currentAttendance, long firstJoin, Object timestamp) {
        this.name = name;
        this.userCount = userCount;
        this.currentAttendance = currentAttendance;
        this.firstJoin = firstJoin;
        this.timestamp = timestamp;
    }

这是我的吸气剂

    public String getXName(){
        return name;
    }
    public int getUserCount(){
        return userCount;
    }
    public int getCurrentAttendance() {
        return currentAttendance;
    }
    public long getFirstJoin(){
        return firstJoin;
    }
    public Object getTimestamp(){
        return timestamp;
    }
}

标签: javahashmap

解决方案


你的问题令人困惑。

你想拥有一个 HashMap 私有数据成员吗?你总是可以这样做:

public class CurrentAttendance {
    private Map something = new HashMap();

    public Map getSomething() { return Collections.unmodifiableMap(this.something); }
}

推荐阅读