Java Collections Framework (with Cheat Sheet)

Topics interesting only for Java programmers

Link to this posting

Postby Ursego » 20 May 2019, 09:34

Copy the following fragment to a text file with .java extension.

To enjoy keywords coloring, open this file in Eclipse. After that, enable word-wrap by pressing Alt + Shift + Y (otherwise you will need to scroll right to read long comments). Set the code editor width so the very first line of the file (with asterisks) fits one line (i.e. is not broken into 2 lines).

You can also open this file in Notepad++. It will understand by the file extension (.java), that it's Java code, and paint keywords. After that, enable word-wrap by marking the menu option View > Word wrap.

In any editor, use the Fixedsys font (as I did) to see straight vertical alignment.

Code: Select all
// It is a set of classes in java.util for storing collections. There are four main interfaces in the Java Collections Framework:
// * List: an ordered collection of elements that ALLOWS DUPLICATE ENTRIES. Elements in a list can be accessed by an int index.
// * Set: a collection that DOES NOT ALLOW DUPLICATE ENTRIES.
// * Queue: a collection that orders its elements in a specific order for processing. A typical queue processes its elements in a first-in, first-out order, but other orderings are possible.
// * Map: a collection that maps keys to values, with no duplicate keys allowed. The elements in a map are key/value pairs.

// List, Set and Queue implement the Collection interface:
public interface Collection<E> extends Iterable<E>
// Notice that Map doesn't implement the Collection interface. It is considered part of the Java Collections Framework, even though it isn't technically a Collection. It is a collection (note the lowercase), though, in that it contains a group of objects. The reason why maps are treated differently is that they need different methods due to being key/value pairs.

// Collections Methods:
// The Collection interface contains useful methods for working with lists, sets, and queues.
boolean add(E element) // inserts a new element into the Collection and returns whether it was successful
boolean remove(Object object) // removes a single matching value in the Collection and returns whether the element was removed
boolean isEmpty()
int size() // returns the number of elements in this collection
void clear() // removes all of the elements from this collection; after it, isEmpty() will return true, and size() - zero.
boolean contains(Object object) // returns true if this collection contains the specified element; calls equals() on each element of the ArrayList to see if there are any matches

// List Methods:
void add(E element) // Adds element to end
void add(int index, E element) // Adds element at index and moves the rest toward the end
E get(int index) // Returns element at index
int indexOf(Object o) // Returns first matching index or -1 if not found
int lastIndexOf(Object o) // Returns last matching index or -1 if not found
void remove(int index) // Removes element at index and moves the rest toward the front; if the index doesn't exist, throws IndexOutOfBoundsException; overloads the method of Collection interface
E set(int index, E e) // Replaces element at index and returns original

// Map Methods:
// Since there are both keys and values, we need generic type parameters for both. The class uses K for key and V for value.
void clear() // Removes all keys and values from the map.
boolean isEmpty() // Returns whether the map is empty.
int size() // Returns the number of entries (key/value pairs) in the map.
V get(Object key) // Returns the value mapped by key or null if none is mapped.
V put(K key, V value) // Adds or replaces key/value pair. Returns previous value or null.
V remove(Object key) // Removes and returns value mapped to key. Returns null if none.
boolean containsKey(Object key) // Returns whether key is in map.
boolean containsValue(Object) // Returns value is in map.
Set<K> keySet() // Returns set of all keys.
Collection<V> values() // Returns Collection of all values.

Java Collections Cheat Sheet:


User avatar
Site Admin
Posts: 141
Joined: 19 Feb 2013, 20:33

Ketones are a more high-octane fuel for your brain than glucose. Become a biohacker and upgrade yourself to version 2.0!

Traffic Counter

eXTReMe Tracker