Richard's J2SE Day 8
Data Structures and Strings

=================================================================
Old notes from "Sams Teach Yourself Java 2 in 21 Days" (still vaild, but different page number)
=================================================================

Java Data Structures (P211)

java.util package consists of interfaces (Iterator and Map) and classes:

 

Iterator (P214)
The Iteractor interface provides a standard means of iterating through a list of elements in a defined sequence. methods:
public boolean hasNext(); => if the structure contains any more elements
public object next(); => retrieves the next element
public void remove;

 

Bit Set (P215)
individual bits to store boolean values

Index
0
1
2
3
Value
Boolean0
Boolean1
Boolean2
Boolean3

(P216) HolidaySked.java, (integer as argument to program) use a bit set to keep track of which days in a year are holidays

 

Vector (P218)
an expandable array of objects

size of vector = number of elements currently stored in it
capacity of vector = amount of memory allocated to hold elements

Vector v = new Vector (); // create default vector containing no elements
Vector v = new Vector (25); // create a vector with a specified capacity
Vector v = new Vector (25, 5); // initial size of 25 elements and will expand in a increments of 5 elements when more than 25 elements are added

v.add("Watson"); // add() method to add element to vector

String s = (String)v.lastElement(); // retrieve the last string, vector class is designed to work with object class, casting needed

String s1 = (String)v.get(0); // get() method, retrieve a element using an index

v.add(0, "Hogan"); // add element at index
v.remove(3); //remove element at index

v.set(1,"Woods"); // set() method to change a specific element

v.clear(); // clear out the vector completely

int i = v.indexOf("Nicklaus"); // find the index of an element based on the element itself

Iterator it = v.iteractor(); // work sequentially with all the elements

int size = v.size(); // determines the number of elements

v.setSize(10); // explicitly set the size of the vector

v.trimtoSize(); // force the capacity to exactly match the size

int capacity = v.capacity(); // see what the capacity is

 

Stacks (P221)
last-in-first-out (LIFO) stack

0
Element3
Top
1
Element2
2
Element1
3
Element0
Bottom

Stack s = new Stack(); // constructor to create a stack

s.push("One");

String s1 = (String)s.pop();

String s3 = (String)s.peek(); // get the top element on the stack without actually popping it off the stack

int i = s.search("Two"); // search for a element, return -1 if element is not found

boolean isEmpty = s.empty(); // determines whether a stack is empty

 

Map (P223)
Map interface defines a framework for implementing key-mapped data structure.
Map interface declares a variety of methods. Implementing classes will have to implement all of those methods. (put and get methods)
Assuming look is a class that implements the Map interface

Rectangle r1 = new Rectangle(0,0,5,5);
look.put ("small", r1);
Rectangle r2 = new Rectangle(0,0,15,15);
look.put("medium",r2);
Rectangle r3 = new Rectangle(0,0,25,25);
look.put("large",r3);

Rectangle 3 = (Rectangle) look.get("medium");

look.remove("large");

int size = look.size();

boolean isEmpty = look.isEmpty();

 

Hash Tables (P224)
Hashtable class is derived from Dictionary, implements the Map interface.
load factor is a number between 0.0 and 1.0

Hashtable hash = new Hashtable(); // constructor
Hashtable hash = new Hashtable(20); // create a hash table with specified initial capacity
Hashtable hash = new Hashtable(20, 0.75F); // speficied initial capaticy and load factor

hash.clear(); // clears a hash table

boolean isThere = hash.contains(new Rectangle(0,0,5,5)); // checks whether an object is stored in the hash table
boolean isThere = hash.containsKey("Small"); // searchs based on a key

hash.rehash(); // force a rehash (to determines that it must increase its capacity)

A hash code is a computed key that uniquely identifies each element in a hash table
refer to book for hashCode() method and equals() method

(P226) ComicBooks.java

(P232) Exercise: ComicBooks.java updated

 

=================================================================
New notes from "Sams Teach Yourself Java 6 in 21 Days"
=================================================================

Vector and Iterator example (Vector using iterator() method): CodeKeeper.java
// run at command line with a argument e.g. "java SquareTool 13"