/*
This program is a part of the companion code for Core Java 8th ed.
(http://horstmann.com/corejava)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see .
*/
import java.util.HashMap;
import java.util.Map;
/**
* This program demonstrates the use of a map with key type String and value type Employee.
* @version 1.10 2004-08-02
* @author Cay Horstmann
*/
public class MapTest
{
public static void main(String[] args)
{
Map staff = new HashMap();
staff.put("144-25-5464", new Employee("Amy Lee"));
staff.put("567-24-2546", new Employee("Harry Hacker"));
staff.put("157-62-7935", new Employee("Gary Cooper"));
staff.put("456-62-5527", new Employee("Francesca Cruz"));
// print all entries
System.out.println(staff);
// remove an entry
staff.remove("567-24-2546");
// replace an entry
staff.put("456-62-5527", new Employee("Francesca Miller"));
// look up a value
System.out.println(staff.get("157-62-7935"));
// iterate through all entries
for (Map.Entry entry : staff.entrySet())
{
String key = entry.getKey();
Employee value = entry.getValue();
System.out.println("key=" + key + ", value=" + value);
}
}
}
/**
* A minimalist employee class for testing purposes.
*/
class Employee
{
/**
* Constructs an employee with $0 salary.
* @param n the employee name
*/
public Employee(String n)
{
name = n;
salary = 0;
}
public String toString()
{
return "[name=" + name + ", salary=" + salary + "]";
}
private String name;
private double salary;
}