 Java代码杂记
Java代码杂记
  # 1 类相同属性比较
public class Test {
  public static void main(String[]args)throws NoSuchFieldException {
    Map<String, String> map1 = new HashMap<>();
    Field[] fields = OmsOrderApiVO.class.getDeclaredFields();
    for (Field field : fields) {
      String fieldName = field.getName();
      ApiModelProperty annotation = field.getAnnotation(ApiModelProperty.class);
      String val = "-";
      if (annotation != null) {
        val = annotation.value();
      }
      map1.put(fieldName, val);
    }
    Map<String, String> map2 = new HashMap<>();
    Field[] fields2 = OmsWaybillApiDTO.class.getDeclaredFields();
    for (Field field2 : fields2) {
      String fieldName = field2.getName();
      ApiModelProperty annotation = field2.getAnnotation(ApiModelProperty.class);
      String val = "-";
      if (annotation != null) {
        val = annotation.value();
      }
      map2.put(fieldName, val);
    }
  }
}
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
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
# 2 驼峰字符串转下划线字符串
public class Test {
  public static void main(String[] args) throws NoSuchFieldException {
    Map<String, String> map1 = new HashMap<>();
    Field[] fields = SpmCashBillDetail.class.getDeclaredFields();
    for (Field field : fields) {
      String fieldName = field.getName();
      System.out.println(toUnderlineCase(fieldName).toUpperCase());
    }
  }
  public static String toUnderlineCase(String camelCaseStr) {
    if (camelCaseStr == null) {
      return null;
    }
    // 将驼峰字符串转换成数组
    char[] charArray = camelCaseStr.toCharArray();
    StringBuffer buffer = new StringBuffer();
    //处理字符串
    for (int i = 0, l = charArray.length; i < l; i++) {
      if (charArray[i] >= 65 && charArray[i] <= 90) {
        buffer.append("_").append(charArray[i] += 32);
      } else {
        buffer.append(charArray[i]);
      }
    }
    return buffer.toString();
  }
}
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
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
上次更新: 2022/06/02, 11:20:10
