项目常用的方法

项目常用的方法

1.Collections

//Collections.unmodifiableMap
//创建一个静态的HashMap
public static final HashMap<String, String> resMap = new HashMap<String, String>();
   static {
	Map<String,String> hashMap = new HashMap<String,String>() ;
		hashMap.put("1", "2");
		hashMap.put("2", "3");
		hashMap.put("3", "4");
		hashMap.put("", "");
		hashMap.put("", "");
		hashMap.put("", "");
//产生一个只读的Map
HashMap<String, String> resMap = Collections.unmodifiableMap(hashMap);
  }
//Collections.nCopies
//拷贝n个""。
  List<String> list = Collections.nCopies(3, "");

2.commons.lang.StringUtils处理字符串类

StringUtils.EMPTY
//源码
public static final String EMPTY="";

1. public static boolean isEmpty(String str) 
   判断某字符串是否为空,为空的标准是 str==null 或 str.length()==0

   StringUtils.isEmpty(null) = true
   StringUtils.isEmpty("") = true
   StringUtils.isEmpty(" ") = false //注意在 StringUtils 中空格作非空处理

2. public static boolean isNotEmpty(String str) 
   等于 !isEmpty(String str)
3. public static boolean isBlank(String str)
   判断某字符串是否为空或长度为0或由空白符(whitespace) 构成
      StringUtils.isBlank(null) = true
      StringUtils.isBlank("") = true
      StringUtils.isBlank(" ") = true
      StringUtils.isBlank("        ") = true
      StringUtils.isBlank("\t \n \f \r") = true   //对于制表符、换行符、换页符和回车符

      StringUtils.isBlank()   //均识为空白符
      StringUtils.isBlank("\b") = false   //"\b"为单词边界符
      StringUtils.isBlank("bob") = false
 4. public static boolean isNotBlank(String str) 
    等于 !isBlank(String str)
 5.public static boolean equals(String str1, String str2)
    比较两个字符串是否相等,如果两个均为空则也认为相等。
 6. public static boolean equalsIgnoreCase(String str1, String str2)
    比较两个字符串是否相等,不区分大小写,如果两个均为空则也认为相等。

3.BeanUtils.copyProperties

1.开发中经常遇到,把父类的属性拷贝到子类中。通常有2种方法:
1.1、一个一个set
1.2、用BeanUtils.copyProperties
很显然BeanUtils更加方便,也美观很多。

2.BeanUtils浅拷贝

浅拷贝: 只是调用子对象的set方法,并没有将所有属性拷贝。(也就是说,引用的一个内存地址)
深拷贝: 将子对象的属性也拷贝过去。

有子对象,但是子对象并不怎么改动,那么用BeanUtils

3.常见的BeanUtils有2个:
spring有BeanUtils
apache的commons也有BeanUtils。

spring的BeanUtilscommons的BeanUtils
方法copyProperty和copyPropertiescopyProperties
参数src ,destdest,src

他们2个的src和dest是正好相反的

4使用

父类中没有的属性,子类是不会自动赋值的。需要手动一个一个set进去

package org.apache.commons.beanutils;(常用)
BeanUtils.copyProperties(A,B);
是B中的值赋给A

package org.springframework.beans;中的
BeanUtils.copyProperties(A,B);
是A中的值赋给B

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值