









Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Material Type: Notes; Class: PROGRAMMING IN JAVA; Subject: Computer Science; University: Rensselaer Polytechnic Institute; Term: Spring 2005;
Typology: Study notes
1 / 17
This page cannot be seen from the preview
Don't miss anything!










-^
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++
-^
-^
-^
Superfluous support for heterogeneous collections
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”
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);
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);
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
or a positive value if this is less than, equal to, or greater than orespectively
public interface
Comparable {
int
compareTo(Object o);
}
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);
}
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
Available methods in a Map– put(Object key, Object value);– putAll(Map map);– get(Object key);– remove(Object key);
-^
-^
-^
-^