获取具有多个属性的枚举列表并将它们放入 ArrayList 或 HashMap 中?
Taking a list of enums with multiple attributes and putting them into an ArrayList or HashMap?
我有一个人的枚举 class,每个人的枚举被定义为:
enumName(String name, String age, String Occupation)
示例:
public enum People{
Joe1("Joe","31","Engineer"),
Joe2("Joe","35","Manager"),
Sam1("Sam", "22", "Student"),
Sam1("Sam", "22", "Student"),
Sam1("Sam", "23", "Student"),
Sam2("Sam", "30", "lawyer"),
Sam3("Sam", "45", "judge");
}
Joe1 和 Joe2 代表同一个人 Joe 的数据,但代表不同的 age/occupation。我怎样才能制作一个 ArrayList 或 Hashmap/table 的名称以及可以通过人名搜索的不同 ages/occupations?
所以我不确定数据结构会是什么样子,即使它是正确的,但我希望能够搜索一个名字并获得与该名字相关的每个年龄和职业值,所以例如,我会搜索 "Joe" 并返回:
Joe-31-Engineer
Joe-35-Manager
对于 Sam 的值,有重复项,但我仍然希望在搜索 Sam 的名字时能够看到它们 "Sam":
Sam-22-Student
Sam-22-Student
Sam-23-Student
Sam-30-lawyer
Sam-45-judge
你可以有一个Map<String, List<People>>
。对于每个名称,您将有一个对应的值,即具有该名称的 enum
值的列表。同名的枚举值也不能超过一个,因此只能有一个 Sam1
.
您可以在枚举中创建一个静态变量,该变量由 personName
的人员组初始化:
enum People {
Joe1("Joe", "31", "Engineer"),
Joe2("Joe", "35", "Manager"),
Sam1("Sam", "22", "Student"),
Sam2("Sam", "30", "lawyer"),
Sam3("Sam", "45", "judge");
private String personName;
private String age;
private String occupations;
private static Map<String, List<People>> byName;
static {
byName = Arrays.stream(values())
.collect(Collectors.groupingBy(People::getPersonName,
Collectors.toList()));
}
People(String personName, String age, String occupations) {
this.personName = personName;
this.age = age;
this.occupations = occupations;
}
public static List<People> getByName(String personName) {
return byName.get(personName);
}
}
然后您可以使用以下声明按姓名搜索人(年龄、职业):
List<People> p = People.getByName("Joe");
我有一个人的枚举 class,每个人的枚举被定义为:
enumName(String name, String age, String Occupation)
示例:
public enum People{
Joe1("Joe","31","Engineer"),
Joe2("Joe","35","Manager"),
Sam1("Sam", "22", "Student"),
Sam1("Sam", "22", "Student"),
Sam1("Sam", "23", "Student"),
Sam2("Sam", "30", "lawyer"),
Sam3("Sam", "45", "judge");
}
Joe1 和 Joe2 代表同一个人 Joe 的数据,但代表不同的 age/occupation。我怎样才能制作一个 ArrayList 或 Hashmap/table 的名称以及可以通过人名搜索的不同 ages/occupations?
所以我不确定数据结构会是什么样子,即使它是正确的,但我希望能够搜索一个名字并获得与该名字相关的每个年龄和职业值,所以例如,我会搜索 "Joe" 并返回:
Joe-31-Engineer
Joe-35-Manager
对于 Sam 的值,有重复项,但我仍然希望在搜索 Sam 的名字时能够看到它们 "Sam":
Sam-22-Student
Sam-22-Student
Sam-23-Student
Sam-30-lawyer
Sam-45-judge
你可以有一个Map<String, List<People>>
。对于每个名称,您将有一个对应的值,即具有该名称的 enum
值的列表。同名的枚举值也不能超过一个,因此只能有一个 Sam1
.
您可以在枚举中创建一个静态变量,该变量由 personName
的人员组初始化:
enum People {
Joe1("Joe", "31", "Engineer"),
Joe2("Joe", "35", "Manager"),
Sam1("Sam", "22", "Student"),
Sam2("Sam", "30", "lawyer"),
Sam3("Sam", "45", "judge");
private String personName;
private String age;
private String occupations;
private static Map<String, List<People>> byName;
static {
byName = Arrays.stream(values())
.collect(Collectors.groupingBy(People::getPersonName,
Collectors.toList()));
}
People(String personName, String age, String occupations) {
this.personName = personName;
this.age = age;
this.occupations = occupations;
}
public static List<People> getByName(String personName) {
return byName.get(personName);
}
}
然后您可以使用以下声明按姓名搜索人(年龄、职业):
List<People> p = People.getByName("Joe");