Java 无法将多个值设置为一个枚举
我有两个enum类College.java和Student.java 下面是这两个类的代码片段Java 无法将多个值设置为一个枚举,java,enums,hashmap,Java,Enums,Hashmap,我有两个enum类College.java和Student.java 下面是这两个类的代码片段 public enum College { ID("Id of College"), NAME("Name of College"), ADDRESS("Address of College"); private final String description; private College(String deta
public enum College {
ID("Id of College"), NAME("Name of College"), ADDRESS("Address of College");
private final String description;
private College(String details) {
this.description = description;
}
}
public enum Student {
ROLL(College.ID, "Roll No of Student"), CLASS(College.NAME, "Class of Student"),
PLACE(College.ADDRESS, "Place of Student"), STUDENTID(setMap());
private final String description;
private final College college;
private Map<college, String> attributes;
private Student(College college, String details) {
this.college = college;
this.details = details;
}
private Student(Map<College, String> attributes) {
this.attributes = attributes;
for(college key: attributes.keySet()) {
this.college=key;
this.description=attributes.get(key);
}
}
private Map<College, String> setMap() {
Map<college, String> map = new HashMap<college, String>();
map.put(College.ID ,"ID of college");
map.put(College.NAME,"Name of Student");
map.put(College.ADDRESS,"Address of the College");
return map;
}
}
公立enum学院{
ID(“学院ID”)、名称(“学院名称”)、地址(“学院地址”);
私有最终字符串描述;
私立学院(详细信息){
this.description=描述;
}
}
公共普查学生{
卷(College.ID,“学生卷号”)、班级(College.NAME,“学生班级”),
地点(College.ADDRESS,“学生所在地”),学生ID(setMap());
私有最终字符串描述;
私立大学;
私有地图属性;
私立学生(学院,字符串详细信息){
这个学院=学院;
this.details=详细信息;
}
私立学生(地图属性){
this.attributes=属性;
for(学院密钥:attributes.keySet()){
这个。学院=钥匙;
this.description=attributes.get(key);
}
}
私有映射setMap(){
Map Map=newhashmap();
地图放置(College.ID,“学院ID”);
地图放置(学院名称,“学生姓名”);
地图放置(学院地址,“学院地址”);
返回图;
}
}
我的问题是,我无法将多个学院枚举设置为一个学生枚举(STUDENTID)。当我尝试使用助手方法并在Map中设置值时,在初始化STUDENTID enum时只设置了一个值。我希望为STUDENTID枚举初始化所有映射值。
还有别的办法吗?请帮帮我,我真的被困在这里了根据我们的讨论,我相信共享的代码将更改为下面的代码
public enum Student {
ROLL(College.ID, "Roll No of Student"), CLASS(College.NAME, "Class of Student"),
PLACE(College.ADDRESS, "Place of Student"),
STUDENTID(
new MyEntry<College, String>(College.ID, "ID of college"),
new MyEntry<College, String>(College.NAME, "Name of Student"),
new MyEntry<College, String>(College.ADDRESS, "Address of the College")
);
private Map<College, String> attributes = new HashMap<,>();
private Student(College college, String details) {
attributes.put(college, details);
}
private Student(MyEntry<College, String>... attributes) {
for(MyEntry<College, String> entry : attributes) {
this.attributes.put(entry.getKey(), entry.getValue());
}
}
}
由于您在讨论中提到您使用的是Java 7,所以您无法访问Java 9Map.ofEntriesMap
或Map.entry
(已描述),因此您必须使用stackoverflow post来模拟这一点。这就是MyEntry课程的来源
这没有经过测试,因为我现在没有JavaIDE设置。不过,我确实对许多函数进行了研究,其中大部分应该是正确的
正如评论和聊天中提到的,使用
enum
是一种反模式,应该避免使用。为什么在这里标记c
?有什么原因不能只使用常规类而不使用enum吗?我标记它是因为来自c的人可能也知道它。。你能帮我解决上面的问题吗?prod中已经有代码了,我们收到了这样的请求,多个学院枚举设置为一个学生枚举,使用@kiranBiradar,这不是标记c的有效原因。如果是这样的话,你可能会给很多语言加上标签。
private final String description;
private final College college;