‘liusuyi’
2023-06-15 4aa86f1386d20c9ad811e2d18b7e56f00b06e25c
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
package com.ruoyi.utils.tools;
 
import java.lang.reflect.Field;
import java.util.*;
 
/**
 * @Description: 工具方法
 * @ClassName: ArdTool
 * @Author: 刘苏义
 * @Date: 2023年06月02日14:37
 * @Version: 1.0
 **/
public class ArdTool {
 
    /**
     * @描述 实体对象转Map
     * @参数 [entity]
     * @返回值 java.util.Map<java.lang.String,java.lang.Object>
     * @创建人 刘苏义
     * @创建时间 2023/6/2 14:35
     * @修改人和其它信息
     */
    public static Map<String, Object> convertEntityToMap(Object entity) {
        Map<String, Object> map = new HashMap<>();
        Field[] fields = entity.getClass().getDeclaredFields();
        for (Field field : fields) {
            field.setAccessible(true);
            try {
                Object value = field.get(entity);
                map.put(field.getName(), value);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
        return map;
    }
    /**
     * 求Map<K,V>中最小 Value 对应的Key值
     *
     * @param map
     * @return
     */
    public static String getKeyByMinValue(Map<String, Double> map) {
        if (map == null) return null;
        List<Map.Entry<String, Double>> list = new ArrayList(map.entrySet());
        Collections.sort(list, (o1, o2) -> (o1.getValue().intValue() - o2.getValue().intValue()));
        String min = list.get(0).getKey();
//        String max = list.get(list.size() - 1).getKey();
        return min;
    }
}