Java Collections Framework - Lecture Notes | CSCI 2220, Study notes of Javascript programming

Material Type: Notes; Class: PROGRAMMING IN JAVA; Subject: Computer Science; University: Rensselaer Polytechnic Institute; Term: Spring 2005;

Typology: Study notes

Pre 2010

Uploaded on 08/09/2009

koofers-user-cs6
koofers-user-cs6 🇺🇸

10 documents

1 / 17

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Programming in Java
CSCI 2220
Collections
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Partial preview of the text

Download Java Collections Framework - Lecture Notes | CSCI 2220 and more Study notes Javascript programming in PDF only on Docsity!

Programming in Java

CSCI 2220

Collections

Java Collections Framework

-^

Consistent, well-defined API for maintaining groupsof objects in containers

-^

A set of interfaces and classes to representcontainers that also include support for manipulationoperations (i.e. adding objects, removing objects,etc.)

-^

The framework also includes built-in methods thatoperate on containers such as sorting, searching,etc.

-^

Somewhat Analogous to the STL framework in C++

Important Points

-^

All interfaces in the framework support collections of thegeneric type Object

-^

As we have seen, every class is a subclass of typeObject and therefore any object of any class can be usedwith any instance of the Collections Framework– Advantage?

-^

Superfluous support for heterogeneous collections

  • Disadvantage? -^

Need for type-casting

all the time

-^

Could cause other class casting problems especially when dealingwith the Set interface and the definition of a “duplicate object”

More Important Points

•^

Should be an obvious point by now!– Application developers can make generic use of

Collection, List, Set, and Map without knowing orcaring how they actually work

  • Polymorphism at work! -

we have seen this behavior

already in our Factory assignment

List example: The Vector class

•^

You should look at the Java API on Sun’s site for details onthe methods available in the Vector class as well as the otherclasses in the Collections framework (there are many!)

-^

As said before, the Vector class works like a resizable arrayunder the hood–

Instantiated with some initial capacity (can be custom defined)– If elements are added to the Vector that overflows the capacity, theinternal array is automatically resized by some customizable incrementvalue

//instantiate a Vector with the default capacityVector v1 = new Vector();//initial capacity of 100Vector v2 = new Vector(100);//initial capacity 100 and capacity increment of 100Vector v3 = new Vector(100, 100);

Basic Vector functionality

•^

Here is a non-exhaustive list of things you can do with Vectors

-^

Just like with Array objects, if you attempt to access a positionin the Vector that is outside of the range of its size, anArrayOutOfBoundsException will be thrown

Vector v = new Vector().. .//Access the capacity and size int

availableSpaces = v.capacity() – v.size(); //add an element at some position in the Vector//shifts all objects to the right downv.add(1, someObject);//overwrite an element at some positionv.set(1, someObject);//search for an element in the Vector int

index = v.indexOf(someObject); //remove an element at some specified positionv.remove(0);

SortedSet example: TreeSet

•^

A SortedSet is even more restricted than a Set in the sensethat the objects must be represented in some logical order

-^

There are two ways to do this, we will use the TreeSet classwhich implements SortedSet to demonstrate– The TreeSet can be instantiated with the default constructor.

When this is done, any objects added to the Set shouldimplement the Comparable interface

  • The compareTo(..) method should return a negative value, zero,

or a positive value if this is less than, equal to, or greater than orespectively

public interface

Comparable {

int

compareTo(Object o);

}

SortedSet example: TreeSet

•^

The TreeSet can also be instantiated with an object thatimplements the Comparator interface

-^

The compare(..) function works similarly to the compareTomethod in the Comparable interface as it should return anegative if o1 < o2, a zero if they are equal, and a positivevalue if o1 > o

-^

When objects are then added to the TreeSet, they arecompared by passing them to the compare(..) function of theComparator

public interface

Comparator {

int

compare(Object o1, Object o2); boolean

equals(Object o);

}

Maps

•^

The basic concept behind Maps is that instead of associatedelements in the container with indexes of an array, you canassociate them user defined “key” objects

object

object

object

...

n^

objectn

Array or Vector

“dog”

object

“cat”

object

“pig”

object

...

“last”

objectn Map

Maps

•^

Available methods in a Map– put(Object key, Object value);– putAll(Map map);– get(Object key);– remove(Object key);

-^

Methods used to process elements in a Map– Set keySet();– Set entrySet();– Collection values();

Collections class

-^

The Collections class provides several static methodsthat can be used to operate on Collection objects

-^

These include methods for:– Sorting– Searching– Copying– Shuffling– Among others…

-^

Again look at the Java API for more details on thesemethods

Homework 4

• Homework 4 is posted online• Due February 23 at 11:55 PM• Homework 3 due tonight