Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

JAVA collections related

  Date: Nov 24    Category: Java    Views: 379

Notes on Collections

Collections is a new subject for the Java Certification
exam. This is a new Java 1.2 facility that is positioned
as a very important part of the language that will provide
the means to manipulate collections of objects in very
useful ways.

Collections is a partical replacement for Arrays, Vectors,

The Java programmer isn't expected to set up their own
linked lists, set operations and other data structures as
good, optimized prebuilt facilities are already part of the
language in Collections.

You are expected to be familiar with the Collections
framework. There has been a change in the emphasis in
that newer books printed by persons involved with creating
the exam are putting more page space on Collections in
more recent publications. For example, the books by
Simon/Heller on Java 2 Certification published last year
in 2000, printed 10 pages out of 900 pages on Collections
and put it in the back of the chapter on utilities.

In a book printed this year in 2001, Simon/Heller devoted an
entire chapter with more pages to the subject even tho the
new book had only 250 pages.

Of the 59 questions on the exam, I'm expecting perhaps
3 to be on Collections. You will not need to be intimately
familiar with Collections, just have a good solid understanding
of the basics, an appreciation for what they will do. and
perhaps a smattering of the types of Collections and
a little about their methods. You might be presented with
a question about which Collection would be good in particular

The emphasis on Collections is thought to be an indication
of how useful the new facility is expected to be. One should
become familiar with Collections and try to use them in their
work. Of couse if you have to support legacy systems which
only work with Java 1.1 and below, this will not be possible.


Collections come from java.util.Collection. Collections are
easier to use than Arrays because the number of elements
in Collections is expandable. But Collections can only work
with objects, it can not work with primatives like arrays.

If you need to use numbers, you will have to use the wrapper
classes for int (Integer), char (Character), long (Long) et cetera.

You shouldn't need to use the older Vector and Hashtable
class as there is a richer functionality in the new
Collection class. Actually, I've found the Vector class to be
unreliable, in that I've put in data in objects that confuses
the Vector logic. I don't know if this weakness is in Collections
or not. I hope not.

Vocabulary: a collection is also referred to as a collection
framework, also as a bag and as a multiset.


Types of Collections

Simple collection - no requirement for order or contraints on
&n bsp; duplication, type or repetition.
List collection - elements in the Collection has an order to
&n bsp; them. Can be an order of any type, such as
&n bsp; value, order added to the List etc.
Set collection - the Collection is contrained so that it can
&n bsp; not contain duplicate values. Nulls may
&n bsp; or may not be OK, but can occur only once
&n bsp; if present.
Map collections - is a set of paired data, one part being the
&n bsp; index, the other part being the object. This
&n bsp; data structure is good for small databases.
&n bsp; The key must be unique. For example if
&n bsp; you were putting up a list of persons, you
&n bsp; probably couldn't use their name as their
&n bsp; could be a duplicate John Smith for example.
&n bsp; However, a social security number or perhaps
&n bsp; an email address could be used as the key.


Vocabulary recap:

Collection - a bunch of objects with no special order or restriction on duplicates
List - a bunch of objects that are ordered in some way but can have duplicates
Set ; - a bunch of objects that has no special order, but can't have duplicates
Map - a bunch of key/data pairs with a unique key for each datum.

You can combine the types and get ordered, unique Collections, but this
will not be tested on. You only need to have a basic understanding of the
four types listed above.


For purposes of the exam there are fours ways to store data in Collections:

Array storage - fast, but inefficient when large number of elements
&n bsp; need to be inserted and deleted. There is no special
&n bsp; search function provided. Good choice when elements
&n bsp; are ordered, don't change much, and don't need to be
&n bsp; searched that often.
Linked list - elements can easily be inserted or removed at any
&n bsp; point. Size can grow easily. Slower to access by index
&n bsp; however. Easy to find subsets, but possibly inefficient.
Tree - elements can be easily added/deleted at any point.
&n bsp; However, trees insist that elements be ordered. Generally
&n bsp; more efficient than arrays or linked lists. Can have
&n bsp; problems when data in trees are imbalanced.
Hash table - a unique key is associated with each datum, this
&n bsp; provides for quick searching. Easy to access data and
&n bsp; easy to insert new data. Generally overkill for
&n bsp; Collections with a small number of elements.


Requirements for using Collections

For an object class to work properly in a Collection framework, the
object must implement a useful equals( ) method. This is so you can
search for things.

Searching an ordered list might require that the object in the
Collection implement the Comparable interface, which has the
compareTo( ) method specified, which should be able to compare
two objects and determine the proper order that they should appear

If using a Map Collection, there must be a correct implementation
of the hashtable( ) method of the object.


The main virtue of using Collections is that it is a framework of
data structures that are constructed so that you can replace one
data structure with another if it will work better. And have very
little coding work to do. They all are able to substitutue for the
other if you use the interface in the creation of the structure
like this:

Map map = new TreeMap( ); // notice the interface
&n bsp; &nbs p; &n bsp; // for the object pointer
&n bsp; &nbs p; &n bsp; // and not TreeMap

Now you can substitute this:

Map map = new HashMap( ); // notice the use of the intertace Map

And all your code will work the same, but your data will be
stored differently.



No Answers Found. Be the First, To Post Answer.

Didn't find what you were looking for? Find more on JAVA collections related Or get search suggestion and latest updates.