0

Thanks for taking the time to look at my question.

I am using a Tree map to add data into my system

private static Map employeeMap = new TreeMap();

I have then created employee objects like so

theEmployee = new Employee(randomIDno,fName, sName, gender, dPayLevel);

I then add it to the tree map like this

employeeMap.put(randomIDno,theEmployee);

I would just like to know how to iterate through all the Employee objects contained in this treeMap and print it out to the screen?

Binyomin
  • 357
  • 2
  • 7
  • 20

3 Answers3

0

In case you need to sort the elements in a different order than the default one. Override the equals() method for the object or provide a Comparator in the constructor

Gavin
  • 81
  • 1
  • 9
0

If you don't care about order or the TreeMap is maintaining order for you, it's quite simple to use the values() method:

for(Employee employee : employeeMap.values()) {
  System.out.println(employee);
}
Bringer128
  • 6,589
  • 2
  • 32
  • 59
0

You could use the Iterator in java

Set s = employeeMap.entrySet();
Iterator i = s.iterator();

while (i.hasNext())
{
    // display each item.
}
Paul Trone
  • 11
  • 2