分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 技术分享

008-TreeMap、Map和Bean互转、BeanUtils.copyProperties(A,B)拷贝、URL编码解码、字符串补齐

发布时间:2023-09-06 01:46责任编辑:胡小海关键词:暂无标签

1、TreeMap 有序Map

  无序有序转换

  使用默认构造方法:

public TreeMap(Map<? extends K, ? extends V> m)

2、Map和Bean互转

BeanUtils位于org.apache.commons.beanutils.BeanUtils下面,其方法populate的作用解释如下:
完整方法:
BeanUtils.populate( Object bean, Map properties ),

这个方法会遍历map<key, value>中的key,如果bean中有这个属性,就把这个key对应的value值赋给bean的属性。

2.1、bean 转换成 map

Person person1=new Person(); ?person1.setName("name1"); ?person1.setSex("sex1"); ?Map<String, String> map = BeanUtils.describe(person1); ?

2.2、map 转换成 bean

public static <T> T map2Bean(Map<String, String> map, Class<T> class1) { ?  T bean = null; ?  try { ?    bean = class1.newInstance(); ?    BeanUtils.populate(bean, map); ?  } catch (Exception e) { ?    e.printStackTrace(); ?  } ?  return bean; ?} ?

3、BeanUtils.copyProperties(A,B)拷贝

  3.1、package org.springframework.beans;中的

    BeanUtils.copyProperties(A,B);//A→B 是A中的值付给B

  3.2、package org.apache.commons.beanutils;(常用)

    BeanUtils.copyProperties(A,B);//B→A 是B中的值付给A

  3.3、package org.apache.commons.beanutils;(常用)

    PropertyUtils.copyProperties(A,B);//B→A 是B中的值付给A

  注意:PropertyUtils提供类型转换功能,即发现两个JavaBean的同名属性为不同类型时,在支持的数据类型范围内进行转换,而BeanUtils不支持这个功能,但是速度会更快一些.

4、URL编码解码

  编码:java.net.URLEncoder.encode(String s)

  解码:java.net.URLDecoder.decode(String s);    

5、字符串补齐

  org.apache.commons.lang.StringUtils  

String test ="123456";String value = StringUtils.leftPad(test, 10, "0");System.out.println(value);

  输出:0000123456

008-TreeMap、Map和Bean互转、BeanUtils.copyProperties(A,B)拷贝、URL编码解码、字符串补齐

原文地址:https://www.cnblogs.com/bjlhx/p/8624284.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved