Java 有没有办法共享重叠贴图的元素?

Java 有没有办法共享重叠贴图的元素?,java,enumeration,Java,Enumeration,这是我在编程时偶然发现的一点东西,我想知道这里是否有人能为我提供一些见解 假设您有一些类似于enum的类,即具有定义为类常量的实例负载的类。作为一个例子,考虑一些类似 public class ChildName { private static final Map<String, ChildName> LUT = new TreeMap<>(); private final String name; private ChildName(St

这是我在编程时偶然发现的一点东西,我想知道这里是否有人能为我提供一些见解

假设您有一些类似于enum的类,即具有定义为类常量的实例负载的类。作为一个例子,考虑一些类似

public class ChildName {

    private static final Map<String, ChildName> LUT = new TreeMap<>();

    private final String name;

    private ChildName(String name) {
        this.name = name.toLowerCase();
        LUT.put(name, this);
    }

    public static Collection<ChildName> getNames() {
       return LUT.values();
    }

    public static ChildName fromValue(String name) {
        return LUT.get(name);
    }

    public static final ChildName SARAH = new ChildName("Sarah");
    public static final ChildName MEGAN = new ChildName("Megan");
    public static final ChildName SANDY = new ChildName("Sandy");
    public static final ChildName JOHN = new ChildName("John");
    public static final ChildName BORIS = new ChildName("Boris");
    // etc...
}
public class ChildName {

    private static final Map<String, ChildName> LUT = new TreeMap<>();

    private final String name;

    protected ChildName(String name) {
        this.name = name.toLowerCase();
        LUT.put(name, this);
    }

    public static Collection<ChildName> getNames() {
        return LUT.values();
    }

    public static ChildName fromValue(String name) {
        return LUT.get(name);
    }

    // maybe here there are some gender-neutral names as constants left
    public static final ChildName ALEX = new ChildName("Alex");
}

public class GirlsName {

    private static final Map<String, ChildName> LUT = new TreeMap<>();

    private GirlsName(String name) {
        super(name);
        LUT.put(name, this);
    }

    public static Collection<ChildName> getNames() {
        return LUT.values();
    }

    public static final ChildName SARAH = new ChildName("Sarah");
    public static final ChildName MEGAN = new ChildName("Megan");
    public static final ChildName SANDY = new ChildName("Sandy");
    // etc...
}

public class BoysName {

    private static final Map<String, ChildName> LUT = new TreeMap<>();

    private BoysName(String name) {
        super(name);
        LUT.put(name, this);
    }

    public static Collection<ChildName> getNames() {
        return LUT.values();
    }

    public static final ChildName JOHN = new ChildName("John");
    public static final ChildName BORIS = new ChildName("Boris");
    // etc...
}
公共类ChildName{
私有静态最终映射LUT=newtreemap();
私有最终字符串名;
私有子名称(字符串名称){
this.name=name.toLowerCase();
LUT.put(名称、本);
}
公共静态集合getNames(){
返回LUT.values();
}
公共静态子名称fromValue(字符串名称){
返回LUT.get(名称);
}
public static final ChildName SARAH=new ChildName(“SARAH”);
public static final ChildName MEGAN=新的ChildName(“MEGAN”);
public static final ChildName SANDY=新的ChildName(“SANDY”);
public static final ChildName JOHN=新的ChildName(“JOHN”);
public static final ChildName BORIS=新的ChildName(“BORIS”);
//等等。。。
}
现在,有人会争辩说,男孩的名字和女孩的名字可能分开(或者考虑来自不同国家的名字或者别的什么,你知道这个想法)。这个想法是,你仍然可以列出所有可能的名字,但也可以列出所有男孩的名字或女孩的名字。这可以用类似的方法来实现

public class ChildName {

    private static final Map<String, ChildName> LUT = new TreeMap<>();

    private final String name;

    private ChildName(String name) {
        this.name = name.toLowerCase();
        LUT.put(name, this);
    }

    public static Collection<ChildName> getNames() {
       return LUT.values();
    }

    public static ChildName fromValue(String name) {
        return LUT.get(name);
    }

    public static final ChildName SARAH = new ChildName("Sarah");
    public static final ChildName MEGAN = new ChildName("Megan");
    public static final ChildName SANDY = new ChildName("Sandy");
    public static final ChildName JOHN = new ChildName("John");
    public static final ChildName BORIS = new ChildName("Boris");
    // etc...
}
public class ChildName {

    private static final Map<String, ChildName> LUT = new TreeMap<>();

    private final String name;

    protected ChildName(String name) {
        this.name = name.toLowerCase();
        LUT.put(name, this);
    }

    public static Collection<ChildName> getNames() {
        return LUT.values();
    }

    public static ChildName fromValue(String name) {
        return LUT.get(name);
    }

    // maybe here there are some gender-neutral names as constants left
    public static final ChildName ALEX = new ChildName("Alex");
}

public class GirlsName {

    private static final Map<String, ChildName> LUT = new TreeMap<>();

    private GirlsName(String name) {
        super(name);
        LUT.put(name, this);
    }

    public static Collection<ChildName> getNames() {
        return LUT.values();
    }

    public static final ChildName SARAH = new ChildName("Sarah");
    public static final ChildName MEGAN = new ChildName("Megan");
    public static final ChildName SANDY = new ChildName("Sandy");
    // etc...
}

public class BoysName {

    private static final Map<String, ChildName> LUT = new TreeMap<>();

    private BoysName(String name) {
        super(name);
        LUT.put(name, this);
    }

    public static Collection<ChildName> getNames() {
        return LUT.values();
    }

    public static final ChildName JOHN = new ChildName("John");
    public static final ChildName BORIS = new ChildName("Boris");
    // etc...
}
公共类ChildName{
私有静态最终映射LUT=newtreemap();
私有最终字符串名;
受保护的子名称(字符串名称){
this.name=name.toLowerCase();
LUT.put(名称、本);
}
公共静态集合getNames(){
返回LUT.values();
}
公共静态子名称fromValue(字符串名称){
返回LUT.get(名称);
}
//也许这里还有一些性别中立的名字
public static final ChildName ALEX=新的ChildName(“ALEX”);
}
公共类GirlsName{
私有静态最终映射LUT=newtreemap();
私有GirlsName(字符串名称){
超级(姓名);
LUT.put(名称、本);
}
公共静态集合getNames(){
返回LUT.values();
}
public static final ChildName SARAH=new ChildName(“SARAH”);
public static final ChildName MEGAN=新的ChildName(“MEGAN”);
public static final ChildName SANDY=新的ChildName(“SANDY”);
//等等。。。
}
公务舱男孩姓名{
私有静态最终映射LUT=newtreemap();
private BoysName(字符串名称){
超级(姓名);
LUT.put(名称、本);
}
公共静态集合getNames(){
返回LUT.values();
}
public static final ChildName JOHN=新的ChildName(“JOHN”);
public static final ChildName BORIS=新的ChildName(“BORIS”);
//等等。。。
}
这可能导致每个类中出现大量重叠的
Map
s

在这种情况下,关于效率或内存的问题可能没有那么多,但尽管如此,它仍然是相当冗余的,感觉不太好

可以认为,可以省略上层类中的
映射
(假设子类中的中性名称只是重复的),并且可以通过组合子类的集合来检索所有结果的集合。然而,我想从设计的角度避免这种方法(想象一下,在这个星球上每个国家都有一个子类的情况下,这种方法会变得多么混乱)

(在我看来)一个更优雅的解决方案是在每个类中保留一个
映射
,但它们共享条目。现在我想知道是否有人现在会这样做,如果这可以使用一个基本的
Map
实现。当然,解决这类问题的其他建议也值得欢迎


PS:我知道我提出的优雅解决方案实际上并没有那么好,但我想不出更好的主意,我不禁想知道如何实现这种映射

interface ChildName {}

enum GirlsName implements ChildName  {
    Sarah, Megan, Sandy
}

enum BoysName implements ChildName  {
    John, Boris
}
现在不需要查找地图:

ChildName name = BoysName.valueOf("Boris");

我将让您编写代码来查找男孩或女孩的名字。

enum
-更进一步,让它们成为
enums

interface ChildName {}

enum GirlsName implements ChildName  {
    Sarah, Megan, Sandy
}

enum BoysName implements ChildName  {
    John, Boris
}
现在不需要查找地图:

ChildName name = BoysName.valueOf("Boris");

我将让您编写代码来查找男孩或女孩的名字。

我建议使用实际的
枚举
,而不是将它们分组到不同的类中,将诸如国家和性别之类的属性存储在
ChildName
类中,然后根据需要进行筛选和分组

public enum Gender {
    MALE, FEMALE
}

public enum Country {
    USA, CA, UK
}

public enum ChildName {
    SARAH(FEMALE, USA),
    MEGAN(FEMALE, CA),
    SANDY(FEMALE, UK),
    JOHN(MALE, USA),
    BORIS(MALE, UK);

    private static final Map<String, ChildName> LUT;
    static {
        LUT = Array.stream(values())
                .collect(Collectors.groupingBy(c -> c.name().toLowerCase()));
    }

    public static Collection<ChildName> getNames() {
       return Arrays.asList(values());
    }

    public static ChildName fromValue(String name) {
        return LUT.get(name.toLowerCase());
    }

    // some examples of filtering and grouping
    // if necessary, they can be cached statically (like LUT)
    public static List<String> getMaleNames() {
        return Arrays.stream(values())
                .filter(c -> c.getGender() == MALE)
                .map(ChildName::name)
                .collect(Collectors.toList());
    }

    public static Map<Gender, List<ChildName>> getGenderMapForCountry(Country country) {
        return Arrays.stream(values())
                .filter(c -> c.getCountry() == country)
                .collect(Collectors.groupingBy(ChildName::getGender));
    }


    private final Gender gender;
    private final Country country;

    ChildName(Gender gender, Country country) {
        this.gender = gender;
        this.country = country;
    }

    public Gender getGender() {
        return gender;
    }

    public Country getCountry() {
        return country;
    }
}
公共枚举性别{
男,女
}
公共枚举国家{
美国、加利福尼亚州、英国
}
公共枚举子名称{
莎拉(女,美国),
梅根(女,加利福尼亚州),
桑迪(女,英国),
约翰(男,美国),
鲍里斯(男,英国);
私有静态最终映射LUT;
静止的{
LUT=Array.stream(values())
.collect(Collectors.groupingBy(c->c.name().toLowerCase());
}
公共静态集合getNames(){
返回Arrays.asList(values());
}
公共静态子名称fromValue(字符串名称){
返回LUT.get(name.toLowerCase());
}
//筛选和分组的一些示例
//如有必要,可以静态缓存它们(如LUT)
公共静态列表getMaleNames(){
返回Arrays.stream(values())
.filter(c->c.getGender()==男性)
.map(ChildName::name)
.collect(Collectors.toList());
}
公共静态地图getGenderMapForCountry(国家/地区){
返回Arrays.stream(values())
.filter(c->c.getCountry()==国家)
.collect(Collectors.groupingBy(ChildName::getGender));
}
私人最终性别;
私人最终国家;
ChildName(性别、国家/地区){
这个。性别=性别;