And an Introduction to GUIs
Using Java's Swing Toolkit
Lecture Time!
• HashMap
• J/Frame
• Component classes
– J/Button
– J/TextField
The Java Collections Framework
• This is a set of classes you can use for containing arbitrarily large collections of objects.
• To use: import java.util.*;
• Some basic Collection classes:
– ArrayList
– Vector
– HashMap
– Hashtable (yes, that's a small t)
HashMap
• ArrayList is usually mentioned as ArrayList
– E is the object type that it will contain.
– ArrayList means that we want an
ArrayList that can contain BankAccounts.
• HashMap, however, is defined as HashMap
– K is the object type that will be used as a key.
• HashMaps do not use an index and must rely on a provided “key” data to fetch an object.
– V is the object type that the HashMap will contain.
• Similar to E in ArrayList.
Example import java.util.*; public class HashMapDemo
{
public static void main( String args[] )
{
HashMap accts = new HashMap(); accts.put( "Bob", new BankAccount( 2000 ) ); accts.put( "Alice", new BankAccount( 1000 ) ); accts.get( "Bob" ).deposit( 500 );
// how do you print Alice's and Bob's balances?
}
}
Other HashMap Methods
• void clear()
– Empties the HashMap of keys and values.
• boolean containsKey( Object key )
– True if the HashMap has a value mapped to this key.
• boolean containsValue( Object value )
– True if the HashMap has the specified value inside.
• Set keySet() // self-study
– Gives a Set of all of its keys for iteration purposes.
• Collection values() // self-study
– Gives a Collection of all its value contents for iteration purposes. Java Swing Toolkit
• import java.awt.*;
• import javax.swing.*;
• While JOptionPane is useful, programmers often want more control over their programs.
– I don't want to use System.out.println() for program output anymore!
– I want to be able to accept 3 different text inputs at the same time