6. 比較方式 && 常用套件Utils


透過上一篇我們知道字串比較的差別
還有物件比較的這一個概念
那現在和大家說明一下一個例子後我再來介紹一些比較方式

String type;

for(Stiring item:List<String>items){

if(item=="1"){
type = "test";
...
    }

    }

type.length();

如果是剛踏入軟體工程師這個領域可能會發生以上兩個問題
1.我想信看過上一篇的大家應該知道item=="1" 一定是false
2.type.length() 開發編輯器(IDE)應該會提示有問題 因為你沒有初始化

然後如果照著提示行為 快速建立通常會幫你補上

String type = null;

...

type.length();

有時候我們不能太過相信電腦幫你判斷的
這樣執行可是會噴錯的 nullException
以上操作可以套用到許多不同型別中,例如:List,Set,Dto...

我們回歸到比較這一部分
字串比較如果使用原生的方法來講

boolean compareStr(String param){
    retruen "test".equals(param);
    }

// 不會寫 param.equals("test");

從我們一開始和大家說明物件初始化和物件為null這件事來講
這個方法參數為String型別但null也是可以當作參數的,因此有可能發生nullException

那我這邊提供給大家我比較常用的的一些「比較」方式
StringUtils.equalsIgnoreCase() 忽略大小寫比較
NumberUtils.compare() ==0 相等
BooleanUtils.compare() ==0 相等 or Boolean.True.equal() // Boolean.True.toString 會是"true"

列出一些我個人常用的方法
StringUtils.isBlank()
StringUtils.isNoneBlank()
StringUtils.split()
StringUtils.substring()
StringUtils.length()
StringUtils.EMPTY

ExceptionUtils.getRootCause() // 有時候錯誤訊息一直堆疊可以透過這個直接找根源

(JUC)
「執行序 - 延遲」
ThreadUtils.sleep(Duration.ofSeconds(1));
ThreadUtils.sleep(DurationUtils.toDuration(10, TimeUnit.SECONDS));

(jackson)
「精準度」
DecimalUtils.toBigDecimal() // 回傳 BigDecimal
DecimalUtils.toDecimal() // 回傳 String

ObjectUtils.allNotNull() // 這邊大家不要誤會是丟一個自定義型別 內多欄位的欄位是不是null
// 主要是每個丟進去讀是不是null ex: ObjectUtils.allNotNull("1","1",null) 這樣會false

Objects.nonNull() // 單獨判斷null

和Collection相關操作需要另外注入

<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-collections4 -->
<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-collections4</artifactId>
    <version>4.4</version>
</dependency>

Collections.emptyList(); 空List
CollectionUtils.size()
CollectionUtils.isEmpty()
CollectionUtils.isNotEmpty()

ListUtils.partition() // 根據總數 根據大小分批
SystemUtils.getEnvironmentVariable() // 取得系統參數

「亂數」
Random random = new SecureRandom();
int number = random.nextInt();
// 以上方式比 SecureRandom.getInstanceStrong() 來的更好因為此方式易造成Thread block

// 底層有 new Random()
number = RandomUtils.nextInt();

// 使用Thread 推薦使用
number = ThreadLocalRandom.current().nextInt();

「取得檔案路徑」
1.PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
File file = resolver.getResources("classpath:")[0].getFile(); // // path from resource, but startwith classpath: current file 讀取原檔案

2.file = FileUtil.getFromResource(""); // path from resource new file 會在暫存區建立檔案

3.file = ResourceUtils.getFile("classpath:"); // path from resource, but startwith classpath: current file 讀取原檔案

「列舉值」
EnumUtils.getEnum()
EnumUtils.getEnumMap(Enum.class) 取得所有的Enum資訊並以MAP回傳對應值

「序列化 -> byte[]」
Dto dto = new Dto()
dto.setName("test") // 假設有一欄位為name
OutputStream outputStream = new BufferedOutputStream(new ByteArrayOutputStream());
SerializationUtils.serialize(SerializationUtils.serialize(dto),outputStream);

「方法 反射」
MethodUtils.invokeMethod() // 透過這物件 呼叫類別/Bean 方法執行操作

ref:
https://hcdtc.github.io/zh/docs/30-development-manual/2-back-end/99-dev-utils/2-common-lang3/1-stringutils/

#java 17 #spring boot







你可能感興趣的文章

HTML訓導處|<b><strong><i><em>標籤的差別

HTML訓導處|<b><strong><i><em>標籤的差別

form 表單驗證(即時)

form 表單驗證(即時)

一起來讀 CRAM - A Cognitive Robot Abstract Machine for Everyday Manipulation in Human Environments

一起來讀 CRAM - A Cognitive Robot Abstract Machine for Everyday Manipulation in Human Environments






留言討論