Sunday 17 May 2015

Storing Java Object In LinkedList

import java.util.LinkedList;

public class LinkedListOfObject {

    public static void main(String[] args) {
       
        LinkedList<User> list = new LinkedList<User>();
        list.add(new User("Yogesh",123));
        list.add(new User("Shivam",123));
        list.add(new User("Anuj",123));
        list.add(new User("Sunder",123));
       
        for (User user : list) {
            System.out.println("user name="+user.getName()+"  user Id="+user.getEmpId());
        }
       
    }
}

class User{
    private String name;
    private int empId;
   
    public User(String name, int empId) {
        super();
        this.name = name;
        this.empId = empId;
    }
   
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getEmpId() {
        return empId;
    }
    public void setEmpId(int empId) {
        this.empId = empId;
    }
}

Output:

user name=Yogesh user Id=123

user name=Shivam user Id=123

user name=Anuj user Id=123

user name=Sunder user Id=123


No comments:

Post a Comment