fredag 1 juni 2012

Interface, Map and HashMap

Lecture 19.
Interface
-> set of methods
-> common functionality among a set of classes

Syntax: public class ClassName implements InterfaceName


Interface is more general than extends.

Map (interface)
Key
Value


An association between a key and a value. Always keysensitive.
Ex. Dictionary - a map
      words - key
      defintions - value
Ex. Phonebook - a map
      names - key
      numbers - value

HashMap (class)
     -> implements Map
template -> 2 types ( one type for your keys and one for your values)

HashMap<String, Integer> phonebook = new HashMap<String, Integer>( );

Methods most commonly used: put and get, but also remove, containskey and size.

phonebook.put("Mehran", 7236059);
phonebook.put("Jenny", 8675309);

Integer MehranNumber = phonebook.get("Mehran");

phonebook.remove(key); //removes key AND value
phonebook.containskey(key); // returns a boolean

THINK of it as a bag where you put things in pairs. No order.