Additional explanation of several Java Collection frameworks
The fastutil library is better than the Trove Library: the Trove library has not been updated for three years and is a non-active open-source project. fastutil has been updating. Fastutil has more features, supports ultra-large collections (larger than 2 ^ 32, that is, 4 GB of sets), and more documents.
Fastutil is an extension of an open-source Java Collection Framework. It inherits the Java Collection Framework and provides several specific types of containers, it includes map, set, list, and priority queue, and implements java. standard interfaces of the util package (two-way iterators not available in the standard class), a large (64-bit) array, set, list, and quick and practical binary or text file I/O operations.
Fastutil provides large-capacity functions for dedicated collection classes, and is more compact and faster than common Java Collection classes. In addition to the object and original type, the fastutil class also provides support for references, that is, the object can be compared using the equal sign operator, without the need to use the equals () method.
Fastutil is released under the Apache 2.0 license. JDK 7 or later is required.
Adding an element to the fastutil set is the same as the API used to add the element to the standard Java set. The following code example shows the ArrayList for inserting an element to the standard JDK and the DoubleArrayList for inserting the element to the fastutil.
1. Insert the Double object to the JDK ArrayList and insert the Double object to the DoubleArrayList of fastutil.
import java.util.ArrayList;import it.unimi.dsi.fastutil.doubles.DoubleArrayList;public class DoubleArrayListTest { public static void main(String[] args) { demoJdkArrayListForDoubles(); demoFastutilArrayListForDoubles(); } public static void demoJdkArrayListForDoubles(){ final ArrayList
doubles = new ArrayList<>(); doubles.add(98.5); doubles.add(24.7); doubles.add(52.8); doubles.add(1234.3); doubles.add(20.0); System.out.println("\tDoubles List: " + doubles); } public static void demoFastutilArrayListForDoubles(){ final DoubleArrayList doubles = new DoubleArrayList(); doubles.add(98.5); doubles.add(24.7); doubles.add(52.8); doubles.add(1234.3); doubles.add(20.0); System.out.println("\tFastutil DoubleArrayList: " + doubles); }}
Output:
Doubles List: [98.5, 24.7, 52.8, 1234.3, 20.0] Fastutil DoubleArrayList: [98.5, 24.7, 52.8, 1234.3, 20.0]
From the code above, we can see that the implementation of the fastutil set is consistent with that of the standard JDK set. Let's look at another example:
2. Use the DoubleArrayList of fastutil as the stack
public static void demoFastutilDoubleStack(){ final DoubleArrayList stack = new DoubleArrayList(); stack.push(19.9); stack.push(32.1); stack.push(11.8); stack.push(3.3); System.out.println("Fastutil Stack of Doubles"); System.out.println("\tPeek: " + stack.peek(0) + "; After Size: " + stack.size()); System.out.println("\tPop: " + stack.pop() + "; After Size: " + stack.size()); System.out.println("\tPeek: " + stack.peek(0) + "; After Size: " + stack.size()); }
Output:
Fastutil Stack of Doubles Peek: 3.3; After Size: 4 Pop: 3.3; After Size: 3 Peek: 11.8; After Size: 3
Note that the peek () method and pop () method of DoubleArrayList have been deprecated. DoubleArrayList inherits AbstractDoubleList. The abstract class AbstractDoubleList discards the peek () method and pop () method, we recommend that you directly use the methods provided by specific classes of the specified type. Here, the add () and getDouble () methods are used.
The Trove Library provides the gnu. trove. TCollections class, which is similar to java. util. Collections and can be considered as a subset. Fastutil provides similar functions, but it provides static methods and is allocated to collection classes of the specified type or structure.
For example, the IntSets class is used with IntLinkedOpenHashSet. Just like its name, it specifies the collection class of the integer type and provides the corresponding static method.
3. Use IntSets in combination with IntLinkedOpenHashSet
public static void demofastutilCollectionsClass(){ final IntLinkedOpenHashSet integers = new IntLinkedOpenHashSet(); integers.add(43); integers.add(4); integers.add(8); integers.add(6); final IntSet unmodifiableIntegers = IntSets.unmodifiable(integers); System.out.println("Unmodifiable Integers: "); System.out.println("\tClass: " + unmodifiableIntegers.getClass().getCanonicalName()); try{ unmodifiableIntegers.add(15); }catch(Exception e){ System.out.println("\tException caught: " + e); } }
Output:
Unmodifiable Integers: Class: it.unimi.dsi.fastutil.ints.IntSets.UnmodifiableSet Exception caught: java.lang.UnsupportedOperationException
Fastutil supports standard Java iteration methods, using a clearly defined iterator and the for-each loop introduced in Java 5. The fastutil collection also supports the JDK 8-style. forEach () method, because the fastutil collection implements the java. util. Iterable interface. The following code is used to describe:
4. The fastutil set is iterated in the standard Java style.
public static void demoIterationWithIterator(){ final LongOpenHashSet longs = new LongOpenHashSet(); longs.add(11); longs.add(23401203413l); longs.add(3); longs.add(32); longs.add(9); final LongIterator longIterator = longs.iterator(); while(longIterator.hasNext()){ final long longValue = longIterator.next(); System.out.print(longValue + "\t"); }}
Output:
9 32 11 3 23401203413
.
public static void demoIteratorWithForEach(){ final LongLinkedOpenHashSet longs = new LongLinkedOpenHashSet(); longs.add(11); longs.add(23401203413l); longs.add(3); longs.add(32); longs.add(9); for(final long longValue : longs){ System.out.print(longValue + "\t"); }}
Output:
11 23401203413 3 32 9
.
public static void demoIteratorWithJdk8ForEach(){ final LongLinkedOpenHashSet longs = new LongLinkedOpenHashSet(); longs.add(11); longs.add(23401203413l); longs.add(3); longs.add(32); longs.add(9); longs.forEach(longValue -> System.out.print(longValue + "\t"));}
Output:
11 23401203413 3 32 9
Other fastutil-related situations
1) The fastutil set implements interfaces of the standard JDK 8 set, so its API is easy to use.
2) The fastutil set usually provides a constructor with parameters (taking the basic data type array as the parameter) and overwrites the toArray () method, and provides a method of the specified type (such as toDoubleArray () for the Double set ).
3) The fastutil set usually provides the implementation of explicitly rewriting toString (), which makes it easy to output each data element, which is different from the Java standard Arrays. toString.
4) The fastutil package is organized based on the implementation of the original type and different data structures, and is all in the same package.
5) because each fastutil set is related to a specific raw data type, no generic parameters are required for each set, and there is no problem related to generics.
6) fastutil API documentation is the best document for learning to use fastutil.