Java LinkedHashMap example code - Put, Get, Remove and Clear

How do you store an object in a LinkedHashMap and access it ?

In this example we store an employee information into a LinkedHashMap with the employee number as the key. We learn the following in this tutorial
  • How to save data into a HashMap
  • Read all data from the HashMap
  • Read a specific entry from the map by key value
  • Delete an entry from the map
  • Remove all saved entries from the map

Sample source code for Employee Java Bean
package com.as400samplecode;

public class Employee {

 int employeeNumber;
 String employeeName;
 String employeePosition;
 Double employeeSalary;

 public int getEmployeeNumber() {
  return employeeNumber;
 }
 public void setEmployeeNumber(int employeeNumber) {
  this.employeeNumber = employeeNumber;
 }
 public String getEmployeeName() {
  return employeeName;
 }
 public void setEmployeeName(String employeeName) {
  this.employeeName = employeeName;
 }
 public String getEmployeePosition() {
  return employeePosition;
 }
 public void setEmployeePosition(String employeePosition) {
  this.employeePosition = employeePosition;
 }
 public Double getEmployeeSalary() {
  return employeeSalary;
 }
 public void setEmployeeSalary(Double employeeSalary) {
  this.employeeSalary = employeeSalary;
 }


}

Sample java source code for LinkedHashMap
package com.as400samplecode;

import java.util.Iterator;
import java.util.LinkedHashMap;

public class MyHashMap {

 public static void main(String[] args)
 {
  //Create Hash Map of Field Definitions
  LinkedHashMap hashMap = new LinkedHashMap(4);

  //Store data
  Employee employeeInfo = new Employee(); 

  employeeInfo.setEmployeeNumber(111);
  employeeInfo.setEmployeeName("John");
  employeeInfo.setEmployeePosition("Programmer");
  employeeInfo.setEmployeeSalary(10000.01);
  hashMap.put(111, employeeInfo);

  employeeInfo = new Employee();
  employeeInfo.setEmployeeNumber(112);
  employeeInfo.setEmployeeName("Albert");
  employeeInfo.setEmployeePosition("Programmer");
  employeeInfo.setEmployeeSalary(10000.02);
  hashMap.put(112, employeeInfo);

  employeeInfo = new Employee();
  employeeInfo.setEmployeeNumber(113);
  employeeInfo.setEmployeeName("Gary");
  employeeInfo.setEmployeePosition("Programmer");
  employeeInfo.setEmployeeSalary(10000.03);
  hashMap.put(113, employeeInfo);

  employeeInfo = new Employee();
  employeeInfo.setEmployeeNumber(114);
  employeeInfo.setEmployeeName("Bob");
  employeeInfo.setEmployeePosition("Manager");
  employeeInfo.setEmployeeSalary(10000.04);
  hashMap.put(114, employeeInfo);


  //Read the whole HashMap
  Iterator iterator = hashMap.keySet().iterator();
  while (iterator.hasNext()) {
   Integer key = (Integer) iterator.next();
   employeeInfo = hashMap.get(key); 
   System.out.println("--->");
   System.out.println(employeeInfo.getEmployeeNumber());
   System.out.println(employeeInfo.getEmployeeName());
   System.out.println(employeeInfo.getEmployeePosition());
   System.out.println(employeeInfo.getEmployeeSalary());
  }

  // Output
  // --->
  // 111
  // John
  // Programmer
  // 10000.01
  // --->
  // 112
  // Albert
  // Programmer
  // 10000.02
  // --->
  // 113
  // Gary
  // Programmer
  // 10000.03
  // --->
  // 114
  // Bob
  // Manager
  // 10000.04


  //Read a specific entry
  employeeInfo = hashMap.get(113); 
  System.out.println("--->");
  System.out.println(employeeInfo.getEmployeeNumber());
  System.out.println(employeeInfo.getEmployeeName());
  System.out.println(employeeInfo.getEmployeePosition());
  System.out.println(employeeInfo.getEmployeeSalary());

  // Output
  // --->
  // 113
  // Gary
  // Programmer
  // 10000.03


  //Remove an entry
  employeeInfo = hashMap.remove(112);
  //Read the whole HashMap
  iterator = hashMap.keySet().iterator();
  while (iterator.hasNext()) {
   Integer key = (Integer) iterator.next();
   employeeInfo = hashMap.get(key); 
   System.out.println("--->");
   System.out.println(employeeInfo.getEmployeeNumber());
   System.out.println(employeeInfo.getEmployeeName());
   System.out.println(employeeInfo.getEmployeePosition());
   System.out.println(employeeInfo.getEmployeeSalary());
  }

  // Output
  // --->
  // --->
  // 111
  // John
  // Programmer
  // 10000.01
  // --->
  // 113
  // Gary
  // Programmer
  // 10000.03
  // --->
  // 114
  // Bob
  // Manager
  // 10000.04


  //Clear all the entries
  hashMap.clear();
  System.out.println("Total key value pairs in LinkedHashMap are : " + hashMap.size());

  // Output
  // Total key value pairs in LinkedHashMap are : 0

 }

}

No comments:

Post a Comment

NO JUNK, Please try to keep this clean and related to the topic at hand.
Comments are for users to ask questions, collaborate or improve on existing.