java fastutil_具有FastUtil的精简Java集合
java fastutil
針對(duì)我最近在GNU Trove庫(kù)上發(fā)表的《 發(fā)現(xiàn)Java原始資源集合的處理 》一書(shū) , TheAlchemist指出了fastutil優(yōu)于trove的一些優(yōu)點(diǎn):“我更喜歡fastutil( http://fastutil.di.unimi.it/ ),因?yàn)樗栽诜e極開(kāi)發(fā)中,具有更多功能,支持大尺寸(> 2 ^ 32),并且具有更好的文檔。” 阿提拉-米哈伊·巴拉茲已借調(diào)第二個(gè)“I @ TheAlchemist的建議為fastutil:此! 這是一個(gè)很棒的圖書(shū)館?!?在這篇文章中,我從以前對(duì)trove的一些相同角度研究了fastutil 。
在fastutil主頁(yè)上,將fastutil描述為Java TM Collections Framework的擴(kuò)展,它提供了“特定于類型的映射,集合,列表和隊(duì)列,具有較小的內(nèi)存占用以及快速的訪問(wèn)和插入”,以及“大型(64位)數(shù)組,設(shè)置和列表,以及用于二進(jìn)制和文本文件的快速,實(shí)用的I / O類。” fastutil的許可證是Apache License,版本2 ,而fastutil的當(dāng)前版本需要Java 7或更高版本。 當(dāng)前(截至撰寫(xiě)本文時(shí)), Java 6和Java 5也可以下載“未維護(hù)”的fastutil版本。
通過(guò)與標(biāo)準(zhǔn)Java集合使用的API調(diào)用相同的API調(diào)用來(lái)完成將元素添加到FastUtil集合的操作,如下面的代碼清單所示,該代碼列表比較了將元素插入JDK ArrayList和將元素插入FastUtil DoubleArrayList的過(guò)程 。
使用JDK插入雙打和使用FastUtil DoubleArrayList插入雙打
/*** Demonstrate standard JDK {@code ArrayList<Double>}* with some JDK 8 functionality.*/ public void demonstrateJdkArrayListForDoubles() {final ArrayList<Double> doubles = new ArrayList<>();doubles.add(15.5);doubles.add(24.4);doubles.add(36.3);doubles.add(67.6);doubles.add(10.0);out.println("JDK ArrayList<Double>:");out.println("\tDoubles List: " + doubles); }/*** Demonstrate use of DoubleArrayList and show how* similar using it is to using {@code ArrayList<Double>}.*/ public void demonstrateFastUtilArrayListForDoubles() {// Demonstrate adding elements to DoubleArrayList is// exactly like adding elements to ArrayList<Double>.final DoubleArrayList doubles = new DoubleArrayList();doubles.add(15.5);doubles.add(24.4);doubles.add(36.3);doubles.add(67.6);doubles.add(10.0);out.println("FastUtil DoubleArrayList:"); // DoubleArrayList overrides toString()out.println("\tDoubles List: " + doubles); }當(dāng)執(zhí)行上述兩種方法時(shí),寫(xiě)入標(biāo)準(zhǔn)輸出的雙精度項(xiàng)列表看起來(lái)完全相同,即使用相同的方括號(hào)括住逗號(hào)分隔的雙精度值。
FastUtil集合傾向于實(shí)現(xiàn)適當(dāng)?shù)腏DK集合接口。 例如,剛剛演示的類DoubleArrayList實(shí)現(xiàn)了幾個(gè)接口,包括Collection <Double>和List <Double>。 事實(shí)證明DoubleArrayList還實(shí)現(xiàn)了it.unimi.dsi.fastutil.doubles.DoubleStack和it.unimi.dsi.fastutil.Stack <Double> 。 下一個(gè)代碼清單中展示了使用此類作為堆棧的能力。
使用FastUtil的DoubleArrayList作為堆棧
/*** Demonstrate FastUtil's Double Stack.** FastUtil's DoubleStack allows access to its* contents via push, pop, and peek. It is declared* as a DoubleArrayList type here so that the size()* method is available without casting.*/ public void demonstrateFastUtilDoubleStack() {final DoubleArrayList stack = new DoubleArrayList();stack.push(15.5);stack.push(17.3);stack.push(16.6);stack.push(2.2);out.println("FastUtil Stack of Doubles");out.println("\tPeek: " + stack.peek(0) + "; After Size: " + stack.size());out.println("\tPop: " + stack.pop() + "; After Size: " + stack.size());out.println("\tPeek: " + stack.peek(0) + "; After Size: " + stack.size()); }正如我在Trove上的博客文章中所討論的那樣,Trove提供了一個(gè)gnu.trove.TCollections類,該類與java.util.Collections類似(子集)。 FastUtil提供了類似的功能,但是這種提供靜態(tài)方法以對(duì)FastUtil集合起作用的方法通過(guò)靜態(tài)方法被分為特定于類型的類型和特定于結(jié)構(gòu)的類,而不是在具有靜態(tài)方法的單個(gè)類中。 下一個(gè)代碼清單演示將這些特定于類型和特定于結(jié)構(gòu)的類之一與靜態(tài)方法IntSets結(jié)合使用, 并將其與FastUtil IntLinkedOpenHashSet結(jié)合使用。 顧名思義, IntSets類提供了“使用[int]特定的集可以做有用的事情的靜態(tài)方法和對(duì)象?!?
將IntSet與IntLinkedOpenHashSet一起使用
/*** Demonstrate one of FastUtil's "equivalent"s of the* java.util.Collections class. FastUtil separates its* grouping of static methods into classes that are* specific to the data type of the collection and to* the data structure type of the collection.*/ public void demonstrateFastUtilCollectionsClass() {final IntLinkedOpenHashSet integers = new IntLinkedOpenHashSet();integers.add(5);integers.add(7);integers.add(3);integers.add(1);final IntSet unmodifiableIntegers = IntSets.unmodifiable(integers);out.println("Unmodifiable Integers:");out.println("\tClass: " + unmodifiableIntegers.getClass().getCanonicalName());try{unmodifiableIntegers.add(15);}catch (Exception ex){out.println("\tException caught: " + ex);} }FastUtil支持使用顯式迭代器和Java SE 5引入的for-each循環(huán)的標(biāo)準(zhǔn)Java迭代方法。 因?yàn)镕astUtil集合實(shí)現(xiàn)java.lang.Iterable,所以FastUtil集合甚至使用.forEach支持JDK 8樣式 (假定代碼是在JDK 8上構(gòu)建和運(yùn)行的)。 這些將在下一個(gè)代碼清單中進(jìn)行演示。
以標(biāo)準(zhǔn)Java樣式迭代FastUtil集合
/*** Demonstrate "traditional" Java iteration of a* FastUtil collection.*/ public void demonstrateIterationWithIterator() {final LongOpenHashSet longs = new LongOpenHashSet();longs.add(15);longs.add(6);longs.add(12);longs.add(13);longs.add(2);final LongIterator longIterator = longs.iterator();while (longIterator.hasNext()){final long longValue = longIterator.next();out.print(longValue + " ");} }/*** Demonstrate iteration of a FastUtil collection* using Java's enhanced for-each approach.*/ public void demonstrateIterationWithForEach() {final LongLinkedOpenHashSet longs = new LongLinkedOpenHashSet();longs.add(15);longs.add(6);longs.add(12);longs.add(13);longs.add(2);for (final long longValue : longs){out.println(longValue + " ");} }/*** Demonstrate iteration of a FastUtil collection* using JDK 8 .forEach approach.*/ public void demonstrateIterationWithJdk8ForEach() {final LongLinkedOpenHashSet longs = new LongLinkedOpenHashSet();longs.add(15);longs.add(6);longs.add(12);longs.add(13);longs.add(2);longs.forEach(longValue -> out.print(longValue + " ")); }與FastUtil相關(guān)的其他觀察
- 由于FastUtil集合實(shí)現(xiàn)標(biāo)準(zhǔn)的JDK 8集合接口,因此可以很容易地使用熟悉的Java習(xí)慣用法來(lái)獲取和使用這些API。
- FastUtil集合通常提供一個(gè)構(gòu)造函數(shù),該構(gòu)造函數(shù)接受基礎(chǔ)數(shù)據(jù)類型的數(shù)組以及重寫(xiě)的toArray()方法和特定于類型的方法(例如toDoubleArray() [用于雙向定位的集合])以數(shù)組形式提供其數(shù)據(jù)元素。原語(yǔ)。
- FastUtil集合通常提供顯式重寫(xiě)的toString()實(shí)現(xiàn),這些實(shí)現(xiàn)允許像JDK集合一樣輕松編寫(xiě)各個(gè)數(shù)據(jù)元素,并且與Java數(shù)組(需要Arrays.toString()方法)不同。
- FastUtil的Java程序包通常按原始類型進(jìn)行組織,并且該原始類型的各種數(shù)據(jù)結(jié)構(gòu)類型的特定實(shí)現(xiàn)都在同一程序包中進(jìn)行。 例如,程序包的名稱類似it.unimi.dsi.fastutil.doubles , it.unimi.dsi.fastutil.ints等。
- 因?yàn)槊總€(gè)FastUtil集合都特定于特定的原始數(shù)據(jù)類型,所以每個(gè)集合都不需要通用參數(shù),并且沒(méi)有與通用相關(guān)的問(wèn)題(例如擦除)。 我還沒(méi)有看到FastUtil像Trove一樣利用特定于類型的集合的特定于類型的方法,這可能是因?yàn)镕astUtil更緊密地實(shí)現(xiàn)了相應(yīng)的Java集合接口。
- 學(xué)習(xí)使用FastUtil時(shí), FastUtil的Javadoc API文檔可能是最佳的起點(diǎn)。 在FastUtil的基于Javadoc的API文檔中,類,接口,枚舉和包通常都記錄得很好。
結(jié)論
與Trove一樣,FastUtil是一個(gè)庫(kù),可以用來(lái)更有效地(就內(nèi)存和性能而言)與Java集合一起使用。 Trove似乎曾經(jīng)是眾多選擇中最受歡迎的,而FastUtil也許是目前最受歡迎的,原因包括TheAlchemist引用的那些理由:“仍在積極開(kāi)發(fā)中,具有更多功能,支持大尺寸(> 2 ^ 32 ),并具有更好的文檔?!?除了Trove和FastUtil之外, 類似的庫(kù)還包括Java的高性能基元集合 ( HPPC ), Koloboke , Goldman Sachs集合 , Mahout集合和Javolution 。
翻譯自: https://www.javacodegeeks.com/2016/01/leaner-java-collections-with-fastutil.html
java fastutil
總結(jié)
以上是生活随笔為你收集整理的java fastutil_具有FastUtil的精简Java集合的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 野生狐狸怕什么 狐狸主要吃什么
- 下一篇: javaone_JavaOne 2015