简体   繁体   English

在Map值中强制两种类型-Java泛型

[英]Enforce two types in Map value - Java Generics

I have a situation, I need to use Map , and put String as key and String or Long as value. 我有一种情况,我需要使用Map ,并将String作为键,将StringLong作为值。

As they don't fall into same hierarchy, I think I can't use wildcards. 由于它们不属于同一层次结构,因此我认为我不能使用通配符。

So is there any way to enforce compiler to accept either String or Long as value or I will have to go with Map without generics . 因此,有什么方法可以强制编译器接受StringLong作为值,否则我将不得不使用没有泛型的 Map

Two possible solutions either use Map<String, Object> or create a class like 两种可能的解决方案要么使用Map<String, Object>要么创建一个类似

public class DualValue {
    private final String stringValue;
    private final Long   longValue;

    public DualValue(final String stringValue) {
        this.stringValue = stringValue;
        this.longValue = null;
    }

    public DualValue(final Long longValue) {
        this.stringValue = null;
        this.longValue = longValue;
    }

    public String getStringValue() {
        return stringValue;
    }

    public Long getLongValue() {
        return longValue;
    }

    public boolean isString() {
        return stringValue != null;
    }

    public boolean isLong() {
        return longValue != null;
    }

    // next two are optional but should be implemented,
    // if you ever want to use this class as key of a Map

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }

        DualValue dualValue = (DualValue) o;

        if (longValue != null ? !longValue.equals(dualValue.longValue) : dualValue.longValue != null) {
            return false;
        }
        if (stringValue != null ? !stringValue.equals(dualValue.stringValue) : dualValue.stringValue != null) {
            return false;
        }

        return true;
    }

    @Override
    public int hashCode() {
        int result = stringValue != null ? stringValue.hashCode() : 0;
        result = 31 * result + (longValue != null ? longValue.hashCode() : 0);
        return result;
    }
} 

and use Map<String, DualValue> . 并使用Map<String, DualValue> You are right that the compiler is not able to merge two destinct class-hirachies with a pure genric approach in another way than using Object which is common parent to every Java class. 没错,编译器无法使用纯泛型方法将两个预定的类层次合并,而不是使用每个Java类都是公共父Object Even if it could you would probably have to check by using value instanceof String or value instanceof Long and cast to one of those if you would want to call any functions on the value not present in Object . 即使可能,您可能需要使用value instanceof Stringvalue instanceof Long value instanceof String进行检查,如果要对Object不存在的值调用任何函数,则将其强制转换为其中之一。

我认为您只需要存储一个对象,这与不带泛型的对象存储几乎一样(当然,仍然会检查String键,并且不会收到编译器警告)。

将long转换为字符串,然后可以得到<String,String>的映射

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM