首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> Java 程式設計 >> Java 相關知識

Apache Commons 常用工具類與方法全攻略

     編輯:Java 相關知識

核心基礎類

  • toString 生成
    • ToStringBuilder、ToStringStyle:快速產生物件的字串表示,支援多種輸出風格。
  • hashCode 實作
    • HashCodeBuilder:傳入奇數與質數參數,自動產生高品質雜湊碼。
  • equals 比較
    • EqualsBuilder:建議使用能唯一識別物件的欄位進行比較。
  • compareTo 排序
    • CompareToBuilder:參數比較順序類似堆疊,後加入的優先比較。

陣列操作

  • 列印陣列
    • ArrayUtils.toString(array)、ArrayUtils.toString(list.toArray())
  • 複製與反轉
    • ArrayUtils.clone(array)、ArrayUtils.reverse(array);JDK 原生 clone() 也可複製陣列,Collections.reverse() 可反轉 List。
  • 基本型別與包裝型別轉換
    • ArrayUtils.toObject(array)、ArrayUtils.toPrimitive(array);搭配 Arrays.asList() 與 List.toArray() 使用更靈活。
  • 元素查找
    • ArrayUtils.contains(array, value)、ArrayUtils.indexOf(array, value)、ArrayUtils.lastIndexOf(array, value)
  • 二維陣列轉 Map
    • ArrayUtils.toMap(array)

日期格式化

  • DateFormatUtils(線程安全)
    • 基於 FastDateFormat 實作,適用於多執行緒環境。
  • 日期四捨五入
    • DateUtils.round(date, Calendar.**):取最接近指定欄位的日期。
  • 日期截斷
    • DateUtils.truncate(date, Calendar.**):將日期截斷到指定精度。

唯一 ID 生成

  • IdentifierUtils、IdentifierFactory:先透過 IdentifierUtils 取得 Factory,再產生唯一標識。

文字處理

  • 空值檢查:StringUtils.isBlank()、isNotBlank()
  • 截斷縮寫:StringUtils.abbreviate()
  • 分割字串:StringUtils.split()
  • 取得夾層內容:StringUtils.substringBetween(str, start, end)
  • 去除空白:trim()、trimToNull()
  • 移除換行:StringUtils.chomp()
  • 字串填充、置中、連接:repeat()、center()、join()
  • 反轉字串:reverse()、reverseDelimited()
  • 自動換行:WordUtils.wrap()、wrapTest()
  • 內容測試:isNumeric()、isAlpha()、isAlphanumeric() 等
  • 計算出現次數:countMatches()
  • 解析格式化字串:substringBetween()、substringAfter()、substringBefore() 等
  • 編輯距離:getLevenshteinDistance()、difference()、indexOfDifference()

字元編碼

  • Base64、Hex、SoundEx

JavaBean 操作

  • 簡單屬性存取:PropertyUtils.getSimpleProperty()
  • 巢狀屬性存取:PropertyUtils.getNestedProperty()
  • 索引屬性存取:PropertyUtils.getIndexedProperty()
  • Map 屬性存取:PropertyUtils.getMappedProperty()
  • 綜合路徑存取:PropertyUtils.getProperty(obj, "regions[0].cities(richmond).population")
  • 屬性類型檢查:PropertyUtils.getPropertyType()、PropertyDescriptor 相關方法
  • Bean 比較器:BeanComparator
  • 屬性複製:PropertyUtils.copyProperties()
  • Bean 複製:BeanUtils.cloneBean()
  • 屬性設值:PropertyUtils.setProperty()
  • 可讀/可寫判斷:PropertyUtils.isReadable()、isWritable()
  • Predicate 過濾:BeanPredicate
  • 轉 Map:PropertyUtils.describe()、BeanMap
  • 字串化屬性:BeanUtils.getProperty()、setProperty()

集合工具

  • 迭代器:LoopingIterator、ArrayListIterator
  • Predicate 過濾:FilterIterator、CollectionUtils.filter()(修改原集合)、select()/selectRejected()(回傳新集合)
  • 去重:UniqueFilterIterator
  • 計數容器:Bag、HashBag、TreeBag
  • 緩衝區:Buffer、UnboundedFifoBuffer、BoundedFifoBuffer
  • 優先佇列:PriorityBuffer
  • 阻塞緩衝:BlockingBuffer.decorate()
  • 多值 Map:MultiMap
  • 雙向 Map:BidiMap、DualHashBidiMap、TreeBidiMap
  • 忽略大小寫 Map:CaseInsensitiveMap
  • 型別安全集合:TypedCollection.decorate()、TypedMap.decorate()
  • Predicate 限制:PredicatedMap、PredicatedList 等裝飾器
  • Transformer 轉換:CollectionUtils.transform()、ChainedTransformer
  • LRU 快取:LRUMap
  • 延遲載入 Map:LazyMap.decorate()
  • 統計元素頻率:CollectionUtils.countMatches()、cardinality()、getCardinalityMap()
  • 集合運算:union()、intersection()、disjunction()、subtract()
  • Map 直接取值:MapUtils.getBoolean()、getNumber() 等

XML 處理

  • Digester:規則驅動的 XML 物件映射。

應用程式架構

  • 命令列解析:CommandLineParser、Option、HelpFormatter 等
  • 設定檔解析:Configuration、PropertiesConfiguration、DOMConfiguration
  • 複合設定:ConfigurationFactory

數學運算

  • 分數:Fraction
  • 極值:NumberUtils.max()、Max.evaluate()、Min.evaluate()
  • 數值範圍:IntRange、DoubleRange 等
  • 隨機數:RandomUtils.nextInt()、nextLong()、nextBoolean()
  • 統計分析:StatUtils(最大、最小、平均、總和、內積等)
  • 線性方程:RealMatrix
  • 複數:Complex、ComplexFormat
  • 迴歸分析:SimpleRegression

模板引擎

  • commons-jexl 套件

IO 與網路

  • 串流複製:IOUtils.copy()、IOUtils.toString()
  • 靜默關閉:IOUtils.closeQuietly()
  • 人性化檔案大小:FileUtils.byteCountToDisplaySize()
  • 檔案操作:copyFile()、writeStringToFile()、copyURLToFile()
  • 目錄刪除:deleteDirectory()、cleanDirectory()
  • 目錄大小:sizeOfDirectory()
  • 建立空檔:FileUtils.touch()
  • 檔案過濾:FilenameFilter、SuffixFileFilter、AndFileFilter 等
  • 計數串流:CountingInputStream、CountingOutputStream
  • Tee 輸出:TeeOutputStream
  • Glob/Regex 過濾:GlobFilenameFilter、Perl5FilenameFilter
  • 網路協議:FTPClient、SMTPClient、POP3Client

HTTP 與 WebDAV

  • GET 請求範例:使用 HttpClient、GetMethod、狀態碼檢查、回應體取得、連線釋放。
  • 參數設定:setQueryString()、NameValuePair
  • 除錯記錄:設定 org.apache.commons.logging 系統屬性啟用 Wire 級別除錯。
  • POST 請求:PostMethod、addParameter()
  • 檔案上傳:MultipartPostMethod、addPart()
  • 認證機制:HttpState(Basic)、NTCredentials(NTLM)
  • Cookie 處理:Cookie 類別
  • 重導向:setFollowRedirects(true)
  • SSL 支援:GetMethod 自動處理、ProtocolSocketFactory 自訂憑證
  • WebDAV:WebdavResource、lockMethod/unlockMethod

搜尋與過濾

  • commons-jxpath、lucene 相關套件
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved