Sie sind auf Seite 1von 5

Data Structures in Java: Part 2, What Is a Collection?

Baldwin explains some of the details surrounding the use of a Java collection fo
r creating data structures. He also discusses the interfaces and the concrete i
mplementations in the Java Collections Framework.
Published: May 14, 2001
By Richard G. Baldwin
Java Programming, Lecture Notes # 1352
Preface
Preview
Introduction
Discussion
Summary
What's Next
Preface
This is the second lesson in a miniseries on Java data structures and the Java C
ollections Framework. The first lesson in the miniseries was entitled Data Stru
ctures in Java: Part 1, Getting Started.
The purpose of this miniseries is to help you learn the essential features of Ob
ject-Oriented data structures in Java using the Collections Framework.
Supplementary material
I recommend that you also study the other lessons in my extensive collection of
online Java tutorials. You will find those lessons published at Gamelan.com. H
owever, as of the date of this writing, Gamelan doesn't maintain a consolidated
index of my Java tutorial lessons, and sometimes they are difficult to locate th
ere. You will find a consolidated index at Baldwin's Java Programming Tutorials
.
The index on my site provides links to the lessons at Gamelan.com.
Preview
Collection is not only the name of a Java interface, it is also the term given t
o an object that groups multiple elements into a single unit.
In this lesson, I will discuss how elements are stored in a collection, and why
it is often necessary to use a downcast when retrieving and processing those ele
ments.
I will discuss the advantages of passing collections between methods as type Col
lection.
I will summarize the core interfaces in the Collections Framework and show you h
ow they are related.
I will very briefly discuss the concrete implementations of the interfaces that
are provided by the framework. (I will discuss those implementations in much mor
e detail in subsequent lessons.)
And finally, I will introduce you to the three kinds of things that are part of
a collections framework.
Introduction
The previous lesson, entitled Data Structures in Java: Part 1, Getting Started,
introduced you to the use of the Java Collections Framework for building and usi
ng data structures. This lesson will continue down that path.
Discussion
What is a collection?
Just to see if you are awake today, let's start with a little quiz.
What is a collection insofar as Java programming is concerned?
A. Something they gather in plates at church.
B. An object that groups multiple elements into a single unit.
C. The name of a Java interface.
D. None of the above.
If you answered A, you are probably reading an article on the wrong website by m
istake. If you answered Both B and C, then you are off to a very good start on
today's lesson.
Collection is the name of a Java interface. This interface is an integral part
of the Java Collections Framework. In fact, as of JDK 1.3, it is one of two top
-level interfaces in the framework. The other top-level interface in the framew
ork is named Map.
According to The Java Tutorial from Sun, a collection (sometimes called a contai
ner) is also an object that groups multiple elements into a single unit.
Typical collection objects might contain information about employees in the comp
any telephone book, all the purchase orders issued during the past year, or the
transactions occurring in a person's checking account.
Slightly different terminology
Note that this terminology may be somewhat different from what you are accustome
d to. For example, if you speak of your coin collection, you are probably speak
ing about the actual coins rather than the container that holds the coins.
This is an important distinction. The usage of the term collection in the Colle
ction Framework usually refers to the container and not to the contents of the c
ontainer. In the framework, the contents are usually referred to as elements.
Store references rather than objects
The collections in the framework always store references to objects, rather than
storing the objects themselves. One consequence of this is that primitive valu
es cannot be stored in a collection without first encapsulating them in an objec
t. (Standard wrapper classes are provided for encapsulating all primitive types.
)
Stored as type Object
Furthermore, the references are always stored as type Object. Thus, when you re
trieve an element from a collection, you frequently need to downcast it before y
ou can gain access to the members of the object to which the reference refers.
Moving data among methods
In addition to their use for storing, retrieving, and manipulating data, collect
ions are also used to move data among methods.
One of the primary advantages of the Collections Framework is the ability to pas
s a collection to a method as the generic interface type Collection. The receiv
ing method doesn't need to know the actual type of the object referred to by the
incoming reference in order to invoke its methods.
Polymorphic behavior
The receiving method can invoke (on the reference to the Collection object) any
of the methods declared in the Collection interface, with confidence that the be
havior of the method will be appropriate for the actual type of Collection objec
t involved. (That is polymorphic behavior.)
Core collection interfaces
If you have been working with the framework, you might be inclined to think that
all of the interfaces in the following list are members of the core collection
interfaces.
Collection
Set
List
SortedSet
Map
SortedMap
Iterator
However, that is not the case.
While the Iterator interface is heavily used in conjunction with collections, ac
cording to The Java Tutorial from Sun, it is not one of the core collection inte
rfaces.
The core collection interfaces identified by the Sun book are shown below, with
indentation showing the superinterface-subinterface relationships among the inte
rfaces.
Collection
Set
SortedSet
List
Map
SortedMap
As you can see, as mentioned earlier, Collection and Map are the two top-level i
nterfaces.
You should probably commit the above list of interfaces and their relationships
to memory. You might find that helpful when navigating the Sun documentation.
Concrete implementations
In addition to interfaces, the framework provides several concrete implementatio
ns of the interfaces defined in the framework. (A concrete implementation is a
class that implements one or more of the interfaces.)
Are you still awake? If so, see if you can answer the following question.
True or False? Each of the following classes provides an implementation of one
of the interfaces that make up the Java Collections Framework. If False, which
items don't belong in the list.
AbstractSet
AbstractList
AbstractMap
HashSet
TreeSet
LinkedList
Vector
ArrayList
HashMap
Hashtable
WeakHashMap
TreeMap
Iterator
Attributes
RenderingHints
Hopefully your answer was False, but even so, that isn't the complete answer.
Iterator is not a class
To begin with, Iterator is not a class. I told you that a couple of paragraphs
back. It is an interface. Therefore, it has no place on the above list of clas
ses.
What about Attributes and RenderingHints?
You may also have wondered if the classes named Attributes and RenderingHints be
long on the list. Note that I didn't restrict the above list to only those clas
ses that might be considered part of the framework, so this was sort of a trick
question. (Of course you could have looked them up in the Sun documentation jus
t like I did.)
While these two classes are not really a part of the core Java Collections Frame
work, they do implement interfaces that are part of the framework.
The RenderingHints class implements the Map interface, and is used in conjunctio
n with the Graphics2D class. The Attributes class also implements the Map inter
face,
What is a Collections Framework?
According to The Java Tutorial from Sun, "A collections framework is a unified a
rchitecture for representing and manipulating collections. All collections frame
works contain three things."
Those three things are:
Interfaces
Implementations
Algorithms
This is probably a good place to close off the discussion for this lesson. The
next lesson will take up at this point, providing a more in-depth discussion of
the interfaces, implementations, and algorithms that make up the framework.
Summary
I started out by telling you that a collection is not only the name of a Java in
terface (Collection) but is also an object that groups multiple elements into a
single unit.
Java Collection objects don't store objects or primitive values directly. Rathe
r, they store references to objects. Further, all such references are stored as
the type Object. This often leads to the later need to downcast the reference
in order to gain access to the members of the object to which it refers.
If you need to store primitive values in a collection, you will first need to wr
ap those values in appropriate objects. Standard wrapper classes are provided f
or all the primitive types.
Collections are not only useful for storing and retrieving data, they are also u
seful for moving data among methods.
Because a collection can be passed to a method as type Collection, all of the me
thods declared in the Collection interface can be invoked on the incoming refere
nce in a polymorphic manner.
In addition to the interfaces defined in the Collections Framework, the framewor
k also provides various concrete implementations of the interfaces for most of t
he commonly-used data structures. This makes it possible for you to convenientl
y use the framework without the requirement to define new Collection classes.
As of JDK 1.3, there are six core interfaces in the framework. Although the Ite
rator interface is often used with collections, it is not one of the core interf
aces.
I ended the lesson by telling you that there are basically three things in a col
lections framework: interfaces, implementations, and algorithms.
What's Next?
In the next lesson, I will
Discuss the purpose of interfaces, implementations, and algorithms in a coll
ections framework
Provide some additional discussion about polymorphic behavior in the Java Co
llections Framework
Discuss some of the advantages of using the framework to satisfy your need f
or data structures.
Copyright 2000, Richard G. Baldwin. Reproduction in whole or in part in any for
m or medium without express written permission from Richard Baldwin is prohibite
d.
About the author
Richard Baldwin is a college professor and private consultant whose primary focu
s is a combination of Java and XML. In addition to the many platform-independent
benefits of Java applications, he believes that a combination of Java and XML w
ill become the primary driving force in the delivery of structured information o
n the Web.
Richard has participated in numerous consulting projects involving Java, XML, or
a combination of the two. He frequently provides onsite Java and/or XML traini
ng at the high-tech companies located in and around Austin, Texas. He is the au
thor of Baldwin's Java Programming Tutorials, which has gained a worldwide follo
wing among experienced and aspiring Java programmers. He has also published arti
cles on Java Programming in Java Pro magazine.
Richard holds an MSEE degree from Southern Methodist University and has many yea
rs of experience in the application of computer technology to real-world problem
s.
baldwin.richard@iname.com
-end-

Das könnte Ihnen auch gefallen