Lecture 5 - Part 2 Collections and Exceptions
Lecture 5 - Part 2 Collections and Exceptions
Collections
Dequeue
Abstract
List
AbstractSet
Stack
Core Collection Interface
Collection - The root of the collection hierarchy. Its a group of objects known as its elements.
Java doesn’t provide any direct implementation of this interface but provides implementation
of more specific sub interfaces, such as Set and List.
Set - A collection without duplicate elements. eg. represent sets, such as the cards comprising
a poker hand, the courses making up a student’s schedule, or the processes running on a machine.
List - An ordered collection (sequence). Lists can contain duplicate elements.
Queue - A collection used to hold multiple elements prior to processing.
Map - An object that maps keys to values. A Map cannot contain duplicate keys; each key can
map to at most one value.
eg. Hashtable
The Collection Interface
List<String > list = new ArrayList <String>( );
This statement creates a new ArrayList ( an implementation of the List
interface), initially containing all the elements in c. T
The ArrayList has been up casted to a List.
An iterator can:
Ask a Collection to hand you an Iterator using a method called iterator (), this Iterator will be ready
to return the first element in the sequence
Get the next object in the sequence with next ()
See if there are any more objects in the sequence with hasNext ()
The ListIterator is a more powerful subtype of Iterator that is produced only by List classes.
While Iterator can only move forward, ListIterator is bidirectional
Iterators
import java.util.*;
class TestJavaCollection1{
public static void main(String args[]){
ArrayList<String> list=new ArrayList<String>();//Creating arraylist
list.add("Car");//Adding object in arraylist
list.add("Bus");
list.add("Jeep");
list.add(“Van");
//Traversing list through Iterator
Iterator itr=list.iterator();
while(itr.hasNext()){
System.out.println(itr.next());
}
}
}
Exercise
Consider the four core interfaces; Set , List , Queue , and Map . For each of the following four
assignments, specify which of the four core interfaces is best - suited
List Whimsical Toys Inc (WTI) needs to record the names of all its employees. Every month, an
employee will be chosen at random from these records to receive a free toy.
Set WTI has decided that each new product will be named after an employee but only first names
will be used, and each name will be used only once. Prepare a list of unique first names.
WTI decides that it only wants to use the most popular names for its toys. Count up the
Map number of employees who have each first name.
WTI acquires season tickets for the local lacrosse team, to be shared by employees. Create a
Queue waiting list for this popular sport.
CS2832: Modular Software Development
Exception Handling
Errors
Exceptional conditions that are external to the application
Exception Hierarchy
Throwable
Exception Error
void simpleMethod() {
throw throw new ArithmeticException("divided by zero!!");
}
Exception Handling
class MyClass{
public void validate(int number) throws InvalidNumberException{
if(number<18)
throw new InvalidNumberException("not valid");
else
System.out.println(“The number if correct!");
}
}
Advantages of Exception Handling