An Introduction to Collections
in Java |
Prof. David Bernstein
|
Computer Science Department |
bernstdh@jmu.edu |
java.util.ArrayList
Classvoid add(int index, Object element)
Object get(int index)
int size()
ArrayList
Class (cont.)
java.util.ArrayList
Class (cont.)void add(Object element)
void clear()
boolean contains(int index, Object element)
int indexOf(Object element)
java.util.HashMap
ClassObject get(Object key)
Object put(Object key, Object element)
int size()
put()
Return:
null
)
if there was no such valueHashMap
Class (cont.)
add()
(in ArrayList
)
and put()
(in HashMap
):
Object
ArrayList
and a HashMap
can only
contain objectsint
, double
,
etc...Integer
:
Integer(int value)
int intValue()
Double
:
Double(double value)
double doubleValue()
HashMap
Object
that
uniquely identifies the value Object
Object
does not have
an attribute (or attributes) that can be used for
this purpose
HashMap
, but you can also use
a HashSet
HashSet
ArrayList
HashMap
We don't know all of the keys unless we keep them in another collection