1 Object[] key_arr = hashmap.keySet().toArray(); 2 Arrays.sort(key_arr); 3 for (Object key : key_arr) { 4 Object value = hashmap.get(key); 5 }
1 package Aug11.Collection; 2 3 import java.util.*; 4 5 public class EmployeeTest { 6 static int EmId=237; 7 public static void main(String[] args) { 8 Scanner reader=new Scanner(System.in); 9 HashMap map = new HashMap(); 10 //TreeMap map = new TreeMap(); //sorted 11 Employee1 emp; 12 for (int i = 0; i <3; i++) { 13 emp=new Employee1(); 14 System.out.println("Enter name:"); 15 String name=reader.next(); 16 System.out.println("Enter address:"); 17 String addr=reader.next(); 18 System.out.println("Enter dateJoined(YY/mm/):"); 19 String dateJoined=reader.next(); 20 System.out.println("Enter dateBirth:"); 21 String dateBirth=reader.next(); 22 System.out.println("Enter basicPay:"); 23 double basicPay=reader.nextDouble(); 24 25 emp.setEmpName(name); 26 emp.setAddr(addr); 27 emp.setDateJoined(dateJoined); 28 emp.setBasicPay(basicPay);; 29 emp.setDateBirth(dateBirth);; 30 map.put(genEmpID(dateJoined), emp); 31 32 } 33 34 System.out.println(" EmpId EmpName Address DateJoined DateBirth BasicPay"); 35 36 Object[] key_arr = map.keySet().toArray(); 37 Arrays.sort(key_arr); 38 for (Object key : key_arr) { 39 Employee1 em = (Employee1) map.get(key); 40 System.out.println(key+"\t"+em.getEmpName()+"\t"+em.getAddr()+"\t"+em.getDateJoined()+"\t"+em.getDateBirth()+"\t"+em.getBasicPay()); 41 42 } 43 44 /*Set<Integer> set = map.keySet(); 45 Iterator<Integer> iter = set.iterator(); 46 while(iter.hasNext()){ 47 int key = iter.next(); 48 Employee1 em=(Employee1) map.get(key); 49 System.out.println(key+"\t"+em.getEmpName()+"\t"+em.getAddr()+"\t"+em.getDateJoined()+"\t"+em.getDateBirth()+"\t"+em.getBasicPay()); 50 }*/ 51 52 } 53 54 55 56 57 58 59 public static int genEmpID(String dateJoined){ 60 String EmpID=dateJoined+(EmId+1); 61 EmId++; 62 String [] s=EmpID.split("/"); 63 StringBuffer bs=new StringBuffer(); 64 for(int i=0;i<s.length;i++){ 65 bs.append(s[i]); 66 } 67 int ID=Integer.parseInt(bs.toString()); 68 return ID; 69 70 } 71 72 } 73 74 75 76 77 78 class Employee1 { 79 private String empName; 80 private String addr; 81 private String dateJoined; 82 private String dateBirth; 83 private double basicPay; 84 85 public String getEmpName() { 86 return empName; 87 } 88 public void setEmpName(String empName) { 89 this.empName = empName; 90 } 91 public String getAddr() { 92 return addr; 93 } 94 public void setAddr(String addr) { 95 this.addr = addr; 96 } 97 public String getDateJoined() { 98 return dateJoined; 99 } 100 public void setDateJoined(String dateJoined) { 101 this.dateJoined = dateJoined; 102 } 103 public String getDateBirth() { 104 return dateBirth; 105 } 106 public void setDateBirth(String dateBirth) { 107 this.dateBirth = dateBirth; 108 } 109 public double getBasicPay() { 110 return basicPay; 111 } 112 public void setBasicPay(double basicPay) { 113 this.basicPay = basicPay; 114 } 115 116 117 118 119 }
时间: 2024-12-22 20:04:31