Java LinkedHashMap illustration code – Put, Get, Remove together with Clear

How produce yous shop an object inwards a LinkedHashMap as well as access it ?

In this representative nosotros shop an employee information into a LinkedHashMap amongst the employee release every bit the key. We larn the next inwards this tutorial

  • How to salve information into a HashMap
  • Read all information from the HashMap
  • Read a specific entry from the map past times commutation value
  • Delete an entry from the map
  • Remove all saved entries from the map

Sample beginning code for Employee Java Bean

package com.as400samplecode;  world aeroplane Employee {   int employeeNumber;  String employeeName;  String employeePosition;  Double employeeSalary;   world int getEmployeeNumber() {   furnish employeeNumber;  }  world void setEmployeeNumber(int employeeNumber) {   this.employeeNumber = employeeNumber;  }  world String getEmployeeName() {   furnish employeeName;  }  world void setEmployeeName(String employeeName) {   this.employeeName = employeeName;  }  world String getEmployeePosition() {   furnish employeePosition;  }  world void setEmployeePosition(String employeePosition) {   this.employeePosition = employeePosition;  }  world Double getEmployeeSalary() {   furnish employeeSalary;  }  world void setEmployeeSalary(Double employeeSalary) {   this.employeeSalary = employeeSalary;  }   } 

Sample coffee beginning code for LinkedHashMap

package com.as400samplecode;  import java.util.Iterator; import java.util.LinkedHashMap;  world aeroplane MyHashMap {   world static void main(String[] args)  {   //Create Hash Map of Field Definitions   LinkedHashMap hashMap = novel LinkedHashMap(4);    //Store information   Employee employeeInfo = novel Employee();     employeeInfo.setEmployeeNumber(111);   employeeInfo.setEmployeeName("John");   employeeInfo.setEmployeePosition("Programmer");   employeeInfo.setEmployeeSalary(10000.01);   hashMap.put(111, employeeInfo);    employeeInfo = novel Employee();   employeeInfo.setEmployeeNumber(112);   employeeInfo.setEmployeeName("Albert");   employeeInfo.setEmployeePosition("Programmer");   employeeInfo.setEmployeeSalary(10000.02);   hashMap.put(112, employeeInfo);    employeeInfo = novel Employee();   employeeInfo.setEmployeeNumber(113);   employeeInfo.setEmployeeName("Gary");   employeeInfo.setEmployeePosition("Programmer");   employeeInfo.setEmployeeSalary(10000.03);   hashMap.put(113, employeeInfo);    employeeInfo = novel 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();   acre (iterator.hasNext()) {    Integer commutation = (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();   acre (iterator.hasNext()) {    Integer commutation = (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 commutation value pairs inwards LinkedHashMap are : " + hashMap.size());    // Output   // Total commutation value pairs inwards LinkedHashMap are : 0   }  } 
Hosting Unlimited Indonesia


Sumber http://www.mysamplecode.com/