Java Reference
In-Depth Information
K.2
The
java.util
package
The
java.util
package is a relatively incoherent collection of useful classes and interfaces.
package java.util-Summary of the most important classes and interfaces
This interface provides the core set of methods for most of the collection-based classes defined in the
java.util
package, such as
ArrayList
,
HashSet
, and
LinkedList
. It defines signatures for
the
add
,
clear
,
iterator
,
remove
, and
size
methods.
interface
Collection
interface
Iterator
Iterator
defines a simple and consistent interface for iterating over the contents of a collection. Its
three methods are
hasNext
,
next
, and
remove
.
interface
List
List
is an extension of the
Collection
interface and provides a means to impose a sequence
on the selection. As such, many of its methods take an index parameter: for instance,
add
,
get
,
remove
, and
set
. Classes such as
ArrayList
and
LinkedList
implement
List
.
interface
Map
The
Map
interface offers an alternative to list-based collections by supporting the idea of associat-
ing each object in a collection with a
key
value. Objects are added and retrieved via its
put
and
get
methods. Note that a
Map
does not return an
Iterator
, but its
keySet
method returns a
Set
of
the keys, and its
values
method returns a
Collection
of the objects in the map.
interface
Set
Set
extends the
Collection
interface with the intention of mandating that a collection contain no
duplicate elements. It is worth pointing out that, because it is an interface,
Set
has no actual impli-
cation to enforce this restriction. This means that
Set
is actually provided as a marker interface to
enable collection implementers to indicate that their classes fulfill this particular restriction.
class
ArrayList
ArrayList
is an implementation of the
List
interface that uses an array to provide efficient direct
access via integer indices to the objects it stores. If objects are added or removed from anywhere
other than the last position in the list, then following items have to be moved to make space or close
the gap. Key methods are
add
,
get
,
iterator
,
remove
, and
size
.
class
Collections
Collections
contains many useful static methods for manipulating collections. Key methods are
binarySearch
,
fill
, and
sort
.
class
HashMap
HashMap
is an implementation of the
Map
interface. Key methods are
get
,
put
,
remove
, and
size
. Iteration over a
HashMap
is usually a two-stage process: obtain the set of keys via its
keySet
method, and then iterate over the keys.
class
HashSet
HashSet
is a hash-based implementation of the
Set
interface. It is closer in usage to a
Collection
than to a
HashMap
. Key methods are
add
,
remove
, and
size
.
class
LinkedList
LinkedList
is an implementation of the
List
interface that uses an internal linked structure to store
objects. Direct access to the ends of the list is efficient, but access to individual objects via an index
is less efficient than with an
ArrayList
. On the other hand, adding objects or removing them from
within the list requires no shifting of existing objects. Key methods are
add
,
getFirst
,
getLast
,
iterator
,
removeFirst
,
removeLast
, and
size
.
class
Random
The
Random
class supports generation of pseudo-random values—typically, random numbers. The
sequence of numbers generated is determined by a
seed value
, which may be passed to a con-
structor or set via a call to
setSeed
. Two
Random
objects starting from the same seed will return
the same sequence of values to identical calls. Key methods are
nextBoolean
,
nextDouble
,
nextInt
, and
setSeed
.
class
Scanner
The
Scanner
class provides a way to read and parse input. It is often used to read input from the
keyboard. Key methods are
next
and
hasNext
.
Search WWH ::
Custom Search