要么庸俗,要么孤独——叔本华

前两天遇到一个坑,当时我通过使用getDeclaredFields()函数获取对象属性时发现一个问题:

获取到的属性的顺序不对,结果我自己一看介绍

image-20200912174129090

原来,它是无序的

所以我们为了解决这个问题

首先自定义一个注解用于制定排序规则

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
package com.ruben.annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* @ClassName: BeanFieldSort
* @Description:
* @Date: 2020/9/11 22:18
* *
* @author: achao<achao1441470436 @ gmail.com>
* @version: 1.0
* @since: JDK 1.8
*/
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface BeanFieldSort {
/**
* 序号
*
* @return
*/
int order();
}

然后在需要排序的bean上加上这个注解

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
package com.ruben.pojo;

import com.ruben.annotation.BeanFieldSort;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
* @ClassName: UserInfo
* @Description:
* @Date: 2020/8/15 20:05
* *
* @author: achao<achao1441470436 @ gmail.com>
* @version: 1.0
* @since: JDK 1.8
*/
@Data
@NoArgsConstructor
@AllArgsConstructor
public class UserInfo {
@BeanFieldSort(order = 5)
private String phoneNumber;
@BeanFieldSort(order = 4)
private String address;
@BeanFieldSort(order = 3)
private String qqNumber;
@BeanFieldSort(order = 2)
private String wxNumber;
@BeanFieldSort(order = 1)
private String weiboNumber;
}

最后是排序的方法,这里使用java8的stream

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
package com.ruben;

import com.ruben.annotation.BeanFieldSort;
import com.ruben.pojo.UserInfo;

import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
* @ClassName: FieldDemo
* @Description:
* @Date: 2020/9/11 22:14
* *
* @author: achao<achao1441470436 @ gmail.com>
* @version: 1.0
* @since: JDK 1.8
*/
public class FieldDemo {
public static void main(String[] args) throws IllegalAccessException, InstantiationException {
//获取对象
Class<UserInfo> userInfoClass = UserInfo.class;
//创建对象
UserInfo userInfo = userInfoClass.newInstance();
System.out.println(userInfo);
//获取bean中所有字段
Field[] fields = userInfoClass.getDeclaredFields();
//遍历
for (Field field : fields) {
//把private属性设为可修改
field.setAccessible(true);
field.set(userInfo, "yeah!");
System.out.println(field.getName());
}
System.out.println("------------");
//排序
List<Field> fieldList = Arrays
.stream(fields)
.sorted(Comparator.comparingInt(f -> f
.getAnnotation(BeanFieldSort.class).order()))
.collect(Collectors.toList());
//遍历输出结果
fieldList.stream().map(Field::getName).forEach(System.out::println);
System.out.println(userInfo);
}
}

输出结果为

image-20200912175049769

可以看到排序前和排序后的结果

关键就是这一句

1
List<Field> fieldList = Arrays.stream(fields).sorted(Comparator.comparingInt(f -> f.getAnnotation(BeanFieldSort.class).order())).collect(Collectors.toList());

sorted()函数中传入排序规则

就是这样啦,希望对大家有所帮助