Java 如何实现具有多个可能重复的字符串属性的枚举类?
作为使用Java的OOP中的后台刷新者,我试图解决一个设计问题: 我有很多人的名字,还有名字、年龄和职业。我想实现一个ListOfPersons枚举类,它就像一个包含所有这些信息的数据库。唯一的问题是,此列表中的许多数据都是重复的,所以例如,年龄和职业属性可能有两个或三个条目Java 如何实现具有多个可能重复的字符串属性的枚举类?,java,oop,enums,hashmap,treemap,Java,Oop,Enums,Hashmap,Treemap,作为使用Java的OOP中的后台刷新者,我试图解决一个设计问题: 我有很多人的名字,还有名字、年龄和职业。我想实现一个ListOfPersons枚举类,它就像一个包含所有这些信息的数据库。唯一的问题是,此列表中的许多数据都是重复的,所以例如,年龄和职业属性可能有两个或三个条目 Example: Enum object one- Name: Joe Age: 40 Age: 41 Occupation: Engineer Occupation: Software Engineer 你可以看到J
Example:
Enum object one-
Name: Joe
Age: 40
Age: 41
Occupation: Engineer
Occupation: Software Engineer
你可以看到Joe有两个年龄条目和两个职业条目
Example 2:
Enum object two-
Name: Judy
Age: 55
Age: 65
Occupation: Judge
在例2中,年龄显示了两次
关键是,有一堆这样的条目必须在枚举类中实现设置它(我的enum类人员)的最佳方法是什么,以便转移到树状图或哈希图(或者是否有更好的数据结构可以用作用户搜索人员的数据库)?
问题是,我不确定如何考虑这些随机重复条目,这使得以后很难将其放入数据结构中
public enum ListOfPersons{
Joe("Joe", 40, 41, "Engineer", "Software Engineer"), Judy("Judy", 55, 65,
"Judge")...etc.
//methods
}
对不起我的英语
如何将自定义类与HashMap一起使用
像这样
class Person {
private List<Integer> ages;
private String name;
...
// getter setter..
// equals hashcode..
}
Map<String, Person> maps = new HashMap<>();
maps.add("Joe", new Person(...));
map.get("Joe");
...
班级人员{
私人名单年龄;
私有字符串名称;
...
//getter setter。。
//等于hashcode。。
}
Map maps=新的HashMap();
添加(“乔”,新人(…);
map.get(“乔”);
...
为什么要将其实现为enum?对不起,我的英语太差了
如何将自定义类与HashMap一起使用
像这样
class Person {
private List<Integer> ages;
private String name;
...
// getter setter..
// equals hashcode..
}
Map<String, Person> maps = new HashMap<>();
maps.add("Joe", new Person(...));
map.get("Joe");
...
班级人员{
私人名单年龄;
私有字符串名称;
...
//getter setter。。
//等于hashcode。。
}
Map maps=新的HashMap();
添加(“乔”,新人(…);
map.get(“乔”);
...
为什么要将其实现为枚举?
enum
似乎不适合您的情况。一种更简单的方法是创建一个Person类(包含您需要的字段),然后重写equals()
和hashCode()
方法,将name作为唯一标识符(我假设name是唯一的,因为您试图将其作为枚举示例中的名称)。然后使用ArrayList
创建Person实例列表
要在以后的数据结构(如Map
(人名作为键,人名对象作为值)中对其进行处理,可以执行以下操作:
Map<String, Person> personsMap = persons.stream()
.collect(Collectors.toMap(Person::getName, Function.identity());
Map personsMap=persons.stream()
.collect(Collectors.toMap(Person::getName,Function.identity());
然后检索:
PersonMap.get(“Joe”)
为您提供Person实例enum
似乎不适合您的情况。更简单的方法是使用Person类(带有您需要的字段),然后重写equals()
和hashCode()
方法以包含名称作为唯一标识符(我假设名称是唯一的,因为您尝试将其作为枚举示例中的名称)。然后使用ArrayList
创建Person实例列表
要在以后的数据结构(如Map
(人名作为键,人名对象作为值)中对其进行处理,可以执行以下操作:
Map<String, Person> personsMap = persons.stream()
.collect(Collectors.toMap(Person::getName, Function.identity());
Map personsMap=persons.stream()
.collect(Collectors.toMap(Person::getName,Function.identity());
然后检索:
personsMap.get(“Joe”)
为您提供Person实例我也建议您使用Hashmap,但如果您仍然使用Enum,则可以使用如下方式存储它们
public enum ListOfPersons{
Joe("Joe", "40,41","Engineer,Software Engineer"), Judy("Judy", "55,65","Judge")...etc.
//methods
}
通过这种方式,您将始终获得3个字符串作为值,然后您可以应用简单的字符串处理将它们转换为您的数据结构我也建议使用Hashmap,但如果您一直使用Enum,则可以使用如下方式存储它们
public enum ListOfPersons{
Joe("Joe", "40,41","Engineer,Software Engineer"), Judy("Judy", "55,65","Judge")...etc.
//methods
}
这样,您将始终获得3个字符串作为值,然后您可以应用简单的字符串处理将它们转换为您的数据结构不确定是否需要这些字符串,但下面的代码中可能有一些有用的提示
import java.util.Set;
import java.util.TreeSet;
import java.util.stream.Collectors;
import java.util.Map;
import java.util.TreeMap;
public class Test {
public static void main(String[] args) {
// load some test data
Set<Integer> joesAges = Person.JOE.getAges();
joesAges.add(21);
joesAges.add(34);
joesAges.add(43);
Set<String> joesOccupations = Person.JOE.getOccupations();
joesOccupations.add("Engineer");
joesOccupations.add("Software Engineer");
joesOccupations.add("Mechanical Engineer");
// .... and so on for all the names in Person enum
String searchTerm = "joe occupation"; // hardcoded search terms, it should be replaced with user's input
String searchTermSeparator = " ";
int indexOfSearchTermsSeparator = searchTerm.indexOf(searchTermSeparator);
String searchName = searchTerm;
String searchDetails = "age"; // initialze with age as default option for search, if user only types a name
if (indexOfSearchTermsSeparator > -1) {
searchName = searchTerm.substring(0, indexOfSearchTermsSeparator);
searchDetails = searchTerm.substring(indexOfSearchTermsSeparator + 1);
}
try {
Person person = Person.valueOf(searchName.toUpperCase());
if ("age".equals(searchDetails)) {
// list all ages for searched name
String agesString = person.getAges()
.stream()
.map(age -> {return "" + age;})
.collect(Collectors.joining( ", age: " ));
System.out.println(searchName + " age: " + agesString);
} else {
// list all occupations of searched name
String occupationsString = person.getOccupations()
.stream()
.collect(Collectors.joining(", occupation: " ));
System.out.println(searchName + " occupation: " + occupationsString);
}
} catch(Exception e) {
System.out.println("Unknown name");
}
}
}
enum Person {
JOE(new TreeSet<Integer>(), new TreeSet<String>()),
JANE(new TreeSet<Integer>(), new TreeSet<String>()),
JUDY(new TreeSet<Integer>(), new TreeSet<String>())
//.... and so on for all the names
;
private Set<Integer> ages;
private Set<String> occupations;
Person(Set<Integer> ages, Set<String> occupations) {
this.ages = ages;
this.occupations = occupations;
}
public Set<Integer> getAges() {
return this.ages;
}
public Set<String> getOccupations() {
return this.occupations;
}
public void reset() {
this.ages = new TreeSet<>();
this.occupations = new TreeSet<>();
}
}
import java.util.Set;
导入java.util.TreeSet;
导入java.util.stream.collector;
导入java.util.Map;
导入java.util.TreeMap;
公开课考试{
公共静态void main(字符串[]args){
//加载一些测试数据
Set joesAges=Person.JOE.getAges();
加入(21);
添加(34);
添加(43);
Set joesOccupations=Person.JOE.getOccupations();
添加(“工程师”);
添加(“软件工程师”);
添加(“机械工程师”);
//…以此类推,适用于所有个人姓名枚举
String searchTerm=“joe occulation”;//硬编码的搜索词,应替换为用户输入
字符串searchTermSeparator=“”;
int indexOfSearchTermsSeparator=searchTerm.indexOf(searchtermsseparator);
字符串searchName=searchTerm;
String searchDetails=“age”//如果用户只键入名称,则使用age作为搜索的默认选项初始化
如果(indexOfSearchTermsSeparator>-1){
searchName=searchTerm.substring(0,indexOfSearchTermsSeparator);
searchDetails=searchTerm.substring(indexOfSearchTermsSeparator+1);
}
试一试{
Person-Person=Person.valueOf(searchName.toUpperCase());
如果(“年龄”。等于(搜索详细信息)){
//列出搜索名称的所有年龄
字符串agesString=person.getAges()
.stream()
.map(年龄->{return”“+age;})
收集(收集者加入(,年龄:);
System.out.println(searchName+“age:”+agesString);
}否则{
//列出搜索名称的所有职业
字符串occupationsString=person.getOccupations()
.stream()
收藏(收藏者加入(,职业:);
s