數組可以通過索引快速訪問和操作元素,在許多場景下仍然是非常有用的,但如果需要動態調整大小或保存不同類型的元素,則可以考慮使用集合類來代替數組。集合類還提供了一系列增加、刪除、修改和查找元素的方法。集合框架中還提供了多種優化和封裝好的實現類,通過使用合適的集合類可以更高效地組織和操作數據。 ...
如:Person對象為:{name:"浩二", age:24, weight:null, height:114},那返回給前端的就為{name:"浩二", age:24, height:114}。
如果這個時候有個需求:
Integer類型的欄位為null給-1
Long類型的欄位為null給-1L
String類型的欄位為null給""(空字元串)
對象類型的欄位為null給new對象,
不能直接動框架,因此需要手動轉化。
代碼寫死轉換
示例
轉換對象 ConvertNullFiledInfo:
@Data public class ConvertNullFiledInfo { private Integer intNum; private Long longNum; private String str; private ConvertNullFiledInfo info; }
轉換代碼:
@Service public class ConvertNullFiledService { /** * 寫死代碼的方式一個一個欄位來轉換 * @param info * @return */ public ConvertNullFiledInfo convertFunction01(ConvertNullFiledInfo info) { info.setIntNum(info.getIntNum() == null ? -1 : info.getIntNum()); info.setLongNum(info.getLongNum() == null ? -1L : info.getLongNum()); info.setStr(info.getStr() == null ? "" : info.getStr()); info.setInfo(info.getInfo() == null ? new ConvertNullFiledInfo() : info.getInfo()); return info; } }
測試代碼:
import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; @SpringBootTest @RunWith(SpringRunner.class) public class ConvertNullFiledServiceTest { @Autowired private ConvertNullFiledService convertNullFiledService; @Test public void convertFunction01() { // str和info欄位給null ConvertNullFiledInfo info = createConvertNullFiledInfo(1, 1L, null, null); ConvertNullFiledInfo result = convertNullFiledService.convertFunction01(info); System.out.println(result); } /** * 自定義欄位創建ConvertNullFiledInfo * @param intNum * @param longNum * @param str * @param info * @return */ private ConvertNullFiledInfo createConvertNullFiledInfo(Integer intNum, Long longNum, String str, ConvertNullFiledInfo info) { ConvertNullFiledInfo result = new ConvertNullFiledInfo(); result.setIntNum(intNum); result.setLongNum(longNum); result.setStr(str); result.setInfo(info); return result; } }
運行結果:
可以看到是正常轉換了的
問題點
這樣寫雖然簡單方便,但是如果有非常多的對象需要轉換,就會有許多重覆代碼;
而且如果欄位有修改(類型、名稱、被刪除、新增),就需要在去轉換方法中修改,因此可以用更好的方式。
所有對象都有Class<T>類,而Class有getDeclaredFields()方法,能獲取到所有欄位(filed),
因此可以使用這種方式來轉換。
示例
轉換代碼:
/** * 遍歷field的方式一個一個欄位來轉換 * @param info * @return */ public ConvertNullFiledInfo convertByField(ConvertNullFiledInfo info) { try { Field[] fields = info.getClass().getDeclaredFields(); for (Field field : fields) { // 設置可訪問私有變數 field.setAccessible(true); // 獲取當前欄位值 Object value = field.get(info); // value不為空就跳過 if (value != null) { continue; } // 獲取當前欄位類型 Class<?> type = field.getType(); if (type == Integer.class) { // Integer類型就設置為-1 field.set(info, -1); } else if (type == Long.class) { // Long類型就設置為-1L field.set(info, -1L); } else if (type == String.class) { // String類型就設置為“” field.set(info, ""); } else if (type == ConvertNullFiledInfo.class) { // ConvertNullFiledInfo類型就設置為新對象 field.set(info, new ConvertNullFiledInfo()); } } } catch (Exception e) { e.printStackTrace(); } return info; }
測試代碼:
@Test public void convertByField() { // str和info欄位給null ConvertNullFiledInfo info = createConvertNullFiledInfo(1, 1L, null, null); ConvertNullFiledInfo result = convertNullFiledService.convertByField(info); System.out.println(result); }
運行結果:
可以看到也是成功轉換了
問題點
這種寫法仍然存在問題,可以看到方法的傳參和返回值都是固定類型為ConvertNullFiledInfo,
並且在遍歷field的時候,也有if判斷是寫定的ConvertNullFiledInfo,
因此也在一定程度上寫死了代碼
優化
為了避免寫死的情況,可以使用泛型來寫
轉換代碼:
@Service public class ConvertNullFiledService<T> { /** * 使用泛型,遍歷field的方式一個一個欄位來轉換 * @param object * @return */ public T convertByFieldGeneric(T object) { try { Field[] fields = object.getClass().getDeclaredFields(); for (Field field : fields) { // 設置可訪問私有變數 field.setAccessible(true); // 獲取當前欄位值 Object value = field.get(object); // value不為空就跳過 if (value != null) { continue; } // 獲取當前欄位類型 Class<?> type = field.getType(); if (type == Integer.class) { // Integer類型就設置為-1 field.set(object, -1); } else if (type == Long.class) { // Long類型就設置為-1L field.set(object, -1L); } else if (type == String.class) { // String類型就設置為“” field.set(object, ""); } else if (type == object.getClass()) { // T類型就設置為新對象 Object newObj = object.getClass().newInstance(); field.set(object, newObj); } } } catch (Exception e) { e.printStackTrace(); } return object; } }
測試代碼:
@Test public void convertByFieldGeneric() { // 全部欄位給null ConvertNullFiledInfo info = createConvertNullFiledInfo(null, null, null, null); ConvertNullFiledInfo result = (ConvertNullFiledInfo) convertNullFiledService.convertByFieldGeneric(info); System.out.println(result); }
運行結果:
成功轉換