[英]Refactoring: Map<String, Double> to Map<String, Double or String>
I've got widely used method like: 我有广泛使用的方法,例如:
public Map<String, Double> parseData(String[] data) {
.................
Where data
is something like new String[] { "column1 -> 2.00", "column2 -> New York", ... }
data
就像new String[] { "column1 -> 2.00", "column2 -> New York", ... }
Problem: It appears that data
can contains both: String -> Double
& String -> String
values. 问题:
data
似乎可以同时包含: String -> Double
& String -> String
值。 So I need smth like: 所以我需要像这样的东西:
public Map<String, String or Double> parseData(String[] data) {
................
Question: Any ideas besides return Map<String, Object>
? 问题:除了返回
Map<String, Object>
之外,还有其他想法吗?
Create a Wrapper StringOrDouble
which will look a bit like this: 创建一个包装器
StringOrDouble
,看起来像这样:
public class StringOrDouble {
private String internalString;
private Double internalDouble;
public StringOrDouble(String input) {
internalString = input;
}
public StringOrDouble(Double input) {
internalDouble = input;
}
public boolean hasString() {
return internalString != null;
}
public boolean hasDouble() {
return internalDouble != null;
}
public String getString() {
return internalString;
}
public Double getDouble() {
return internalDouble;
}
}
Then have a map of type Map<String, StringOrDouble>
and use that. 然后使用
Map<String, StringOrDouble>
类型的Map<String, StringOrDouble>
并使用它。 When you use the values, you can check which one it is by testing with hasString()
and/or hasDouble()
. 使用这些值时,可以通过使用
hasString()
和/或hasDouble()
进行测试来检查它是哪一个。 Alternatively you could have an enum which determines which type it is. 或者,您可以使用一个枚举来确定它是哪种类型。
public Map<String, Container> parseData(String[] data)
You can introduce a wrapper class for this 您可以为此引入一个包装器类
public class Container {
private String s;
private Double d;
public Container(String s) {
this.s=s;
}
public Container(Double d) {
this.d=d;
}
public hasString() {
return s!=null;
}
public hasDouble() {
return d!=null;
}
//getters/setters
}
As far as I understand, you want something like Map<String, ? extends String || Double
据我了解,您想要类似
Map<String, ? extends String || Double
Map<String, ? extends String || Double
Map<String, ? extends String || Double
as the return type, but no such thing is supported in Java: Map<String, ? extends String || Double
用作返回类型,但Java不支持此类:
4.9 Intersection Types An intersection type takes the form T1 & ... & Tn, n>0, where Ti, 1in, are type expressions.
4.9交集类型交集类型采用T1&...&Tn,n> 0的形式,其中Ti,1in是类型表达式。 Intersection types arise in the processes of capture conversion (§5.1.10) and type inference (§15.12.2.7).
交集类型出现在捕获转换(第5.1.10节)和类型推断(第15.12.2.7节)的过程中。 It is not possible to write an intersection type directly as part of a program;
不可能将交集类型直接编写为程序的一部分; no syntax supports this.
没有语法支持此功能。 The values of an intersection type are those objects that are values of all of the types Ti, for 1in.
相交类型的值是那些对象,即所有类型Ti的值,为1in。
So you'd better parse the input array and hold different arrays for each different type or you can use a wrapper class to represent the values in the map returned, as some other answerers explained. 因此,您最好解析输入数组并为每种不同的类型保存不同的数组,或者可以使用包装器类来表示返回的映射中的值,如其他答复者所述。
使用超类:
public Map<String, Object> parseData(String[] data)
Just an alternative to @blalasaadri. 只是@blalasaadri的替代方法。 don't pretend to be better:
不要假装变得更好:
public static class StringDoubleValue {
private final Optional<String> sValue;
private final Optional<Double> dValue;
public MetricValue(String sValue) {
this.sValue = Optional.of(sValue);
this.dValue = Optional.absent();
}
public MetricValue(Double dValue) {
this.sValue = Optional.absent();
this.dValue = Optional.of(dValue);
}
public Object get() {
return (sValue.isPresent()) ? sValue.get() : dValue.get();
}
@Override
public String toString() {
if (sValue.isPresent()) ? sValue.get() : dValue.get().toString();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.