Friday 17 October 2014

Java : Collection Framework : LinkedList (Add User Defined Object)



Click here to watch in Youtube : https://www.youtube.com/watch?v=UtkQ1fCZnFw

Student.java
public class Student
{

    private String name;
    private int age;

    public Student(String name, int age)
    {
        super();
        this.name = name;
        this.age = 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;
    }

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

}

LinkedListExample.java
import java.util.LinkedList;

/*
 * Storing user-defined class objects.
 */
public class LinkedListExample
{

    public static void main(String[] args)
    {
        LinkedList<Student> linkedList = new LinkedList<Student>();

        Student john = new Student("John", 32);
        Student david = new Student("David", 42);
        Student peter = new Student("Peter", 52);

        linkedList.add(john);
        linkedList.add(david);
        linkedList.add(peter);

        /*
         * Using for each loop getting each student object from the linkedList
         */
        for (Student student : linkedList)
        {
            System.out.println(student.toString());
        }
    }
}

Output
Student [name=John, age=32]
Student [name=David, age=42]
Student [name=Peter, age=52]

To Download LinkedListDemoUserDefined Project Click the below link

https://sites.google.com/site/javaee4321/java-collections/LinkedListDemoUserDefined.zip?attredirects=0&d=1

See also:
  • All JavaEE Viedos Playlist
  • All JavaEE Viedos
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • No comments:

    Post a Comment