Adv_Java_Module-01[1]
Adv_Java_Module-01[1]
• Package java.util.
• In this lecture we will survey the interfaces,
abstract classes and classes for linear data
structures provided by the Java Collections
Framework.
• We will not cover all of the details (e.g., the
exceptions that may be thrown).
Collection interfaces
• The core collection interfaces encapsulate
different types of collections.
• They represent the abstract data types that
are part of the collections framework. They
are interfaces so they do not provide an
implementation!
Traversing Collections in Java
• There are two ways to traverse collections:
– using Iterators.
• standardized way of accessing the elements
within a collection, one at a time.
• Thus, an iterator provides a means of
enumerating the contents of a collection.
• Because each collection implements Iterator, the
elements of any collection class can be accessed
through the methods defined by Iterator.
– with the (enhanced) for-each construct
MAP
• In addition to collections, the framework
defines several map interfaces and classes.
• Maps store key/value pairs. Although maps
are part of the Collections Framework, they
are not “collections” in the strict use of the
term.
• You can, however, obtain a collection-view of a
map.
• java.util contains a wide array of functionality,
it is quite large. Here is a list of its classes:
• The interfaces defined by java.util are shown
next:
Collections Overview
• The Collections Framework was designed to meet several
goals
• First, the framework had to be high-performance. The
implementations for the fundamental collections (dynamic
arrays, linked lists, trees, and hash tables) are highly
efficient.
• Second, the framework had to allow different types of
collections to work in a similar manner and with a high
degree of interoperability
• Third, extending and/or adapting a collection had to be
easy.
• Finally, mechanisms were added that allow the integration
of standard arrays into the Collections Framework
• Algorithms are another important part of the
collection mechanism.
• Algorithms operate on collections and are
defined as static methods within the
Collections class.
• Thus, they are available for all collections.
Each collection class need not implement its
own.
Iterators.
• standardized way of accessing the elements
within a collection, one at a time.
• Thus, an iterator provides a means of
enumerating the contents of a collection.
• Because each collection implements Iterator, the
elements of any collection class can be accessed
through the methods defined by Iterator.
MAP
• In addition to collections, the framework defines
several map interfaces and classes.
• Maps store key/value pairs. Although maps are
part of the Collections Framework, they are not
“collections” in the strict use of the term
Recent Changes to Collections
• Generics Fundamentally Change the Collections Framework
– Generics add the one feature that collections had been missing:
type safety
– All collections are now generic,and many of the methods that
operate on collections take generic type parameters.
• Autoboxing Facilitates the Use of Primitive Types
– a collection can store only references, not primitive values. In
the past, if you wanted to store a primitive value, such as an int,
in a collection, you had to manually box it into its typewrapper
– it needed to be manually unboxed (by using an explicit cast) into
its proper primitive type
– There is no need to manually perform these operations since
java do it automatically
• The For-Each Style for Loop
The Collection Interfaces
Collection Interface
Declaration: