【问题标题】:Convert array with two types to HashMap, TreeMap, LinkedHashMap [closed]将具有两种类型的数组转换为 HashMap、TreeMap、LinkedHashMap [关闭]
【发布时间】:2015-12-01 07:54:13
【问题描述】:

创建数组:

{int, String}, {String, String}, {int, long}, {String,boolean}, {String, double}, {int, class Car}, {String, class Car}

转换成HashMap、TreeMap、LinkedHashMap

这是汽车类:

public class Car implements Comparable{

    int id;
    String car_name;
    String number;

    public Car(int id, String car_name, String number) {
        this.id = id;
        this.car_name = car_name;
        this.number = number;
    }

    @Override
    public String toString() {
        return "Car{" +
                "id='" + id + '\'' +
                ", car_name='" + car_name + '\'' +
                ", number='" + number + '\'' +
                '}';
    }
}

有我的代码:

public class Main {

    private static HashMap<Integer,String> toHM(Object[][] a){
        HashMap<Integer,String> h = new HashMap<>();

        for (Object[] o : a){
            h.put((Integer) o[0], (String) o[1]);
        }
        return h;
    }

    private static HashMap<Integer,String> toLHM(Object[][] a){
        HashMap<Integer,String> h = new LinkedHashMap<>();

        for (Object[] o : a){
            h.put((Integer) o[0], (String) o[1]);
        }
        return h;
    }

    private static TreeMap<Integer,String> toTM(Object[][] a){
        TreeMap<Integer,String> h = new TreeMap<>();

        for (Object[] o : a){
            h.put((Integer) o[0], (String) o[1]);
        }
        return h;
    }



    public static void main(String[] args) {

        Object[][] a = new Object[2][2];
        a[0][0] = 1;
        a[0][1] = "test";
        a[1][0] = 2;
        a[1][1] = "other test";

       HashMap<Integer,String> aHM = toHM(a);
       HashMap<Integer,String> aLHH = toLHM(a);
       TreeMap<Integer,String> aTM = toTM(a);

    }
}

这是正确的代码吗?可能,最好在该代码中使用泛型? 我知道如何解决这个问题吗?

【问题讨论】:

  • 您不能只将对象转换为字符串或整数。而是覆盖您的方法以采用不同的数组或在其中包含一个 instanceof if 语句。

标签: java arrays object converter typeconverter


【解决方案1】:

检查一下:

@SuppressWarnings("unchecked")
public static <S, T1, T2> S convert(Object[] inputobject, Class<S> mapType,
        Class<T1> keyType, Class<T2> valueType)
        throws IllegalAccessException, InstantiationException {
    Map<T1, T2> map = (Map<T1, T2>) mapType.newInstance();
    map.put((T1) inputobject[0], (T2) inputobject[1]);
    return (S) map;
}
  • inputObject:对象数组
  • ma​​pType :地图类,例如。 Hashmap、Treemap 等
  • keyType :Key 类,例如。字符串、整数等
  • valueType:值的类别

示例输入:

Object[] obj = new Object[] { 1, "test" };
System.out.println(convert(obj, HashMap.class, Integer.class, String.class));

样本输出:

{1=test}

我希望这会奏效。

【讨论】:

    【解决方案2】:
    1. 根据标准,您应该始终对接口进行编码。我的意思是引用应该是接口,对象应该是实现。
    2. 我看到你在所有三个方法中都有相同的主体,我不能有一个返回地图的方法。您可以添加参数并将其分配给引用。
    3. 调用函数可以捕获所需的返回类型。

    下面是一个伪代码,供您参考

    例如。

    调用函数:

    HashMap hMap = getMap(Object[][] a, HashMap.class);
    

    方法签名:

    private <R> R (Object[][] a,Class<R> responseClazz){
        Map mp = new responseClazz();
        .....
        return mp;
    }
    

    【讨论】:

      猜你喜欢
      • 2015-11-30
      • 1970-01-01
      • 2011-02-22
      • 1970-01-01
      • 1970-01-01
      • 2012-05-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多