* 将无序Map转换成有序Map
* @explain 将参数按照第一个字符的键值 ASCII 码递增排序(字母升序排序),如果遇到相同字符则按照第二个字符的键值 ASCII 码递增排序,
* 以此类推通过SortedMap可以实现
*
@param
paramsMap
*
@return
Map 按ASCII码升序排列
public
static
Map<String, Object> toSortedMap(Map<String, Object>
paramsMap) {
//
用于存储接收到的key:value,并按key以ASCII码进行升序排列组合
SortedMap<String, Object> data =
new
TreeMap<String, Object>
();
//
获取Map对象中的键值对对象
Set<Entry<String, Object>> entrySet =
paramsMap.entrySet();
Entry
<String, Object> entry =
null
;
String key
= ""
;
Object value
=
null
;
//
遍历paramsMap数据,添加到SortedMap对象
for
(Iterator<Entry<String, Object>> iterator =
entrySet.iterator(); iterator.hasNext();) {
entry
=
iterator.next();
key
=
entry.getKey();
value
=
entry.getValue();
data.put(key, value);
log.info(
"Map-->SortedMap:" +
data);
return
data;
3.将map的value转化成String类型
* Map<String, Object>-->Map<String, String>
* @explain 前提:value必须为String类型
*
@param
paramsMap
*
@return
*
@throws
Exception
public
static
Map<String, String> toStringMap(Map<String, Object> paramsMap)
throws
Exception{
Map
<String, String> data =
new
HashMap<String, String>
();
//
获取Map对象中的键值对对象
Set<Entry<String, Object>> entrySet =
paramsMap.entrySet();
Entry
<String, Object> entry =
null
;
String key
= ""
;
String value
=
null
;
for
(Iterator<Entry<String, Object>> iterator =
entrySet.iterator(); iterator.hasNext();) {
entry
=
iterator.next();
key
=
entry.getKey();
if
(key
instanceof
String) {
value
=
(String) entry.getValue();
data.put(key, value);
}
else
{
log.error(
"Map中含有非String类型的值,对应的key=" +
key);
throw
new
Exception("Map中含有非String类型的值,对应的key=" +
key);
log.info(
"Map<String, Object>-->Map<String, String>:" +
data);
return
data;
4.将map的value转化成Object类型
* Map<String, String>-->Map<String, Object>
* @explain 将value转换成Object类型
*
@param
paramsMap
*
@return
public
static
Map<String, Object> toObjectMap(Map<String, String>
paramsMap){
Map
<String, Object> data =
new
HashMap<String, Object>
();
//
获取Map对象中的键值对对象
Set<Entry<String, String>> entrySet =
paramsMap.entrySet();
Entry
<String, String> entry =
null
;
String key
= ""
;
Object value
=
null
;
for
(Iterator<Entry<String, String>> iterator =
entrySet.iterator(); iterator.hasNext();) {
entry
=
iterator.next();
key
=
entry.getKey();
value
=
entry.getValue();
data.put(key, value);
log.info(
"Map<String, String>-->Map<String, Object>:" +
data);
return
data;
5.将map的key小写转换成大写
* key 小写-->大写
* @explain 将key改为大写
*
@param
paramsMap
*
@return
public
static
Map<String, Object> toUpperMap(Map<String, Object>
paramsMap){
Map
<String, Object> data =
new
HashMap<String, Object>
();
//
获取Map对象中的键值对对象
Set<Entry<String, Object>> entrySet =
paramsMap.entrySet();
Entry
<String, Object> entry =
null
;
String key
= ""
;
Object value
=
null
;
for
(Iterator<Entry<String, Object>> iterator =
entrySet.iterator(); iterator.hasNext();) {
entry
=
iterator.next();
key
=
entry.getKey().toUpperCase();
value
=
entry.getValue();
data.put(key, value);
log.info(
"Map 小写-->大写:" +
data);
return
data;
相关推荐:
java Map
java 复制Map对象(深拷贝与浅拷贝)
java json与map互相转换(一)