看着很不顺眼,函数里面的代码是一样的,能不能整合成一个方法?既可以判断Map的大小也可以判断Collection public static <T extends Collection<?>> boolean isNormalData(
Context context, T data) {
if (data == null) {
DUIUtil.ToastText(context,
context.getString(R.string.tip_data_null));
return false;
} else if (data.isEmpty()) {
DUIUtil.ToastText(context,
context.getString(R.string.tip_data_empty));
return false;
}
return true;
} public static <T extends Map<?, ?>> boolean isNormalMapData(
Context context, T data) {
if (data == null) {
DUIUtil.ToastText(context,
context.getString(R.string.tip_data_null));
return false;
} else if (data.isEmpty()) {
DUIUtil.ToastText(context,
context.getString(R.string.tip_data_empty));
return false;
}
return true;
}
重构泛型

解决方案 »

  1.   

    public static <T extends Collection<?> & Map<?, ?>> boolean isNormalData(
      

  2.   


    试了一下不行啊,我用HashMap<String, String> data;会报错
    Bound mismatch: The generic method isNormalData(Context, T) of type TogetherHelper is not applicable for the arguments (Context, HashMap<String,String>). The inferred type 
     HashMap<String,String> is not a valid substitute for the bounded parameter <T extends Collection<?> & Map<?,?>>