0% found this document useful (0 votes)
19 views10 pages

Algorithms and Data Structure Libraries For Java

Data structures

Uploaded by

vgopi379
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views10 pages

Algorithms and Data Structure Libraries For Java

Data structures

Uploaded by

vgopi379
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Acta Electrotechnica et Informatica, Vol. 20, No. 1, 2020, 39–48, DOI: 10.

15546/aeei-2020-0006 39

ALGORITHMS AND DATA STRUCTURE LIBRARIES FOR JAVA

Patrik PERHÁČ, Slavomı́r ŠIMOŇÁK


Department of Computers and Informatics, Faculty of Electrical Engineering and Informatics, Technical University of Košice,
Letná 9, 042 00 Košice, E-mail: [email protected], [email protected]

ABSTRACT
This paper is dedicated to the comparison of different libraries of algorithms and data structures for the Java language. Within
the paper currently available and relevant libraries are analyzed and compared. Selected libraries are compared in terms of provided
implementations of the particular data structures and algorithms, but also based on the performance of these implementations. Then a
summary of the results and recommendations regarding the practical uses of each library are provided. Performance is measured using
the Java Microbenchmark Harness. According to the results of this paper, implementations in Java Collections Framework are suitable
for most use cases, when more complex data structures are not needed. When more specific data structures or algorithms are needed,
that are not covered in Java Collections, GUAVA is a good alternative. For graph-like data structures the use of libraries JUNG and
JGraphT is recommended.

Keywords: data structures, algorithms, libraries, Java

1. INTRODUCTION vide for the data structures which are also mentioned in this
paper. That paper is not actual and does not provide detailed
Libraries are used frequently by programmers and make comparisons and descriptions of the selected libraries.
their work easier. These libraries are written by profession- A study on usage patterns of collections implementa-
als, who optimize the solutions to have the best possible tions from six popular alternative collection libraries, and
performance. Libraries of algorithms and data structures their evaluation and performance comparisons in different
provide implementations of common data structures which scenarios can be found in the paper Empirical Study of Us-
are the fundamental building blocks of every program [1]. age and Performance of Java Collections [5]. 10,986 Java
These implementations often differ from each other in their projects were analyzed in that paper.
internal structure, but also in performance. For a program- Other useful information can be found on the site algo-
mer to be able to decide which library or implementation to rist 3 . This site offers basic information about a great num-
use in a given project, he must have at least basic knowl- ber of libraries for a wide variety of languages. Detailed
edge of these libraries and their possible uses. Although comparisons are not provided on this website.
this paper focuses on the Java language, libraries provid- In an article listing useful libraries for Java, among oth-
ing implementations for data structures and algorithms ex- ers, libraries for collections are also listed, though not com-
ist for other languages too. To mention only a few of them: pared with each other [6]. An overview of the provided
System.Collections.Generic [2] and C5 Generic Collection data structures in the built in Java Collections Framework
Library for C# and CLI [3] for C#, STL1 for C++ and the is provided by John Zukowski in his book [7].
Python Standard Library2 for Python. Libraries for these In contrast to the mentioned articles and collections of
languages or others could also be compared in a further libraries, in our work the selected libraries were compared
work. The main goal of this paper is to present and sum- in detail based on their contents, the implementations they
marize the results of our work, which provided a detailed provide for the selected data structures and algorithms, and
comparison of the contents and performance of the built- also the performance of these implementations. The results
in Java Collection Framework and several other libraries of of the comparison are also summarized and recommenda-
algorithms and data structures. These comparisons should tions regarding the practical use of each library are provided
provide a basis on which programmers can decide which in the work related to this paper [8].
library is suitable for use in their projects.

3. METHODS
2. RELATED WORK
In this paper selected libraries were compared based on
A basic comparison was done, when the Data Structures their contents and performance. Their contents were eval-
Library for Java was written [4]. That paper provides com- uated based on how many different implementations they
parison of Java Collections, the Generic Library for Java, provide for abstract data types and algorithms, and what
the Graph Foundation Classes for Java, and the Data Struc- these implementations provide (methods, capabilities and
tures Library in Java. However, some of these libraries other attributes) [9].
are outdated or not open source. Also, the performance of Their performance was measured using the Java Mi-
these libraries is not compared. In that paper libraries were crobenchmark Harness (JMH) [10] [11]. Google Caliper
mostly compared based on the implementations they pro- was also considered for this task, but JMH offers more flex-
1 StandardTemplate Library http://www.cplusplus.com/reference/stl/
2 PythonStandard Library https://docs.python.org/3/library/
3 http://algorist.com/algorist.html

ISSN 1335-8243 (print) c 2020 FEI TUKE ISSN 1338-3957 (online), www.aei.tuke.sk
40 Algorithms and Data Structure Libraries for Java

ible and customizable measurements for Java code frag- store key-value pairs. JDSL can be obtained from GitHub4
ments [12] [13]. Benchmarks were run in average time or from the Maven Repository5 .
mode (measures the average time it takes for the benchmark
method to execute). For the measurements in this paper 10 4.3. Java Universal Network/Graph Framework
warm up iterations were used to make sure the results are
not completely random. More warm up iterations make the JUNG is an open source library for modeling, ana-
measurements more accurate. After warm up, 15 measure- lysis and visualization of graphs [16]. This library focuses
ment iterations were run [14]. While the benchmarks were mainly on the ADT Graph, but also provides implementa-
running, the computer it was running on was not used, and tions for the ADT Tree. According to the authors, the target
all unnecessary applications were closed to make the results audience is Java programmers with interest in graphs, and it
as clean as possible. The results are also influenced by the is suited for building applications related to network explo-
hardware used, but since all benchmarks were running on ration and data mining [17]. JUNG covers a large portion
the same system under the same circumstances, the values of the graph algorithms. All algorithms can be found in
of the benchmark can be different on different systems or the package edu.uci.ics.jung.algorithms, divided into sub-
hardware, while the relationship between the values remain packages by category. Tools for graph visualization are pro-
the same. vided in the package edu.uci.ics.jung.visualization, but the
Another possibility of measuring performance is the use library Vl-jung6 can also be used for this purpose. JUNG
of Savina benchmark suite for actor oriented programs [15]. can be downloaded from its official site7 , or from the Maven
However this possibility was not explored further in the Repository.
work related to this paper.
4.4. Google Core Libraries for Java
4. LIBRARIES GUAVA is also an open source set of libraries de-
Libraries, which implement the basic or more advanced veloped by Google, which is used in almost all Google
data structures or algorithms can be found for every higher projects written in Java. It provides new types of col-
level language. Most languages even have built in support lections, immutable collections, graph implementations,
for the most frequently used ones. In this work we focus hashing and much more. GUAVA contains implemen-
on open source libraries that are dedicated to the Java lan- tations or some kind of extension for an existing im-
guage. plementation for every mentioned data structure except
the ADT Tree, but tree-like structures can also be repre-
sented as directed acyclic graphs. It provides tree traver-
4.1. Java Collections Framework sal and graph algorithms. In terms of algorithms and data
structures, the packages com.google.common.collect and
Java Collections (JC) is the built in library of basic data com.google.common.graph are of the most interest to this
structures of the Java language. It contains implementa- work. The latest version of GUAVA can be imported to any
tions for the ADT List, ADT Set, ADT Priority queue, ADT project from the Maven Repository.
Dictionary and also for sorting and searching algorithms.
JC is frequently updated to use the most recent technolo-
gies provided by Java. The Collection interface is on the 4.5. Apache Commons Collections
top of the class hierarchy in JC, from which all other in- Commons Collections expands Java Collections with
terfaces and classes are derived, the only exception is the new interfaces and implementations. CC is an open source
Map interface. The algorithms that can be used on these library which is still being developed. It decorates already
data structures are implemented in the static methods of the existing implementations with new behaviours, such as sup-
Collections class. port for use in multiple threads, or the ability to access el-
ements of a collection using a key. It also provides its own
4.2. Data Structures Library for Java implementations for certain data structures like lists, sets or
maps. All classes and interfaces in Commons Collections
JDSL is an older library, developed at Brown Univer- are derived from existing ones in Java Collections. Apart
sity. Its newest version was released in 2005 [4]. According from data structures, CC also provides different implemen-
to the authors, this library is not supposed to replace Java tations of iterators and comparators. Like all previous li-
Collections, but extend it. JDSL provides implementations braries, CC can also be obtained from the Maven Reposi-
for all the mentioned data structures except ADT Set, and tory.
covers most of the mentioned algorithms as well. The in-
terfaces in JDSL are organised to two categories: one for
4.6. JGraphT
positional containers and the second for key-based contain-
ers. Positional containers capture the topological relations Like JUNG, JGraphT is also specialised for the ADT
between elements, while key-based containers are used to graph. It covers most types of graphs from graph the-
4 JDSL on GitHub https://github.com/lewischeng-ms/jdsl
5 JDLS on Maven https://mvnrepository.com/artifact/jdsl/jdsl
6 Vl-jung GitGub homepage https://github.com/timboudreau/vl-jung
7 Official site of JUNG: http://jung.sourceforge.net/download.html

ISSN 1335-8243 (print) c 2020 FEI TUKE ISSN 1338-3957 (online), www.aei.tuke.sk
Acta Electrotechnica et Informatica, Vol. 20, No. 1, 2020 41

ory, and also most of the graph algorithms. JGraphT decorator design pattern to add new properties to existing
provides adapters to convert graphs from other libraries, implementations (for example GrowthList or LazyList).
such as GUAVA. All graph implementations in JGraphT
implement the Graph interface and are grouped in the Table 1 Data structure and algorithm coverage of the libraries.
org.jgrapht.graph package. The algorithms are in the JC = Java Collections Framework, DSL = Data Structures
package org.jgrapht.alg divided into sub-packages by cate- Library for Java, JNG = Java Universal Network/Graph
gory. Traversal algorithms are also provided in the package Framework, GUA = Google Core Libraries for Java, CC =
org.jgrapht.traverse. JGrapht supports graph visualization, Apache Commons Collections, JGT = JGraphT, PQ = Priority
however an external library must be used. The authors rec- queue, Dict = Dictionary, BFS = Breadth First Search, DFS =
ommend the JGraphX library for the visualization of graphs Depth First Search, MST = Minimum Spanning Tree
created in JGraphT [18].
JC DSL JNG GUA CC JGT
4.7. Other libraries
List • • • •
Apart from the previously mentioned libraries, others
are also available. When JDSL was created, it was com- Set • • •
pared to the libraries that were available at the time. They PQ • • •
were the Generic Library for Java (JGL) and Graph Foun-
dation Classes for Java (GFC). JGL was based on the design Dict • • • •
of the STL C++ library. It is not open source so it was not Tree • •
included in this comparison. GFC is an outdated library,
last updated in 1999. It uses old technologies which is the Graph • • • •
reason it’s not included in this comparison.
Other libraries, that are dedicated to a specific data BFS • •
structure or type of algorithm may also be available and DFS • • •
compared with each other in the future. This work is ded-
icated to the libraries that cover a larger number of data MST • • •
structures or algorithms.
Inorder •

5. COMPARISON Preorder • •

As previously mentioned, the selected libraries will be Postorder • •


compared based on their contents and their performance.
Merge sort • •
The data structures and algorithms for which the selected
libraries provide implementations are marked with a black Heap sort •
circle in Table 1. Performance was measured for data struc-
tures and algorithm which had multiple implementations. Quick sort • •

Radix sort •
5.1. ADT List
Bin. search •
The built in Java Collections Framework offers
two implementations for the ADT List. The first is From the mentioned implementations ArrayList had
ArrayList, which internally uses an array, the size of the best performance in each operation except inserting to
which dynamically changes. It has an other variant, the beginning of the list. TreeList was also fast when
CopyOnWriteArrayList, which is thread safe. The other idexes were used. Implementations from JDSL achieved
implementation is the LinkedList, which internally uses comparable results for most operations. The results of the
a doubly linked list. benchmark can be seen in Table 2.
JDSL provides two implementations of the ADT List:
ArraySequence and NodeSequence. They are analogi-
5.2. ADT Set
cal to the ArrayList and LinkedList from JC. In JDSL
methods for inserting to the beginning and the end of the There are multiple implementations for the ADT Set
list are also provided. provided by Java Collections. Thread safe implementa-
GUAVA offers a number of static utility methods for tions, like CopyOnWriteArraySet, and specific sets for
working with lists. These methods include constructing the storing enumeration types like EnumSet are also pro-
cartesian product of multiple lists, partitioning a list to sub- vided. For the performance comparison the implementa-
lists, or reversing the order of elements in a list. tions HashSet, LinkedHashSet and TreeSet were se-
Commons Collections offers some thread safe imple- lected.
mentations for the ADT List, and another implementation: The GUAVA library offers multiple implementations of
TreeList. The TreeList is optimized for fast insertions the Multiset data type, which is similar to ADT Set, but
and deletions based on indexes. Apart from these imple- allows duplicit elements. There are three main implemen-
mentations, CC also provides multiple classes using the tations of this data structures in GUAVA: HashMultiset,

ISSN 1335-8243 (print) c 2020 FEI TUKE ISSN 1338-3957 (online), www.aei.tuke.sk
42 Algorithms and Data Structure Libraries for Java

LinkedHashMultiset and TreeMultiset which are features, like synchronisation or only accepting elements
structurally similar to the implementations in JC. that match a specified predicate.
In Commons Collections there are also only implemen- The results of the performance tests (as seen in Table 3)
tations of the ADT Multiset. The main implementation of were done separately for the ADT Set and Multiset. The Set
this data structure is HashMultiSet which internally uses implementations achieved similar results in all operations
a HashMap. It also has a method called setCount, by which except remove and contains, where TreeSet was slower.
the number of occurrences of an element can be set. Other The same could be said about the Multiset implementations,
classes only decorate the base implementation with new where TreeMultiset had a worse performance.

Table 2 Results of the ADT List Benchmark. Unit of measurement: µs / operation

JC JDSL CC

ArrayList LinkedList ArraySequence NodeSequence TreeList

clear() 0.002 0.003 0.002 0.001 0.002

insert() 0.034 0.122 0.143 0.229 0.299

insert(first) 538.889 0.154 13380.409 0.205 0.357

insert(middle) 115.268 3050.458 7752.18 3122.877 0.372

insert(last) 9.486 28.579 45.001 32.702 0.354

remove(element) 4452.429 9080.809 n/a n/a 23886.322

contains(element) 1488.72 2529.767 0.005 0.005 5157.715

size() 0.003 0.003 0.003 0.003 0.003

isEmpty() 0.004 0.004 0.004 0.004 0.003

insertFirst() n/a n/a 12894.091 0.165 n/a

insertLast() n/a n/a 0.192 0.15 n/a

Table 3 Results of the ADT Set Benchmark. Unit of measurement: µs / operation

JC GUAVA CC

HashSet LinkedHashSet TreeSet HashMultiset LinkedHashMultiset TreeMultiset HashMultiSet

clear() 0.003 0.003 0.002 0.006 0.007 0.005 0.003

insert() 0.011 0.012 0.01 0.005 0.005 0.023 0.005

remove(el.) 0.003 0.003 0.052 0.002 0.002 0.131 0.002

contains(el.) 0.006 0.006 0.047 0.007 0.008 0.055 0.006

size() 0.004 0.004 0.004 0.004 0.004 0.014 0.003

isEmpty() 0.004 0.004 0.004 0.005 0.006 0.013 0.004

5.3. ADT Priority queue rityQueue for representing priority queues. It is a double-
ended queue, which provides access for the least and also
The class PriorityQueue represents the ADT Prior- the greatest element in the queue.
ity queue in Java Collections. The elements in the priority The performance of these implementations was
queue are ordered based on their natural ordering, or by a comparable in all operations except insert and
Comparator provided to the constructor. poll, where ArrayHeap had the worst perfor-
In JDSL the ADT Priority queue also has one imple- mance and PriorityQueue was slightly faster than
mentation: ArrayHeap, implemented using a binary heap. MinMaxPriorityQueue. The results can be seen in Fig. 1.
The library GUAVA provides the class MinMaxPrio-

ISSN 1335-8243 (print) c 2020 FEI TUKE ISSN 1338-3957 (online), www.aei.tuke.sk
Acta Electrotechnica et Informatica, Vol. 20, No. 1, 2020 43

Fig. 2 Results of the ADT Dictionary Benchmark pt.1. Unit of


measurement: µs / operation
Fig. 1 Results of the ADT Priority queue Benchmark. Unit of
measurement: µs / operation

5.4. ADT Dictionary

JC offers multiple implementations for the Dictionary


ADT: HashTable (obsolete since Java 11), HashMap (simi-
lar to HashTable), LinkedHashMap (uses a hash table and
a doubly linked list), TreeMap (based on a Red-Black tree),
WeakHashMap (based on a hash table, but values are auto-
matically removed when the key gets deleted by the garbage
collector).
In JDSL the Dictionary interface has two imple-
mentations: RedBlackTree (built on a restructurable bi-
nary tree) and HashtableDictionary (uses a chaining
hashtable).
Fig. 3 Results of the ADT Dictionary Benchmark pt.2. Unit of
In the GUAVA library, bidirectional maps (HashBiMap) measurement: µs / operation
and multimaps can be found. There are multiple ap-
proaches to implement a multimap based on the type
of collection in which the elements are stored. The
following classes were included in the performance
comparison: ArrayListMultimap, TreeMultimap and
HashMultimap. 5.5. ADT Tree
Commons Collections has implementations for
both the Map (HashedMap, LinkedMap) and Mul- In JDSL NodeTree represents a node-based multipur-
timap data structures (ArrayListValuedHashMap, pose tree, and NodeBinaryTree can be used as a binary
HashSetValuedHashMap). CC also provides implementa- tree.
tions for the bidirectional map data structure, which allows JUNG also has two implementations: DelegateTree
bidirectional lookup between key and values. (delegates to a specified instance of DirectedGraph) and
The performance was measured for the mentioned im- OrderedKAryTree (a tree where each vertex has ≤ k chil-
plementations of the ADT Dictionary or Map and Mul- dren).
timap. Due to the number of compared implemen- The performance was measured for all mentioned im-
tations, the results are displayed via charts in figures plementations except OrderedKAryTree, due to inconsis-
Fig. 2 and Fig. 3. TreeMap and TreeMultimap had the tencies with the documentation of the class. All three
worst performance, and also HastableDictionary and classes achieved similar results in all operations except par-
RedBlackTree from JDSL. However, the implementations ent, children and isRoot, where DelegateTree from JUNG
from JDSL have both find and findAll operations defined, had far worse performance than the implementations from
so they can be used as a map just as well as a multimap. JDSL. The results of the benchmark can be seen in Fig. 4.

ISSN 1335-8243 (print) c 2020 FEI TUKE ISSN 1338-3957 (online), www.aei.tuke.sk
44 Algorithms and Data Structure Libraries for Java

For the performance comparison the classes SparseGraph


and SparseMultigraph were selected, which represent
the base implementations of the graph and multigraph data
structure.
In GUAVA two types of graphs can be found:
Graph (values are only associated with the vertices) and
ValueGraph (vertices and also edges are associated with
values). In GUAVA graphs can be created with the help
of the GraphBuilder and ValueGraphBuilder classes.
Static utility methods are available in the Graphs class for
determining if a graph has cycles, creating a subgraph of a
graph induced by nodes, reversing the edge directions and
a lot more.
JGraphT is also a library specialized for the ADT
Graph. It offers implementations for every kind of graph
Fig. 4 Results of the ADT Tree Benchmark. Unit of
and most graph algorithms. For the performance compar-
measurement: µs / operation
ison the SimpleGraph and Multigraph classes were se-
lected, which are the base implementations for graphs and
multigraphs.
5.6. ADT Graph The performance of these implementations was mea-
sured on graphs with random structure. As seen in
One implementation of the ADT Graph is provided in
Table 4, performances are similar in most operations.
JDSL: IncidenceListGraph. It allows self-loops, paral-
SparseMultigraph had worse performance in the oper-
lel edges, mixed directed and undirected edges. It is im-
ations addEdge and adjacent. The removeEdge operation
plemented via a list of vertices and a list of edges. The
was the slowest for the IncidenceListGraph.
neighbouring vertices are stored for each vertex.
Further information about graphs and graph algorithms
JUNG is a library specifically for the ADT Graph. It
in Java can be found in the paper by Marije de Heus [19].
contains implementations for almost every kind of graph.

Table 4 Results of the ADT Graph Benchmark. Unit of measurement: µs / operation. v = vertex, w = weight

JDSL JUNG GUAVA JGraphT

Mutable-
IncidenceListGraph SparseGraph SparseMultigraph SimpleGraph Multigraph
ValueGraph

addVertex(vertex) 0.399 0.793 1.026 0.717 0.736 0.747

addEdge(v1, v2) 0.605 0.407 4.141 0.189 0.128 1.259

addEdge(v1, v2, w) n/a n/a n/a n/a 0.067 0.067

adjacent(v1, v2) 0.209 0.681 448.264 n/a n/a n/a

removeEdge(v1, v2) 74.137 0.048 1.721 0.114 0.033 0.032

getVertices() 0.011 0.008 0.015 0.006 0.004 0.004

5.7. Sorting algorithms Quick sort Quick sort is also a divide and conquer algo-
rithm, its approach is to separate bigger and smaller ele-
In this paper the following sorting algorithms were ments repeatedly. It uses a chosen pivot value that is used
taken into consideration: to divide bigger and smaller elements [20].

Merge sort Sorting algorithm with divide and conquer Radix sort Radix sort sorts strings or numbers by com-
approach. Merge sort divides the array or list to halves and paring their digits or characters. It groups the values by in-
combines them in a sorted manner [20]. dividual digits or characters that share the same significant
position and value, then joins these groups [20].
Heap sort Heap sort is a comparison based sorting algo-
rithm based on a binary heap structure. It creates a heap From the selected libraries only a few provide imple-
and then sorts the data in reverse by repeatedly placing the mentations of sorting algorithms. In Java Collections the
largest unsorted element into its correct place [20]. method sort in the Collections and Arrays classes uses

ISSN 1335-8243 (print) c 2020 FEI TUKE ISSN 1338-3957 (online), www.aei.tuke.sk
Acta Electrotechnica et Informatica, Vol. 20, No. 1, 2020 45

Timsort to sort Lists of objects or arrays of objects. An im- was slower compared to the implementations in JC. The
plementation of Dual-Pivot Quicksort can also be found in results of the implementations from JDSL were slower by
JC, however, it’s limited to sorting arrays of primitive types. multiple orders of magnitude. The results of the benchmark
JDSL provides implementations for multiple sort- can be seen in Table 5.
ing algorithms: ArrayMergeSort (optimised for sort-
ing ArraySequence) and ListMergeSort (optimised for 5.8. Searching algorithms
sorting NodeSequence). JDSL is the only library from the
selected ones, that provides an implementation of the Heap Binary search Binary search searches for a value in an
sort algorithm in the HeapSort class. An implementation ordered collection. It operates with a left an right index,
of Quick sort is also provided by the ArrayQuickSort compares the middle value and if the condition is unsatis-
class. fied, the half not containing the value is eliminated and the
An implementation of the Radix sort algorithm can be search continues on the remaining half until it finds the tar-
found in JGraphT in the RadixSort class, which has one get value [21].
method: sort, for sorting a list of integers. This implemen-
tation is limited to sorting lists of integers. If the number of An implementation of searching algorithms, specifically
elements in the list is less than RadixSort.CUT OFF, then binary search, was only found in Java Collections. The bi-
the standard Java sorting algorithm is used. narySearch method can be found in both the Collections
In the performance measurement Arrays.sort() and Arrays classes. The method returns the index of
achieved the best results, but it is limited to sorting arrays the search key, if it is contained in the list, otherwise,
of primitive types. The Timsort implementation in both (-(insertion point) - 1)8 . In the Arrays class over-
Collections.sort() and Arrays.sort() also were loaded methods are also provided to search only in a spe-
fairly fast. The Radix sort implementation from JGraphT cific part of an array [22] [23] [24].

Table 5 Sorting algorithms Benchmark results. Unit of measurement: µs / operation

JC JGraphT

Collections.sort(List) Arrays.sort(Object[]) Arrays.sort(int) RadixSort.sort(List)

3.418 3.103 0.41 26.336

JDSL

ArrayMergeSort ListMergeSort ArrayQuickSort HeapSort

385.625 393.144 3103.646 188.939

5.9. Tree traversals turn an iterator with the specified order of elements.
The performance was measured on trees with the same
Tree data structure can be traversed in multiple ways. structure. As seen in Table 6, implementations from JDSL
In this paper Inorder (visits left child first, then the root and achieved better results, that could be caused by the fact that
the right child is last), Preorder (root, left child, right child) GUAVA does not have a specific tree implementation, but it
and Postorder (left child, right child, root) traversals were uses directed acyclic graphs.
examined.
In JDSL traversing trees can be done using itera- Table 6 Tree traversal algorithms Benchmark results. Unit of
tors. It offers three iterators: PreOrderIterator (can measurement: µs / operation
be used with binary trees and multi purpose trees),
PostOrderIterator (also works with both types of trees) JDSL GUAVA
and InOrderIterator (only works with binary trees). In
addition, JDSL also offers an implementation of the Euler Tree BinaryTree MutableGraph
tour algorithm [25] in the EulerTour class. PreOrder 2.186 1.936 7.513
Although GUAVA does not have its own implementa-
tion of the ADT Tree, trees can be represented in GUAVA PostOrder 2.19 1.981 8.392
using directed acyclic graphs. A Traverser object can be
InOrder n/a 2.188 n/a
obtained using the Traverser.forTree() method. After ob-
taining the Traverser object for a specific tree, Postorder
traversal can be achieved by using the depthFirstPostOrder
5.10. Graph algorithms
method and Preorder traversal by using the depthFirstPre-
Order method on the Traverser object. Both methods re- This work focuses on three graph algorithms:
8 Collections class documentation https://docs.oracle.com/javase/7/docs/api/java/util/Collections.html

ISSN 1335-8243 (print) c 2020 FEI TUKE ISSN 1338-3957 (online), www.aei.tuke.sk
46 Algorithms and Data Structure Libraries for Java

Breadth-first search Starts at the root node, and explores graph transformation.
all of the neighbour nodes at the present depth, before mov- GUAVA provides an implementation for both DFS and
ing on to the nodes at the next level [26]. BFS algorithms, but it doesn’t contain any minimum-
spanning-tree implementations. Traversing graphs is sim-
Depth-first search Starts at the root node, and explores ilar to traversing trees in GUAVA. First, an instance of
as far as possible along each branch before backtracking the Traverser object must be obtained using the Tra-
[27]. verser.forGraph() method. After this, the breadthFirst,
depthFirstPreOrder and depthFirstPostOrder methods can
be called, each of which returns an Itarable object with the
Minimum-spanning-tree MST is a subset of the edges specified order of vertices.
of a graph that connects all vertices without any cycles and JGraphT is also a library specifically for the ADT
with the minimum possible total edge weight [28]. Graph. It contains implementations for all three previously
mentioned algorithms. It also provides implementations for
JDSL provides implementation for DFS in the DFS class. a large number of algorithms from graph theory. Depth-
Other variants of this algorithm are also available for di- first and Breadth-first traversals can be achieved by us-
rected graphs and for finding cycles in graphs. An im- ing the DepthFirstIterator and BreadthFirstIterator classes.
plementation of minimum-spanning-tree is also provided in JGraphT contains implementations of multiple minimum-
the IntegerPrimTemplate abstract class. When extend- spanning-tree algorithms, such as BoruvkaMinimumSpan-
ing this class, the method weight must be overridden. JDSL ningTree, KruskalMinimumSpanningTree and PrimMini-
offers other graph algorithms as well, for example topolog- mumSpanningTree.
ical sorting, an implementation of Dijkstra’s algorithm or Performance was measured on graphs with the same
pathfinding algorithms. structure. The results for the BFS and DFS algo-
Although JUNG is a library specifically for the ADT rithms were similar. From the implementations of the
Graph, it does not contain implementations of the DFS and minimum-spanning-tree algorithm, KruskalMinimumSpan-
BFS algorithms. It does contain a minimum-spanning-tree ningTree was the fastest, and PrimMinimumSpanningTree
implementation in the PrimMinimumSpanningTree class. It the slowest (three times slower than the implementation of
also provides a number of different algorithms related to Kruskal’s algorithm). The benchmark results can be seen in
clustering, filtering, graph flows, scoring, shortest path and Table 7.

Table 7 Graph algorithms Benchmark results. Unit of measurement: µs/operation

JGraphT
JDSL GUAVA JUNG
Boruvka Kruskal Prim

BFS n/a 4.631 n/a 6.471 n/a n/a n/a

DFS 7.388 6.259 n/a 7.53 n/a n/a n/a

SpanningTree 18.48 n/a 34.696 n/a 22.675 10.047 12.751

6. CONCLUSIONS AND RECOMMENDATIONS tions provided satisfy the requirements. Implementations


from JC are easy to use and offer a decent performance in
In this section the results of the comparison of the li-
comparison to the other libraries.
braries will be summarized. Also recommendations on the
possible cases in which the individual libraries can be used
will be provided for each library.
6.2. Data Structures Library for Java
6.1. Java Collections Framework
JDSL is the oldest from the selected libraries. Due to
One advantage of using JC is, that it is built into Java, its age, it does not support generic programming which
no additional library is needed. It provides implementa- may cause issues in the form of type incompatibility. The
tions for the basic data structures, such as lists, sets, pri- method names often differ from the ones in other libraries.
ority queues and dictionaries. These implementations are It covers all mentioned data structures except the ADT Set
frequently updated to use the newest technologies. JC has and supports a wider variety of operations for some data
multiple search algorithms included and is the only library structures. It also has a built in support for converting data
from the selected libraries, that has an implementation of a structures between JDSL and JC.
search algorithm. It lacks the more complicated data struc- The use of JDSL should be only considered if an im-
tures like the ADT Tree or Graph. plementation is needed which is not provided by any other,
Java Collection can be used when there is no need for more up to date library. Despite its age, JDSL has a lot of
more complex data structures and the basic implementa- interesting implementations for most data structures.

ISSN 1335-8243 (print) c 2020 FEI TUKE ISSN 1338-3957 (online), www.aei.tuke.sk
Acta Electrotechnica et Informatica, Vol. 20, No. 1, 2020 47

6.3. Java Universal Network/Graph Framework is not overwhelmingly good. Some implementations can
be useful in specific cases, for example TreeList when a
JUNG contains only implementations of the ADT
lot of insertions and deletions with indexes are executed, or
Graph and Tree, but it covers a relatively large vari-
when a specific behaviour provided by CC is needed. In
ety of graphs. It also contains tree implementations,
other cases the use of more universal, flexible library like
but no tree traversal algorithms. A big advantage of
GUAVA is recommended.
JUNG is its built-in support of graph visualization in the
edu.uci.ics.jung.visualization package.
This library can be used for modeling, analysis and vi-
sualization of graphs and networks. All implementations 6.6. JGraphT
can be modified to meet the needs of the user. It can also be
used for visualization (either using the built in tools or an JGraphT is also a library dedicated specifically for the
external library like Vl-jung) ADT Graph. It covers most types of graphs and graph algo-
rithms. From the selected libraries only JGraphT provides
6.4. Google Core Libraries for Java an implementation of the Radix sort algorithm, although
it is limited to sorting lists of integers. JGraphT also pro-
GUAVA is an up to date library, which provides imple- vides adapters for converting graphs from the GUAVA li-
mentations or new features for every mentioned data struc- brary. Graphs can also be visualized, but an external library
ture, except the ADT Tree, but trees can also be represented must be used. The authors recommend using the JGraphX
in GUAVA by directed acyclic graphs. It also provides tree library for visualization.
traversal algorithms and graph algorithms. Static utility
methods can be found in the Lists, Sets, Queues, Maps
and Graphs classes, that can make the programmer’s work
easier. 6.7. Future work
GUAVA is the most frequently used from the mentioned
libraries according to the Maven Repository. It can be used This work can be further extended for example by
for a wide variety of projects because of its flexibility. It’s widening the selection of Java libraries. Libraries for other
a great extension for JC. languages and platforms could also be considered (libraries
for C#, C++, Python and others) in comparison. The scope
of data structures could be extended to include data struc-
6.5. Apache Commons Collections
tures that are not covered in this paper. Furthermore a com-
Commons Collections provides a number of implemen- parison between the individual implementations could also
tations for the basic data structures but their performance be done based on their internal structure.

//javarevisited.blogspot.com/2018/01/top-20-
libraries-and-apis-for-java-programmers.html.
REFERENCES
[7] ZUKOWSKI, J.: Java Collections. Apress. Jan. 2008. 420p.
isbn: 1430208546, 9781430208549.
[1] SHAFFER, C.A.: Data Structures and Algorithm
Analysis. Edition 3.2 (Java Version). Dover Publi- [8] PERHÁČ, P.: Algorithms and Data Structures Libraries for
cations. Mar. 2013. url: http://people.cs.vt.edu/ Java, Bachelor’s thesis, May 2019.
~shaffer/Book/JAVA3elatest.pdf [9] GOODRICH, M.T. – TAMASSIA, R. – GOLDWASSER,
[2] Microsoft: Microsoft Docs, .NET Framework API Refer- M.H.: Data Structures and Algorithms in Java, 6th Edition.
ence, System.Collections.Generic Namespace. url: https: Wiley, 2014. ISBN: 978-1-118-77133-4.
//docs.microsoft.com/en-us/dotnet/api/system.
[10] EGOROV, D.: JMH - Great Java Benchmarking.
collections.generic?view=netframework-4.7.2.
dzone.com, Performance Zone, Tutorial. Okt. 2017.
[3] KOKHOLM, N. – SESTOFT, P.: The C5 Generic Collec- url: https://dzone.com/articles/jmh-great-
tion Library for C# and CLI, IT University of Copenhagen. java-benchmarking.
Nov. 2016. url: https://www.itu.dk/research/c5/.
[11] COSTA, D.: Benchmark-driven Software Perfor-
[4] TAMASSIA, R. et al.: An Overview of JDSL 2.0, mance Optimization. PhD. Thesis. July 2019, url:
the Data Structures Library in Java. Aug. 2005. url: https://www.researchgate.net/publication/
https://cs.brown.edu/cgc/jdsl/other_modules/ 335014121_Benchmark-driven_Software_
overview/overview.pdf. Performance_Optimization.
[5] COSTA, D. – ANDRZEJAK, A. – SEBOEK, J. – LO, D.: [12] SESTOFT, P.: Microbenchmarks in Java and C#.
Empirical Study of Usage and Performance of Java Col- IT University of Copenhagen, Denmark. Sep. 2015.
lections. ICPE ’17: Proceedings of the 8th ACM/SPEC url: https://www.itu.dk/people/sestoft/papers/
International Conference on Performance Engineering. benchmarking.pdf.
April 2017. url: https://dl.acm.org/doi/10.1145/
[13] SHIPILEV, A.: (The Art of) (Java) Benchmarking.
3030207.3030221.
Talk given by Aleksey Shipilev for JavaOne SF 2011.
[6] JAVIN, P.: Top 20 Libraries and APIs Java Developer should Okt. 2011. url: https://shipilev.net/talks/j1-
know. Javarevisited blog, September 2019. url: https: Oct2011-21682-benchmarking.pdf

ISSN 1335-8243 (print) c 2020 FEI TUKE ISSN 1338-3957 (online), www.aei.tuke.sk
48 Algorithms and Data Structure Libraries for Java

[14] COSTA, D.– BEZEMER, C.P. – LEITNER, P. – AN- [23] SULTANA, N. et al.: A Brief Study and Analysis of
DRZEJAK, A.: What’s Wrong With My Benchmark Different Searching Algorithms. 2017 IEEE International
Results? Studying Bad Practices in JMH Bench- Conference on Electrical, Computer and Communication
marks, IEEE Transactions on Software Engineering. doi: Technologies, IEEE ICECCT 2017, COIMBATORE,
10.1109/TSE.2019.2925345. June 2019. url: https:// Volume: 4. url: https://www.researchgate.net/
ieeexplore.ieee.org/document/8747433. publication/314175061_A_Brief_Study_and_
Analysis_of_Different_Searching_Algorithms.
[15] IMAM, S.M. – SARKAR, V.: Savina - An Actor Bench-
mark Suite: Enabling Empirical Evaluation of Actor Li- [24] SINGH, CH.: Search Algorithms in Java. StackAbuse.com.
braries. Proceedings of the 4th International Workshop on Mar. 2019. url: https://stackabuse.com/search-
Programming based on Actors Agents & Decentralized algorithms-in-java/
Control (AGERE! ’14). ACM, New York, USA, pp. 67-80, [25] FOURNIER, J.C.: Graph Theory and Applications: With
October 2014. url: http://soft.vub.ac.be/AGERE14/ Exercises and Problems, Chapter 9. ISTE Ltd., Jan. 2009.
papers/ageresplash2014_submission_19.pdf. ISBN: 9781848210707, 9780470611548.
[16] O’MADADHAIN, J. – FISHER, D. – NELSON, T.: JUNG [26] CORMEN, T.H. et al.: Introduction to Algorithms, Third
Java Universal Network/Graph Framework, JUNG Fre- Edition. The MIT Press. July 2009. ISBN: 9780262033848.
quently Asked Questions. Jan. 2010. url: http://jung. [27] THULASIRAMAN, K.: Handbook of Graph Theory, Com-
sourceforge.net/faq.html. binatorial Optimization, and Algorithms. Chapman and
[17] FISHER, D.: The Java Universal Network/Graph Frame- Hall/CRC. Dec. 2015. ISBN: 9781584885955.
work (JUNG): A Brief Tour. A talk given by Danyel Fisher [28] ANTOŠ, K.: Minimum spanning tree problem. 14th Con-
for the UC Irvine KDD Project Apr. 2004. ference on Applied Mathematics, APLIMAT 2015. Slovak
[18] MICHAIL, D. – KINABLE, J. – NAVEH, B. – University of Technology in Bratislava, 2015.
SICHI, J.V.: JGraphT - A Java library for graph
data structures and algorithms. Apr. 2019. url: Received July 29, 2019, accepted April 6, 2020
https://www.researchgate.net/publication/
332494171_JGraphT_--_A_Java_library_for_ BIOGRAPHIES
graph_data_structures_and_algorithms.
Patrik Perháč was born on 9. 7. 1997. In 2019 he gradu-
[19] DE HEUS, M.: Towards a Library of Parallel Graph Algo-
ated (B.Sc.) at the department of Computers and Informat-
rithms in Java. 14th Twente Student Conference on IT, En-
schede, The Netherlands, 2011. url: https://fmt.ewi. ics of the Faculty of Electrical Engineering and Informatics
utwente.nl/media/49.pdf at Technical University in Košice. His thesis was titled Al-
gorithms and data structures libraries for Java.
[20] MANOOCHEHR, A.: Abstract Data Types and Algorithms.
Macmillan Computer Science Series. Palgrave Macmillan Slavomı́r Šimoňák received his M.Sc. degree in computer
UK, 1990. ISBN: 978-0-333-51210-4, 978-1-349-21151-7.
science in 1998 and his Ph. D. degree in computer tools
[21] ZAVERI, M.: An intro to Algorithms: Searching and systems in 2004, both from the Technical University of
and Sorting algorithms. codeburst.io. Mar. 2018. url: Košice, Slovakia. He is currently Associate Professor at the
https://codeburst.io/algorithms-i-searching- Department of Computers and Informatics of the Faculty
and-sorting-algorithms-56497dbaef20 of Electrical Engineering and Informatics at the Technical
[22] SEDGEWICK, R.: Algorithms in C, Parts 1-4: Fundamen- University of Košice, Slovakia. His research interests in-
tals, Data Structures, Sorting, Searching, 3rd Edition. Addi- clude formal methods integration and application, commu-
son Wesley, 1998. ISBN 9780201350883, 0201350882. nication protocols, algorithms, and data structures.

ISSN 1335-8243 (print) c 2020 FEI TUKE ISSN 1338-3957 (online), www.aei.tuke.sk

You might also like