Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

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
like image 520
Hercules Avatar asked Nov 01 '12 10:11

Hercules


People also ask

Can iterator be used for list?

An iterator is an object in Java that allows iterating over elements of a collection. Each element in the list can be accessed using iterator with a while loop.

How do I find an iterator in a list?

Obtain an iterator to the start of the collection by calling the collection's iterator() method. Set up a loop that makes a call to hasNext(). Have the loop iterate as long as hasNext() returns true. Within the loop, obtain each element by calling next().

Can iterator be used for ArrayList?

The iterator can be used to iterate through the ArrayList wherein the iterator is the implementation of the Iterator interface.


1 Answers

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);

like image 66
Ashlin Karkada Avatar answered Oct 17 '22 09:10

Ashlin Karkada