Thursday 26 February 2015

Java : Collection Framework : LinkedHashMap (Add User-Defined Object)


Click here to watch in Youtube : 
https://www.youtube.com/watch?v=8XpccS80VuM&list=UUhwKlOVR041tngjerWxVccw

Employee.java
public class Employee
{

    private String name;
    private int    age;
    private int    salary;

    public Employee( String name, int age, int salary )
    {
        super();
        this.name = name;
        this.age = age;
        this.salary = salary;
    }

    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 int getSalary()
    {
        return salary;
    }

    public void setSalary( int salary )
    {
        this.salary = salary;
    }

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

}
LinkedHashMapExample.java
import java.util.Map;
import java.util.Set;
import java.util.LinkedHashMap;

/*
 * Example of adding User defined Object.
 */
public class LinkedHashMapExample
{
    public static void main(String[] args)
    {

        LinkedHashMap<Integer, Employee> linkedHashMap = new LinkedHashMap<Integer, Employee>();

        Employee john = new Employee("John", 32, 40000);
        Employee david = new Employee("David", 42, 80000);
        Employee peter = new Employee("Peter", 52, 150000);

        /*
         * Key is EmpId - Value is Employee Object
         */

        linkedHashMap.put(20, john);
        linkedHashMap.put(10, david);
        linkedHashMap.put(40, peter);

        System.out.println("linkedHashMap : " + linkedHashMap + "\n");

        Set<Map.Entry<Integer, Employee>> set = linkedHashMap.entrySet();

        System.out.println("set : " + set + "\n");

        System.out.println("-----------------------");
        System.out.println("Key" + " | " + "value");
        System.out.println("-----------------------");

        for (Map.Entry<Integer, Employee> entry : set)
        {
            int empId = entry.getKey();
            Employee employee = entry.getValue();
            System.out.println(empId + "   | " + employee);
        }

    }
}
Output
linkedHashMap : {20=Employee [name=John, age=32, salary=40000], 10=Employee [name=David, age=42, salary=80000], 40=Employee [name=Peter, age=52, salary=150000]}

set : [20=Employee [name=John, age=32, salary=40000], 10=Employee [name=David, age=42, salary=80000], 40=Employee [name=Peter, age=52, salary=150000]]

-----------------------
Key | value
-----------------------
20   | Employee [name=John, age=32, salary=40000]
10   | Employee [name=David, age=42, salary=80000]
40   | Employee [name=Peter, age=52, salary=150000]
To Download LinkedHashMapDemoUserDefined Project Click the below link
https://sites.google.com/site/javaee4321/java-collections/LinkedHashMapDemoUserDefined.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