Java list 转字符串并加入分隔符的方法

1
2
3
4
5
6
7
8
import org.apache.commons.lang.StringUtils;

List<String> list=new ArrayList<String>();
list.add("first");
list.add("second");
list.add("third");

StringUtils.join(list.toArray(), ",")

比较两个对象的差异值

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/**
* @Auther: chenweitao
* @Date: 2018/11/22 09:59
* @Description: 比较两个对象的差异值
*/
public class CompareFieldsUtil {


/**
* 比较两个实体属性值,返回一个map以有差异的属性名为key,value为一个list分别存obj1,obj2此属性名的值
*
* @param obj1 进行属性比较的对象1
* @param obj2 进行属性比较的对象2
* @param ignoreArr 选择忽略比较的属性数组
* @return 属性差异比较结果map
*/
@SuppressWarnings("rawtypes")
public static Map<String, List<Object>> compareFields(Object obj1, Object obj2, String[] ignoreArr) {
try {
Map<String, List<Object>> map = new HashMap<String, List<Object>>();
List<String> ignoreList = null;
if (ignoreArr != null && ignoreArr.length > 0) {
// array转化为list
ignoreList = Arrays.asList(ignoreArr);
}
if (obj1.getClass() == obj2.getClass()) {// 只有两个对象都是同一类型的才有可比性
Class clazz = obj1.getClass();
// 获取object的属性描述
PropertyDescriptor[] pds = Introspector.getBeanInfo(clazz,
Object.class).getPropertyDescriptors();
for (PropertyDescriptor pd : pds) {// 这里就是所有的属性了,参数
String name = pd.getName();// 属性名
Method readMethod = pd.getReadMethod();// get方法

if (ignoreList != null && ignoreList.contains(name)) {// 如果当前属性选择忽略比较,跳到下一次循环
continue;
}

// 在obj1上调用get方法等同于获得obj1的属性值
Object o1 = readMethod.invoke(obj1);
// 在obj2上调用get方法等同于获得obj2的属性值
Object o2 = readMethod.invoke(obj2);
if (o1 instanceof Timestamp) {
o1 = new Date(((Timestamp) o1).getTime());
}
if (o2 instanceof Timestamp) {
o2 = new Date(((Timestamp) o2).getTime());
}
// if (o1 == null && o2 == null) { //两方都为null时跳过记录
// continue;
// }
if (o1 == null) { // 第一个对象的属性为null时跳过记录
continue;
} else if (o1 == null && o2 != null) {
List<Object> list = new ArrayList<Object>();
list.add(o1);
list.add(o2);
map.put(name, list);
continue;
}
if (!o1.equals(o2)) {// 比较这两个值是否相等,不等就可以放入map了
List<Object> list = new ArrayList<Object>();
list.add(o1);
list.add(o2);
map.put(name, list);
}
}
}
return map;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}


}

对象复制工具

1. BeanUtils.copyProperties(Object source, Object target)

用法: 讲source的属性值复制到target,属性为null时也会进行复制。

需求:排除null值进行复制
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
public class CopyObjectUtil {


public static String[] getNullPropertyNames(Object source) {
final BeanWrapper src = new BeanWrapperImpl(source);
java.beans.PropertyDescriptor[] pds = src.getPropertyDescriptors();

Set<String> emptyNames = new HashSet<String>();
for (java.beans.PropertyDescriptor pd : pds) {
Object srcValue = src.getPropertyValue(pd.getName());
if (srcValue == null) emptyNames.add(pd.getName());
}
String[] result = new String[emptyNames.size()];
return emptyNames.toArray(result);
}

public static void copyPropertiesIgnoreNull(Object src, Object target) {
BeanUtils.copyProperties(src, target, getNullPropertyNames(src));
}


}

使用方式与BeanUtils.copyProperties相同:

1
CopyObjectUtil.copyPropertiesIgnoreNull(Object source, Object target);

排除指定字段进行复制

BeanUtils.copyProperties(Object source, Object target, new String[] { "id", "createDate", "modifyDate" })

JAVA判断各种类型数据是否为空

  1. 判断list是否为空(Map、Set同list)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
if(list != null && list.size() == 0){
}

if(list != null && !list.isEmpty()){
}

list!=null:判断是否存在list,null表示这个list不指向任何的东西,如果这时候你调用它的方法,那么就会出现空指针异常。

list.isEmpty():判断list里是否有元素存在

list.size():判断list里有几个元素


所以判断list里是否有元素的最佳的方法是:

if(list != null && !list.isEmpty()){
//list存在且里面有元素
}
  1. 判断String类型数据是否为空
1
2
3
4
5
6
直接用if( s.equals("")),if( !s.isEmpty()),if(s.length()>0)来判断:忽略了s为null的情况,s指向不确定的对象,无法调用一个确定的Sting对象的方法

(1)str == null;
(2)"".equals(str);
(3)str.length <= 0;
(4)str.isEmpty();
  1. 判断date类型数据是否为空
1
2
3
4
5
6
Date date=…… //实例化
if(date==null){
System.out.println("date为空");
}else{
System.out.println("date不为空");
}