Ram N
Posts: 2777
Nickname: ramram
Registered: Jul, 2014
|
|
Java : Collection Framework : HashMap (Add User-Defined Object)
|
Posted: Feb 25, 2015 7:06 AM
|
|
This post originated from an RSS feed registered with Java Buzz
by Ram N.
|
Original Post: Java : Collection Framework : HashMap (Add User-Defined Object)
Feed Title: JAVA EE
Feed URL: http://ramj2ee.blogspot.com/feeds/posts/default?alt=rss
Feed Description: This blog has viedo tutorials and Sample codes related to below Technologies.
1.J2EE
2.Java
3.Spring
4.Hibernate
5.Database
6.Oracle
7.Mysql
8.Design Patterns
|
Latest Java Buzz Posts
Latest Java Buzz Posts by Ram N
Latest Posts From JAVA EE
|
|
Advertisement
|
Click here to watch in Youtube : https://www.youtube.com/watch?v=Z1MIdaZ0vEM&list=UUhwKlOVR041tngjerWxVccwEmployee.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 + "]"; }
}
HashMapExample.java import java.util.Map; import java.util.Set; import java.util.HashMap;
/* * Example of adding User defined Object. */ public class HashMapExample { public static void main(String[] args) {
HashMap<Integer, Employee> hashMap = new HashMap<Integer, Employee>();
/* * Key is EmpId - Value is Employee Object */
Employee john = new Employee("John", 32, 40000); Employee david = new Employee("David", 42, 80000); Employee peter = new Employee("Peter", 52, 150000);
hashMap.put(20, john); hashMap.put(10, david); hashMap.put(40, peter);
System.out.println("hashMap : " + hashMap + "\n");
Set<Map.Entry<Integer, Employee>> set = hashMap.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 hashMap : {20=Employee [name=John, age=32, salary=40000], 40=Employee [name=Peter, age=52, salary=150000], 10=Employee [name=David, age=42, salary=80000]}
set : [20=Employee [name=John, age=32, salary=40000], 40=Employee [name=Peter, age=52, salary=150000], 10=Employee [name=David, age=42, salary=80000]]
----------------------- Key | value ----------------------- 20 | Employee [name=John, age=32, salary=40000] 40 | Employee [name=Peter, age=52, salary=150000] 10 | Employee [name=David, age=42, salary=80000]
To Download DisplayAllHeadersApp Project Click the below linkhttps://sites.google.com/site/javaee4321/servlets/DisplayAllHeadersApp.zip?attredirects=0&d=1See also:All JavaEE Viedos PlaylistAll JavaEE ViedosServlets TutorialAll Design Patterns LinksJDBC Tutorial
Read: Java : Collection Framework : HashMap (Add User-Defined Object)
|
|