I am working with examples from the book, Core Javaâ„¢ 2 Volume II – Advanced Features, Seventh Edition by Cay S. Horstmann, Gary Cornell and using the following example, my code compiles fine with the DOS prompt, but errors galore appear in MyEclispe, as well as the Java Perspective.
import java.util.*;
public class MapTest {
public static void main(String[] args)
{
Map<String, Employee> staff = new HashMap<String, Employee>();
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<String, Employee> entry : staff.entrySet())
{
String key = entry.getKey();
Employee value = entry.getValue();
System.out.println(“key=” + key + “, value=” + value);
}
}
}
//New Class
/**
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;
}
Eclipse does not seem to undetstand the Generics (read http://java.sun.com/j2se/1.5/pdf/generics-tutorial.pdf ) that are included in the code. Is there something I need to do in order to compile, or will MyEclipse use the new features in 1.5?
Thanks,
Curtis Fisher