

































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


































Hold a group of objects Significantly increase your programming power All perform bound checking array: efficient, can hold primitives Collection: a group of individual elements List, Set Map: a group of key-value object pairs HashMap Misleading: sometimes the whole container libraries are also called collection classes
Can only take object Have to “wrap” primitives int - > Integer, double-> Double Have to cast or unwrap on retrieval Slow, error prone, tedious…. Fixed by JDK1.5, hopefully Advantage: automatic expanding
In java.util, a “wrapper” class for array A set of static utility methods fill(): fill an array with a value equals(): compare two arrays for equality sort(): sort an array binarySearch(): find one element in a sorted array All these methods overload for all primitive types and Object
sort(array), sort(array, fromIndex, toIndex) All elements in the array must implement the Comparable interface sort(array, comparator) sort(array, fromIndex, toIndex, comparator) All elements in the array must be mutually comparable by the specified comparator
With a single method compareTo() Takes another Object as argument And returns: Negative value if this is less than argument Zero value if this is equal to argument positive value if this is greater than argument
Only usable on sorted array! Otherwise, result unpredictable If there are multiple elements equal to the specified object, there is no guarantee which one will be found. Return: Location if find the key (positive number) (-( insertion point ) - 1) if not find key (negative)
Collection List ArrayList LinkedList Vector Set HashSet LinkedHashSet TreeSet Collection: hold one item at each location List: items in order Set: no duplicates, no ordering Preserve the insertion of the elements
Map HashMap LinkedHashMap Hashtable TreeMap Map: key-value pairs, fast retrieval no duplicate keys, no ordering Preserve the insertion of the elements
An array that automatically expand itself Put objects using add() Get out using get(int index) Need to cast type Method size() to get the number of objects Similar to .length attribute of array Example: CatsAndDogs.java
Access method regardless of the underlying structure Generic programming Can change underlying structure easily “light-weight” object Cheap to create Can move in only one direction
ArrayList Rapid random access Slow when inserting or removing in the middle LinkedList Optimal sequential access Fast insertion and deletion from the middle addFirst(), addLast(), getFirst(), removeFirst() Easy to be used as queue, stack
Each element add to the Set must be unique , otherwise won’t add. Objects added to Set must define equals() to establish object uniqueness Not maintain order