简体   繁体   English

重构:地图 <String, Double> 到地图 <String, Double or String>

[英]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 -> DoubleString -> 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.

相关问题 如何映射Map <String,Double> - How to map a Map<String,Double> 在地图内通过键获取价值 <String, Map<String, Double> &gt; JSP - Get value by key inside a Map<String, Map<String, Double>> JSP 路线:将双精度数组字符串转换为字符串,字符串(键和值)的映射 - itinerary: making double array String into a Map of String, String (Key & Value) 映射地图 <String, Double> 使用Hibernate和JPA - Mapping a Map<String, Double> with Hibernate and JPA Map:定义类型整数和双精度但不是字符串的方法 - Map: Defining a Method for Type Integer and Double but not String 使用key = String和value = ArrayList创建映射<Double> - Create a map with key=String and value=ArrayList<Double> 对包含双精度和字符串值的哈希映射进行排序 - Sort a hash map that contains double and string values 转换List时ClassCastException <Map<String,Double> &gt;到TreeSet <Map<String,Double> &gt; - ClassCastException when converting List<Map<String,Double>> to TreeSet<Map<String,Double>> 如何对Hashmap进行排序<String, Map<String, Double> &gt; 在 Java 中? - How to sort Hashmap<String, Map<String, Double>> in Java? java流列表 <Map<String,Double> &gt;需要基于字符串的平均值 - java stream List<Map<String,Double>> need average based on the string
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM