GVKun编程网logo

返回默认值而不是null的Java Map实现(返回值默认类型)

25

本文的目的是介绍返回默认值而不是null的JavaMap实现的详细情况,特别关注返回值默认类型的相关信息。我们将通过专业的研究、有关数据的分析等多种方式,为您呈现一个全面的了解返回默认值而不是null

本文的目的是介绍返回默认值而不是null的Java Map实现的详细情况,特别关注返回值默认类型的相关信息。我们将通过专业的研究、有关数据的分析等多种方式,为您呈现一个全面的了解返回默认值而不是null的Java Map实现的机会,同时也不会遗漏关于API接口返回值通过反射设置null的默认值、asp.net – 模型绑定 – Nullable decimal和double默认为0而不是null?、asp.net-mvc – 如何设置TextBox的空字符串而不是null的默认值、c# – default(T)与空集合而不是null的知识。

本文目录一览:

返回默认值而不是null的Java Map实现(返回值默认类型)

返回默认值而不是null的Java Map实现(返回值默认类型)

Map<String, List<String>>我的代码中有一个,如果地图的#get()方法返回的是空列表而不是空值,则应避免使用空指针。java
API中是否有类似的东西?我应该延长HashMap吗?

答案1

小编典典

@Jon的答案是直接处理您所要询问的好方法。

但是令我惊讶的是,您可能要实现的是“多图”;即从键到值集合的映射。如果是这种情况,那么您还应该查看Guava或Apache
commons集合中的multimap类。

看着:

  • com.google.common.collect.Multimap界面和它的实施方式中,或
  • org.apache.commons.collections.MultiMap界面和它的实现。
  • org.apache.commons.collections4.MultiMap界面和它的实施方式中(先前multimap中的一个新版本;在V4中引入)。

API接口返回值通过反射设置null的默认值

API接口返回值通过反射设置null的默认值

##背景 一个费用的类,里面有各种费用,测试的时候数据不完整导致很多字段都是null。开始的时候返回给客户端的字段自动把null去掉了,但这样导致客户端接收的model不完整,自己添加字段的逻辑有点复杂。因此还是同意服务器给null设置默认值。

string类型的默认值通过jackson来设置

string要返回空字符串而不是null。因为使用springmvc,默认使用jackson来做json转化。那么可以采用自定义jackson的方式来设置null为"".

/**
 * Copyright &copy; 2012-2014 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */

import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.TimeZone;

import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.JsonParser.Feature;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.util.JSONPObject;
import com.fasterxml.jackson.module.jaxb.JaxbAnnotationModule;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

/**
 * 简单封装Jackson,实现JSON String<->Java Object的Mapper.
 * 封装不同的输出风格, 使用不同的builder函数创建实例.
 * @author ThinkGem
 * @version 2013-11-15
 */
public class JsonMapper extends ObjectMapper {

	private static final long serialVersionUID = 1L;

	private static Logger logger = LoggerFactory.getLogger(JsonMapper.class);

	private static JsonMapper mapper;

	public JsonMapper() {
		this(Include.NON_EMPTY);
	}

	public JsonMapper(Include include) {
		// 设置输出时包含属性的风格
		if (include != null) {
			this.setSerializationInclusion(include);
		}
		// 允许单引号、允许不带引号的字段名称
		this.enableSimple();
		// 设置输入时忽略在JSON字符串中存在但Java对象实际没有的属性
		this.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
        // 空值处理为空串
		this.getSerializerProvider().setNullValueSerializer(new JsonSerializer<Object>(){
			@Override
			public void serialize(Object value, JsonGenerator jgen,
					SerializerProvider provider) throws IOException,
					JsonProcessingException {
				jgen.writeString("");
			}
        });
		// 进行HTML解码。
		this.registerModule(new SimpleModule().addSerializer(String.class, new JsonSerializer<String>(){
			@Override
			public void serialize(String value, JsonGenerator jgen,
					SerializerProvider provider) throws IOException,
					JsonProcessingException {
				jgen.writeString(StringEscapeUtils.unescapeHtml4(value));
			}
        }));
		// 设置时区
		this.setTimeZone(TimeZone.getDefault());//getTimeZone("GMT+8:00")
	}

	/**
	 * 创建只输出非Null且非Empty(如List.isEmpty)的属性到Json字符串的Mapper,建议在外部接口中使用.
	 */
	public static JsonMapper getInstance() {
		if (mapper == null){
			mapper = new JsonMapper().enableSimple();
		}
		return mapper;
	}

	/**
	 * 创建只输出初始值被改变的属性到Json字符串的Mapper, 最节约的存储方式,建议在内部接口中使用。
	 */
	public static JsonMapper nonDefaultMapper() {
		if (mapper == null){
			mapper = new JsonMapper(Include.NON_DEFAULT);
		}
		return mapper;
	}
	
	/**
	 * Object可以是POJO,也可以是Collection或数组。
	 * 如果对象为Null, 返回"null".
	 * 如果集合为空集合, 返回"[]".
	 */
	public String toJson(Object object) {
		try {
			return this.writeValueAsString(object);
		} catch (IOException e) {
			logger.warn("write to json string error:" + object, e);
			return null;
		}
	}

	/**
	 * 反序列化POJO或简单Collection如List<String>.
	 * 
	 * 如果JSON字符串为Null或"null"字符串, 返回Null.
	 * 如果JSON字符串为"[]", 返回空集合.
	 * 
	 * 如需反序列化复杂Collection如List<MyBean>, 请使用fromJson(String,JavaType)
	 * @see #fromJson(String, JavaType)
	 */
	public <T> T fromJson(String jsonString, Class<T> clazz) {
		if (StringUtils.isEmpty(jsonString)) {
			return null;
		}
		try {
			return this.readValue(jsonString, clazz);
		} catch (IOException e) {
			logger.warn("parse json string error:" + jsonString, e);
			return null;
		}
	}

	/**
	 * 反序列化复杂Collection如List<Bean>, 先使用函數createCollectionType构造类型,然后调用本函数.
	 * @see #createCollectionType(Class, Class...)
	 */
	@SuppressWarnings("unchecked")
	public <T> T fromJson(String jsonString, JavaType javaType) {
		if (StringUtils.isEmpty(jsonString)) {
			return null;
		}
		try {
			return (T) this.readValue(jsonString, javaType);
		} catch (IOException e) {
			logger.warn("parse json string error:" + jsonString, e);
			return null;
		}
	}

	/**
	 * 構造泛型的Collection Type如:
	 * ArrayList<MyBean>, 则调用constructCollectionType(ArrayList.class,MyBean.class)
	 * HashMap<String,MyBean>, 则调用(HashMap.class,String.class, MyBean.class)
	 */
	public JavaType createCollectionType(Class<?> collectionClass, Class<?>... elementClasses) {
		return this.getTypeFactory().constructParametricType(collectionClass, elementClasses);
	}

	/**
	 * 當JSON裡只含有Bean的部分屬性時,更新一個已存在Bean,只覆蓋該部分的屬性.
	 */
	@SuppressWarnings("unchecked")
	public <T> T update(String jsonString, T object) {
		try {
			return (T) this.readerForUpdating(object).readValue(jsonString);
		} catch (JsonProcessingException e) {
			logger.warn("update json string:" + jsonString + " to object:" + object + " error.", e);
		} catch (IOException e) {
			logger.warn("update json string:" + jsonString + " to object:" + object + " error.", e);
		}
		return null;
	}

	/**
	 * 輸出JSONP格式數據.
	 */
	public String toJsonP(String functionName, Object object) {
		return toJson(new JSONPObject(functionName, object));
	}

	/**
	 * 設定是否使用Enum的toString函數來讀寫Enum,
	 * 為False時時使用Enum的name()函數來讀寫Enum, 默認為False.
	 * 注意本函數一定要在Mapper創建後, 所有的讀寫動作之前調用.
	 */
	public JsonMapper enableEnumUseToString() {
		this.enable(SerializationFeature.WRITE_ENUMS_USING_TO_STRING);
		this.enable(DeserializationFeature.READ_ENUMS_USING_TO_STRING);
		return this;
	}

	/**
	 * 支持使用Jaxb的Annotation,使得POJO上的annotation不用与Jackson耦合。
	 * 默认会先查找jaxb的annotation,如果找不到再找jackson的。
	 */
	public JsonMapper enableJaxbAnnotation() {
		JaxbAnnotationModule module = new JaxbAnnotationModule();
		this.registerModule(module);
		return this;
	}

	/**
	 * 允许单引号
	 * 允许不带引号的字段名称
	 */
	public JsonMapper enableSimple() {
		this.configure(Feature.ALLOW_SINGLE_QUOTES, true);
		this.configure(Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
		return this;
	}
	
	/**
	 * 取出Mapper做进一步的设置或使用其他序列化API.
	 */
	public ObjectMapper getMapper() {
		return this;
	}

	/**
	 * 对象转换为JSON字符串
	 * @param object
	 * @return
	 */
	public static String toJsonString(Object object){
		return JsonMapper.getInstance().toJson(object);
	}
	
	/**
	 * JSON字符串转换为对象
	 * @param jsonString
	 * @param clazz
	 * @return
	 */
	public static Object fromJsonString(String jsonString, Class<?> clazz){
		return JsonMapper.getInstance().fromJson(jsonString, clazz);
	}
	
	/**
	 * 测试
	 */
	public static void main(String[] args) {
		List<Map<String, Object>> list = Lists.newArrayList();
		Map<String, Object> map = Maps.newHashMap();
		map.put("id", 1);
		map.put("pId", -1);
		map.put("name", "根节点");
		list.add(map);
		map = Maps.newHashMap();
		map.put("id", 2);
		map.put("pId", 1);
		map.put("name", "你好");
		map.put("open", true);
		list.add(map);
		String json = JsonMapper.getInstance().toJson(list);
		System.out.println(json);
	}
	
}

自定义类型的默认值需要自己设定

除了String以外的类型就不好统一设定了,毕竟各自的format不同。所以采用数据传输对象(DTO)(Data Transfer Object)来包装响应。那么,在包装的时候处理下null就好了。

当前的问题是,一个费用的类型的字段太多,比如double的费用由十几个,那么if-else会写吐的,因此采用反射来设置double的默认为0。

        Class<FeeVo> clazz = FeeVo.class;
        Field[] declaredFields = clazz.getDeclaredFields();
        for (Field field : declaredFields) {
            Class<?> type = field.getType();
            String name = field.getName();
            if (Double.class.equals(type)){//double类型设置默认为0
                try {
                    PropertyDescriptor propertyDescriptor = new PropertyDescriptor(name, clazz);
                    Method readMethod = propertyDescriptor.getReadMethod();
                    Object value = readMethod.invoke(this);
                    if (value==null){
                        Reflections.setFieldValue(this, name,0d);
                        LOGGER.debug("设置double字段[{}]为0",name);
                    }
                } catch (IntrospectionException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (InvocationTargetException e) {
                    e.printStackTrace();
                }
            }

        }

参考:

  • http://www.cnblogs.com/qqzy168/p/3798239.html

asp.net – 模型绑定 – Nullable decimal和double默认为0而不是null?

asp.net – 模型绑定 – Nullable decimal和double默认为0而不是null?

当我的表单发布到我的控制器操作,并且表单绑定到我的模型时,可空双精度和小数的空白表单值默认为0,但可为空的整数的空白表单值默认为null.这是正确的吗?

我的模型有几个可以为空的字段的可空类型.它有可空的整数,双精度和小数.然后我有一个常规表单,其中包含每个属性的文本框,并且它们都默认为空(空字符串).当此表单以所有空表单值发回到我的控制器操作,并且它绑定到我的模型时,空的双精度数和小数数显示为0,空的整数为空.

在我看来,当传入一个空的表单值时,所有可空类型都应默认为null,但即使不是这种情况,看起来非常不一致,双精度和小数的处理方式与整数不同.

解决方法

这个问题似乎在MVC4中得到了解决,因为我无法重现这个问题.我的空文本框都将null绑定到我的模型的可空int,double或decimal.没有问题.所以问题可能在其他地方,或者这可能是MVC3中的一个错误,不再是.

也就是说,如果您仍然遇到问题并且无法使用MVC4,请尝试制作自己的自定义模型绑定器,以完成您需要做的事情.这是十进制的示例:

public class NullableDecimalBinder : IModelBinder {

    public object BindModel(ControllerContext controllerContext,ModelBindingContext bindingContext) {
        ValueProviderResult valueResult = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
        ModelState modelState = new ModelState { Value = valueResult };
        object result = null;

        if (valueResult.AttemptedValue.Length > 0) {
            try {
                // Bonus points: This will bind using the user's current culture.
                result = Convert.ToDecimal(valueResult.AttemptedValue,System.Globalization.CultureInfo.CurrentCulture);
            } catch (FormatException e) {
                modelState.Errors.Add(e);
            } catch (InvalidOperationException e) {
                modelState.Errors.Add(e);
            }
        }

        bindingContext.ModelState.Add(bindingContext.ModelName,modelState);
        return result;
    }
}

然后,要使用它,请将此行添加到Global.asax的Application_Start中:

ModelBinders.Binders.Add(typeof(decimal?),new NullableDecimalBinder());

asp.net-mvc – 如何设置TextBox的空字符串而不是null的默认值

asp.net-mvc – 如何设置TextBox的空字符串而不是null的默认值

我可能已经过时了,但我坚持的一个原则是尽可能避免空白。

但是,我发现,对于用户输入要保存的对象的属性的强类型视图,如果未输入某些字段,则将其分配为空。

然后当您尝试保存更改时,验证失败。

所以不是将每个属性设置为一个空字符串,如何自动将窗体上的每个TextBox设置为默认值为空字符串而不是空值?

解决方法

您可以在模型中的字符串属性上放置以下属性:
[displayFormat(ConvertEmptyStringToNull=false)]

所以每当有人发表一个空文本字段的表单,这些将是一个空字符串,而不是null …

c# – default(T)与空集合而不是null

c# – default(T)与空集合而不是null

我想要通用的方法返回传递类型的默认值,但是对于集合类型,我想获取空集合而不是null,例如:
GetDefault<int[]>(); // returns empty array of int's
GetDefault<int>(); // returns 0
GetDefault<object>(); // returns null
GetDefault<IList<object>>(); // returns empty list of objects

我开始写的方法如下:

public static T GetDefault<T>()
{
   var type = typeof(T);
   if(type.GetInterface("IEnumerable") != null))
   {
      //return empty collection
   }
   return default(T);   
}

如何完成?

编辑:
如果任何人想要获取某些类型的默认值,则基于类型实例而不是类型标识符,可以使用以下结构,即:

typeof(int[]).GetDefault();

内部实现是基于@ 280Z28的答案:

public static class TypeExtensions
{
    public static object GetDefault(this Type t)
    {
        var type = typeof(Default<>).MakeGenericType(t);
        var property = type.GetProperty("Value",BindingFlags.Static | BindingFlags.Public);
        var getaccessor = property.Getgetmethod();
        return getaccessor.Invoke(null,null);
    }
}

解决方法

您可以使用静态构造函数的魔术来高效地执行此操作.要使用代码中的默认值,只需使用Default< T> .Value.在应用期间,该值仅对任何给定类型T一次进行评估.
public static class Default<T>
{
    private static readonly T _value;

    static Default()
    {
        if (typeof(T).IsArray)
        {
            if (typeof(T).GetArrayRank() > 1)
                _value = (T)(object)Array.CreateInstance(typeof(T).GetElementType(),new int[typeof(T).GetArrayRank()]);
            else
                _value = (T)(object)Array.CreateInstance(typeof(T).GetElementType(),0);
            return;
        }

        if (typeof(T) == typeof(string))
        {
            // string is IEnumerable<char>,but don't want to treat it like a collection
            _value = default(T);
            return;
        }

        if (typeof(IEnumerable).IsAssignableFrom(typeof(T)))
        {
            // check if an empty array is an instance of T
            if (typeof(T).IsAssignableFrom(typeof(object[])))
            {
                _value = (T)(object)new object[0];
                return;
            }

            if (typeof(T).IsGenericType && typeof(T).GetGenericArguments().Length == 1)
            {
                Type elementType = typeof(T).GetGenericArguments()[0];
                if (typeof(T).IsAssignableFrom(elementType.MakeArrayType()))
                {
                    _value = (T)(object)Array.CreateInstance(elementType,0);
                    return;
                }
            }

            throw new NotImplementedException("No default value is implemented for type " + typeof(T).FullName);
        }

        _value = default(T);
    }

    public static T Value
    {
        get
        {
            return _value;
        }
    }
}

我们今天的关于返回默认值而不是null的Java Map实现返回值默认类型的分享已经告一段落,感谢您的关注,如果您想了解更多关于API接口返回值通过反射设置null的默认值、asp.net – 模型绑定 – Nullable decimal和double默认为0而不是null?、asp.net-mvc – 如何设置TextBox的空字符串而不是null的默认值、c# – default(T)与空集合而不是null的相关信息,请在本站查询。

本文标签: