1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
|
package com.hspedu.map_;
import java.security.KeyStore; import java.util.*;
@SuppressWarnings({"all"}) public class Map01 { public static void main(String[] args) {
HashMap employeeHashMap = new HashMap(); employeeHashMap.put(001, new Employee("Mike", 18000, 001)); employeeHashMap.put(002, new Employee("John", 5000, 002)); employeeHashMap.put(003, new Employee("Milan", 19000, 003));
System.out.println("------entrySet for--------------"); Set entrySet = employeeHashMap.entrySet(); for (Object obj : entrySet) { Map.Entry m = (Map.Entry) obj; System.out.println(m.getKey() + " - " + m.getValue()); }
System.out.println("-----------iterator---------"); Iterator iterator = entrySet.iterator(); while (iterator.hasNext()) { Object next = iterator.next(); Map.Entry m = (Map.Entry) next; Employee emp = (Employee) m.getValue(); if (emp.getSalary() > 18000) { System.out.println(emp); } }
System.out.println("-----------keySet and values---------"); Set keySet = employeeHashMap.keySet(); Collection values = employeeHashMap.values(); Iterator iterator2 = keySet.iterator(); while (iterator2.hasNext()) { Object next = iterator2.next(); System.out.println(next + " - " + employeeHashMap.get(next)); } Iterator iterator3 = values.iterator(); while (iterator3.hasNext()) { Object val = iterator3.next(); System.out.println("value" + " - " + val); }
} }
class Employee { private String name; private double salary; private int employeeId;
public Employee(String name, double salary, int employeeId) { this.name = name; this.salary = salary; this.employeeId = employeeId; }
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public double getSalary() { return salary; }
public void setSalary(double salary) { this.salary = salary; }
public int getEmployeeId() { return employeeId; }
public void setEmployeeId(int employeeId) { this.employeeId = employeeId; }
@Override public String toString() { return "Employee{" + "name='" + name + '\'' + ", salary=" + salary + ", employeeId=" + employeeId + '}'; } }
|