1

I am trying to fetch the data using List but by using iterator. It is displaying me the object. How can I fetch the data from it?

import java.util.*;
    class Getter_Setter
    {
        int id;
        String name;
        public List<Getter_Setter> buckets;
        public String getName()
        {
            return name;
        }
        public int getId()
        {
            return id;
        }
        public void setId(int id)
        {
            this.id=id;
        }
        public void setName(String name)
        {
            this.name=name;
        }
        public void setGetter_Setter(List<Getter_Setter> buck)
        {
            this.buckets=buck;
        }
    }
    class Simple
    {
        public static void main(String arg[])
        {
            run();
        }
        public static void run()
        {

            List<Getter_Setter> getList=sum();
            Iterator itr=getList.iterator();
            while(itr.hasNext())
            {
                System.out.println(itr.next());
            }
        }
        public static List<Getter_Setter> sum()
        {
            List<Getter_Setter> list=new ArrayList<Getter_Setter>();
            Getter_Setter get=new Getter_Setter();
            get.setId(30);
            get.setName("Hanish");

            System.out.println(get.getId());
            System.out.println(get.getName());
            list.add(get);
            return  list;
        }
    }
3
  • Getter_Setter getterSetter = (Getter_Setter) itr.next() then you can reference to getterSetter to access its data Commented Apr 24, 2015 at 14:57
  • 1
    Override toString() in Getter_Setter. Commented Apr 24, 2015 at 14:57
  • You could also override Getter_Setter#toString Commented Apr 24, 2015 at 14:58

3 Answers 3

1

Your sum methods returns a typed List<Getter_Setter>, so you can get a strongly-typed iterator from it :

List<Getter_Setter> getList = sum();
Iterator itr<Getter_Setter> = getList.iterator();

Which allows you to directly retrieve Getter_Setter elements, on which you can call the getId() and getName() methods :

Iterator<Getter_Setter> itr = getList.iterator();
while (itr.hasNext()) {
    Getter_Setter item = its.next();
    System.out.println(item.getId());
    System.out.println(item.getName());
}
Sign up to request clarification or add additional context in comments.

Comments

0

Since you have not Overridden any toString() method it's giving you object hash code you need to override this(end code snippet) to print things what you want to print from that class otherwise

the default toString() implementation of the Object class, that is:

  public String toString() {
        return getClass().getName() + "@" + Integer.toHexString(hashCode());
    }

gets called when you pass object in println() method

 class Getter_Setter {
        int id;
        String name;
        public List < Getter_Setter > buckets;
        public String getName() {
            return name;
        }
        public int getId() {
            return id;
        }
        public void setId(int id) {
            this.id = id;
        }
        public void setName(String name) {
            this.name = name;
        }
        public void setGetter_Setter(List < Getter_Setter > buck) {
            this.buckets = buck;
        }
        @Override
        public String toString() {
            return "Getter_Setter [id=" + id + ", name=" + name + ", buckets="
                    + buckets + "]";
        }

Comments

0

Use the foreach style to iterate. It is a generic typed iteration method. It automatically makes the objects into your generic type for you. i.e obj to instance of Getter_Setter.

public static void run() {

        List < Getter_Setter > getList = sum();


       foreach(Getter_Setter g: getList){

          System.out.println(g.getId());

         System.out.println(g.getName());
          } 
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.