Friday, November 20, 2015

Java Collections Framework (With Pictures!)

Save the #TreeSets.




Before I can show you my fun implementing in Java of some math problems that have been puzzling many number theorists, it is imperative to understand collection interfaces. These are basically tools to organize data and are constructed so that the programmer can manipulate them to fit their code.

List Interface

Unique features of lists in Java:
  • Allowance of duplicates.
  • Dynamic searching of elements by positional values.


ArrayList
In Java, the ArrayList is a general implementation of lists. Similar to Arrays or Lists in other programming languages, ArrayLists in Java can hold primitive values or objects through storing them by indices, with the first index as always 0. ArrayList size can also be changed dynamically. Though ArrayLists permits all elements, including null, there's probably a more efficient way of doing that.
LinkedList
Similar to ArrayLists, LinkedLists provide similar functionality for lists of elements, but through different implementation. Generally, LinkedLists should be preferred if using them to add or remove many elements without needing a large number of random access to those elements.
Vector
Vectors share similarities with both the above. Vector list size can change dynamically, includes support for all elements, as implementations of Queue. Vectors are synchronized, meaning only one thread which is accessing a Vector can be active. Vectors are rather antiquated, and generally do not need to be worried about.

Set Interface

Modeling its mathematical namesake, here are some unique features of Sets in Java:

  • No duplication of elements, and at most one null element.
  • Mutable objects beware!


HashSet

HashSets store elements using a hash function (basically, a key-and-value system), meaning it assigns a key to each bucket, or slot, for each element to reside. HashSets guarantee no order, but can be used to quickly ignore duplicate elements, since it is the most notable and fastest operating set.
LinkedHashSet
LinkedHashSets store elements by order of insertion, also known as natural order.
TreeSet
TreeSets store elements in order on the basis of their values, or you can specify a comparator for custom ordering. Because of this, TreeSets are slower than HashSets.


Map Interface

Java's Maps and its features can be compared to Python's Dictionaries:

  • Assigns a key to a value.
  • Each key can have at most one value.
  • A map cannot contain duplicate keys, but duplicate values may be assigned to separate keys.


HashMap
A HashMap is an implementation of Map that allows all elements as keys and values, including null. HashMaps cannot guarantee order, especially the consistency of the order over time.
LinkedHashMap
The main benefit of LinkedHashMaps is in the less-chaotic method of ordering than HashMaps; they use natural order.
TreeMap
This type of map uses natural ordering of keys unless specified by a comparator.


Below is a flow chart of when to use what when it comes to the data you are going to be handling.



I hope this helps with the understanding of these concepts. These tend to be generalized topics in computer science, across all languages, though the specifics differ. Soon, I'll post about a special kind of problem that has gauged my interest.

Resources