有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何在POJO类中使用HashMap并检索它?

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

我试过我的构造函数,我不知道如何使用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;
    }
}

共 (1) 个答案

  1. # 1 楼答案

    你的问题令人困惑

    是否要拥有HashMap私有数据成员?您始终可以这样做:

    public class CurrentAttendance {
        private Map something = new HashMap();
    
        public Map getSomething() { return Collections.unmodifiableMap(this.something); }
    }