How to iterate a list inside a list in java?

Posted by user2142786 on Stack Overflow See other posts from Stack Overflow or by user2142786
Published on 2014-08-22T04:10:09Z Indexed on 2014/08/22 4:20 UTC
Read the original article Hit count: 121

Filed under:
|
|
|

Hi i have two value object classes .

package org.array;

import java.util.List;



public class Father {

    private String name;
    private int age ;
    private List<Children> Childrens;
    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;
    }
    public List<Children> getChildrens() {
        return Childrens;
    }
    public void setChildrens(List<Children> childrens) {
        Childrens = childrens;
    }


}

second is for children

package org.array;

public class Children {

    private String name;
    private int age ;
    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;
    }


}

and i want to print there value i nested a list inside a list here i am putting only a single value inside the objects while in real i have many values . so i am nesting list of children inside father list. how can i print or get the value of child and father both. here is my logic.

package org.array;

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

public class ArrayDemo {

    public static void main(String[] args) {
         List <Father> fatherList = new ArrayList<Father>();
        Father father =  new Father();
        father.setName("john");
        father.setAge(25);
        fatherList.add(father);

         List <Children> childrens = new ArrayList<Children>();
         Children children = new Children();
        children.setName("david");
        children.setAge(2);
        childrens.add(children);
        father.setChildrens(childrens);
        fatherList.add(father);

        Iterator<Father> iterator = fatherList.iterator();
        while (iterator.hasNext()) {
            System.out.println(iterator.toString());
        }



        }
        }

© Stack Overflow or respective owner

Related posts about java

Related posts about list