Collection : Java Glossary

*0-9ABCDEFGHIJKLMNOPQRSTUVWXYZ (all)

Morpho butterfly  Collection
Introduction Books
Gotchas Learning More
Examples of Collections Links
Alternatives

Introduction

You will see the term used in three contexts:

  1. A collection in data structure terms, is a group of elements. It includes Sets and Lists. It also includes Maps which don’t implement Collection.
  2. Collection is the interface implemented by the collection classes such as ArrayList, TreeSet and HashSet implement. It defines the methods common to them all.
  3. Collections is a class of utility methods for working on collections, e.g. sorting and binary search, even though they only work on Lists.
Most of the Collection classes live in the java.util package.

In Java version 1.5 or later the Collection classes are generified to keep track of the class of the elements permitted to be contained in them.

All classes that implement Collection automatically implement the superinterface Iterable, allowing you to iterate over all their elements.

Gotchas

Examples of Collections

Some types of collections that implement Collection include the following:
Collection Classes and Interfaces
Class Implements
or Extends
Collection
Interface
Interface or Collection Synchronised Notes
AbstractCollection abstract class   an interface to act as skeleton to build most classes that implement the Collection, interface.
java.lang.reflect.Array class   Reflection classes used to dynamically create Array objects where you don’t know anything about them at compile time. You probably were looking for Arrays instead.
java.sql.Array interface   A class for storing and retrieving arrays in an SQL (Standard Query Language) database. You probably were looking for Arrays instead.
ArrayList class A non-thread-safe List that is marginally faster than thread-safe Vector. In versions prior to  Java version 1.4, the difference was more pronounced — 3 to 4 times faster. Rolling your own non-Collection array is about 4 times faster than Vector. In earlier non-optimising JVM (Java Virtual Machine) s, the difference was more like 40 times faster. The main overhead of using ArrayList and Vector is the casting needed to fish out the elements. ArrayList is like a [] array that automatically grows. Lookup by dense ints. Considerably faster than a HashMap.
Arrays class static methods for sorting and searching ordinary arrays, e.g. binarySearch, fill, sort. asList converts an array into a List Collection you can then feed to methods expecting a Collection or List. Don’t confuse this with the two Array classes.
Collection interface The interface that all collections implement that guarantee you can always add, remove, clear, size, iterator, contains, toArray etc. in a standard way. Don’t confuse this with the Collections class. toArray can be made smarter if you pass it an empty array to fill just the right size. Then you have an array of specific classes, not just generic Objects. Use it like this:
The Collection. toString method implemented by all Collections is a great debugging tool to dump out the entire contents of a Collection in a reasonably human-readable format enclosing each element in [].
Collections class static methods for operating on collections, e.g. sort, binarySearch, min, max, shuffle. Don’t confuse the Collections class with the Collection interface.
javax.swing.tree.DefaultTreeModel class This was written to support Swing’s JTree, but it will suffice as a generic tree collection.
HashMap class a Map, an unsynchronized Hashtable. Allows unordered lookup by key, usually a String. It is thread safe if you only do lookups. Otherwise you must synchronise externally, or use a Hashtable. No duplicates allowed.
HashSet class a Set, a collection of unique objects, without ability to lookup by key, usually a String. Not ordered. Often used to maintain a list of legal values, e.g. state abbreviations. You can quickly find out if a given value is in the legal list.
Hashtable class a Map, a synchronised HashMap. Allows unordered lookup by key, usually a String. Note, this is spelled Hashtable not Hash Table.
IdentityHashMap class It is like a Hashtable. Used in manipulating graphs where you have to keep track of which nodes have already been visited. In Java version 1.4 or later . It compares with == instead of equals.
LinkedHashMap class a HashMap that also threads the objects together, usually in insertion order. This way you can retrieve the elements in insertion order with almost no more overhead than a HashMap. In Java version 1.4 or later.
LinkedList class a List that behaves like Vector, but implemented as a doubly linked chain of objects. Fast for insert/delete. Slow for indexing.
List interface   specifies an ordered sequence of elements. Implemented by ArrayList, Vector and LinkedList.
Map interface   supports lookup of objects by unique key. Keys may or may not be ordered.
Queue interface   Java version 1.5 or later. Used for enqueing objects waiting to be processed. Has some very complex multi-thread abilities. It might be looked on more as threading synchronisation package than a collection.
RandomAccess interface   An interface does not do anything. It has no methods. It is just a marker that lets you know random access to a List is efficient, e. g. LinkedList would not implement it, whereas ArrayList would. In  Java version 1.4 or later.
Set interface   a mathematical set of elements, with no duplication. No lookup by key.
SortedMap interface   Map interface that guarantees sorted keys, usually Strings. implemented by TreeMap.
SortedSet interface   Set interface that guarantees iteration will be in ascending order. Implemented by TreeSet
Stack class   pushdown stack, a LIFO (Last In First Out). You can also fudge a simple LIFO stack with LinkedList using use only addFirst, removeFirst.
TreeMap class a SortedMap using a red-black tree. Allows ordered lookup by key, usually Strings. Does not allow duplicate keys, unless you cheat to fool it into thinking they are not really duplicates. Slower than HashMap.
TreeSet class a SortedSet using a red-black tree. Allows ordered access, but no access by key. Slower than HashSet.
Vector class a List. This is the same old Vector you are familiar with, now implementing Collection. It acts like a []- style array that automatically grows as needed. It is a synchronised ArrayList, i.e. suitable for use when more than one thread accesses the Vector.

Missing Collections

Collections that you would expect to find in Java prior to 1.5 but which are not included are: In Java version 1.5 some of these deficiencies were rectified with the Queue interface.

Duplicate Keys

Nearly all the collections prevent you from storing duplicate keys. There are several ways around that:

The new java.lang.Comparable interface makes it easier to sort Collections. Even String implements it.

Even if you are stuck with Java version 1.1, you can still retrofit some of the 1.2 JDK (Java Development Kit) Collections classes. You won’t be able to use any of the natural ordering methods since they require adding the java.lang. Comparable interface to the core classes.

Alternatives

There are a number of techniques for returning multiple objects from a method.

Alternatives to Collections
Technique Advantages Disadvantages
Collection no conversion needed caller can modify the original copy of the Collection.
Collection.unmodifiableCollection caller cannot modify the original copy of the Collection. Use it with:
// Making collections unmodifiable
Collection<C> x =  Collections.unmodifiableCollection( someCollection );
List<V> x =        Collections.unmodifiableList( someMap );
Map<K,V> x =       Collections.unmodifiableMap( someMap );
Set<S> x =         Collections.unmodifiableSet( someSet );
SortedMap<K,V> x = Collections.unmodifiableSortedMap( someSortedMap );
java.util.List
The caller accesses the data through at extra layer of indirection. Changes made to the base collection are reflected is the unmodifiable collection. The unmodifiable collection is not immutable.
Iterator The elements can be generated as needed. They don’t all have to exist in RAM (Random Access Memory) at once. Somewhat awkward syntax to walk the Iterator.
array Can be serialised. Fast access. You must copy the entire dataset to create an array separate from the original copy so the caller cannot modify the original.
java.util.Stream Uses new terse lambda syntax Requires Java 1.8+.

Books

book cover recommend book⇒Java Generics and Collectionsto book home
by Maurice Naftalin & Philip Wadler 978-0-596-52775-4 paperback
publisher O’Reilly recommended 978-0-596-55150-6 eBook
published 2006-10-18 B0026OR2HM kindle
Covers both generics and Collections. Covers Java 1.5+ features such as autoboxing, for:each as well. Recommended by Mike Schilling.
Australian flag abe books anz abe books.co.uk UK flag
Chinese flag amazon.cn amazon.co.uk UK flag
German flag abe books.de abe books.ca Canadian flag
German flag amazon.de amazon.ca Canadian flag
Spanish flag amazon.es Chapters Indigo Canadian flag
Spanish flag iberlibro.com abe books.com American flag
French flag abe books.fr amazon.com American flag
French flag amazon.fr Barnes & Noble American flag
Italian flag abe books.it Google play American flag
Italian flag amazon.it O’Reilly Safari American flag
India flag junglee.com Powells American flag
UN flag Kobo other stores UN flag
book cover recommend book⇒Data Structures and the Java Collections Frameworkto book home
by William Collins 978-0-470-48267-4 paperback
publisher Wiley 978-1-118-13620-1 eBook
published 2011-01-11 B005FHM6FU kindle
This is about abstract data collections in general, not just the ones implemented in Java.
Australian flag abe books anz abe books.co.uk UK flag
Chinese flag amazon.cn amazon.co.uk UK flag
German flag abe books.de abe books.ca Canadian flag
German flag amazon.de amazon.ca Canadian flag
Spanish flag amazon.es Chapters Indigo Canadian flag
Spanish flag iberlibro.com abe books.com American flag
French flag abe books.fr amazon.com American flag
French flag amazon.fr Barnes & Noble American flag
Italian flag abe books.it Google play American flag
Italian flag amazon.it O’Reilly Safari American flag
India flag junglee.com Powells American flag
UN flag Kobo other stores UN flag
Greyed out stores probably do not have the item in stock. Try looking for it with a bookfinder.

Learning More

Oracle’s Technote Guide on Collections Summary : available:
Oracle’s Javadoc on Collection interface : available:
Oracle’s Javadoc on Collections class of utility methods : available:
Oracle’s Javadoc on Collections.binarySearch (Lists only) : available:
Oracle’s Javadoc on Collections.reverseOrder() : available:
Oracle’s Javadoc on Collections.reverseOrder(Comparator) : available:
Oracle’s Javadoc on Collections.shuffle (Lists only) : available:
Oracle’s Javadoc on Collections.sort (Lists only) : available:
Oracle’s Javadoc on Collections.synchronizedCollection : available:
Oracle’s Javadoc on Collections.unmodifiableCollection : available:

Oracle’s Javadoc on AbstractCollection class : available:
Oracle’s Javadoc on ArrayList class : available:
Oracle’s Javadoc on Arrays array utility methods : available:
Oracle’s Javadoc on HashMap class : available:
Oracle’s Javadoc on HashSet class : available:
Oracle’s Javadoc on Hashtable class : available:
Oracle’s Javadoc on IdentityHashMap class : available:
Oracle’s Javadoc on Iterable interface : available:
Oracle’s Javadoc on LinkedList class : available:
Oracle’s Javadoc on List interface : available:
Oracle’s Javadoc on Map interface : available:
Oracle’s Javadoc on Queue interface : available:
Oracle’s Javadoc on RandomAccess interface : available:
Oracle’s Javadoc on Set interface : available:
Oracle’s Javadoc on SortedSet interface : available:
Oracle’s Javadoc on TreeMap interface : available:
Oracle’s Javadoc on TreeSet interface : available:
Oracle’s Javadoc on Vector class : available:

Oracle’s Javadoc on Arrays.sort : available:
Oracle’s Javadoc on Collections.sort : available:

Oracle’s Javadoc on java.lang.reflect.Array class : available:
Oracle’s Javadoc on java.sql.Array class : available:
Oracle’s Javadoc on javax.swing.tree.DefaultTreeModel class : available:
Oracle’s Javadoc on Stream Interface : available:

This page is posted
on the web at:

http://mindprod.com/jgloss/collection.html

Optional Replicator mirror
of mindprod.com
on local hard disk J:

J:\mindprod\jgloss\collection.html
logo
Please the feedback from other visitors, or your own feedback about the site.
Contact Roedy. Please feel free to link to this page without explicit permission.
no blog for this page
IP:[65.110.21.43]
Your face IP:[54.92.232.155]
You are visitor number