Hercules
Hercules

Reputation: 101

How can I print a list (employee type) using Iterator?

Employee class:

public class Employee {

    int empid;
    String name;
    int age;

    public Employee(int empid,String name,int age)
    {
        this.empid=empid;
        this.name=name;
        this.age=age;
    }
    public int getEmpid() {
        return empid;
    }
    public void setEmpid(int empid) {
        this.empid = empid;
    }
    public String getname() {
        return name;
    }
    public void setname(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    } 
}

Comparator class:

public class Employee_comparator implements Comparator<Employee> {

    @Override
    public int compare(Employee object1, Employee object2) {
        return object1.getname().compareTo(object2.getname());
    }
}

Main class:

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

public class Employee_Main {

    /**
     * @param args
     */
    public static void main(String[] args) {

        List<Employee> list=new ArrayList<Employee>();

        list.add(new Employee(33186,"varun",23));
        list.add(new Employee(33187,"deepak",23));
        list.add(new Employee(33188,"apple",23));
        list.add(new Employee(33189,"rohan",23));

        Collections.sort(list,new Employee_comparator());

        for(int i=0;i<list.size();i++) {

            System.out.print("age:"+list.get(i).getAge());
            System.out.print("empid:"+list.get(i).getEmpid());
            System.out.println("name:"+list.get(i).getname());
        }

        Iterator<Employee> itr=list.iterator();

        while(itr.hasNext())
        {
            System.out.println(itr.next());
        }
    }
}

When I try printing by using get methods, it is working fine. But when, I try to print the elements of the list using Iterator, it's giving the following output:

new_.Employee@28122812new_.Employee@280d280dnew_.Employee@28172817new_.Employee@28082808

Upvotes: 10

Views: 52023

Answers (9)

Eyal Talmor
Eyal Talmor

Reputation: 1

static void filter(List c) {
        int i=0;
        for (Iterator<String> it = c.iterator(); it.hasNext(); ) {
            if (it.next() != null)
                System.out.println(c.get(i));
            i++;
        }
    }

Upvotes: 0

Vusal Mustafayev
Vusal Mustafayev

Reputation: 11

Use toString method() ,it will be printed

Upvotes: -1

Sina Keshvadi
Sina Keshvadi

Reputation: 11

You should add a Employee method to your class:

public void printEmploee()
{
  System.out.printf("ID:%s \nName: %s \nAge: %s \n",
  this.id, this.name, this.age);
}

Then use the Iterator:

Iterator<Employee> itr = list.iterator();
while (itr.hasNext()) {
itr.next().printEmploee();
}

Upvotes: 0

Renato Lochetti
Renato Lochetti

Reputation: 4576

You can implement a method toString() in the class Employee:

public String toString(){
    return "Name: "+ this.name
       + ". Age:"+ this.age
       + ". Id:"+ this.empId; 
} //example

This way, when you do:

System.out.println(itr.next());

it will print the name, the age and the id of the employee

Upvotes: 11

Ashlin Karkada
Ashlin Karkada

Reputation: 1410

You can do the same in Java 8 using below code

Iterator<Employee> employees = list.iterator(); employees.forEachRemaining(System.out::println);

or simply,

list.iterator.forEachRemaining(System.out::println);

Upvotes: 3

Ankit
Ankit

Reputation: 3181

You are printing the Employee class object and not its values,

Try :

Iterator<Employee> itr=list.iterator();

while(itr.hasNext())
{
    //System.out.println(itr.next());
    System.out.println(itr.next().getEmpid());
}

Between, use enhanced for loop, it is faster than iterator.

EDIT:

Enhanced for loop is more readable than Iterator. Whereas, Iterator gives you option of removing values using iterator.remove() method. for loop throws ConcurrentModificationException if you try to update list during loop.

Upvotes: 4

someone
someone

Reputation: 6572

your itr.next() contains employee object and it will print toString() method. Since you have not override to String method you get this. You could override to String method as you want and

System.out.println(itr.next());

will print what is it in toString() method.

This will be in your Employee class

 @Override
public String toString() {
    return "Employee [empid=" + empid + ", name=" + name + ", age=" + age + "]";
}

Upvotes: 1

Yagnesh Agola
Yagnesh Agola

Reputation: 4659

Because itr.next() has return Object of the class Employee that you have sotre in it. try

    Iterator<Employee> itr=list.iterator();

    while(itr.hasNext())
    {
        Employee employee = itr.next();

        System.out.print("age:"+employee.getAge());
        System.out.print("empid:"+employee.getEmpid());
        System.out.println("name:"+employee.getname());
    }

Upvotes: 5

Abubakkar
Abubakkar

Reputation: 15664

Override the toString method so that you can get the Employee details even if its toString method is called.

public void toString(){
 return "Employee Id: "+empid+" Name: "+name+" Age: "+age;
}

Now when you call itr.next() your Employee class toString method is called and now it will return the String that you have created in the overriden toString method.

Upvotes: 2

Related Questions