问题陈述:
创建LRU缓存并存储最多5个员工对象,找出谁先登录,然后是谁...
package com.test.example.dto;
import java.sql.Timestamp;
public class Employee implements Comparable<Employee> {
private int id;
private String name;
private int age;
private Timestamp loginTime ;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
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 Timestamp getLoginTime() {
return loginTime;
}
public void setLoginTime(Timestamp loginTime) {
this.loginTime = loginTime;
}
@Override
public String toString() {
return "Employee [id=" + id + ", name=" + name + ", age=" + age + ", loginTime=" + loginTime + "]";
}
Employee(){}
public Employee(int id, String name, int age, Timestamp loginTime) {
super();
this.id = id;
this.name = name;
this.age = age;
this.loginTime = loginTime;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + age;
result = prime * result + id;
result = prime * result + ((loginTime == null) ? 0 : loginTime.hashCode());
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (obj == null) return false;
if (getClass() != obj.getClass()) return false;
Employee other = (Employee) obj;
if (age != other.age) return false;
if (id != other.id) return false;
if (loginTime == null) {
if (other.loginTime != null) return false;
} else if (!loginTime.equals(other.loginTime)) return false;
if (name == null) {
if (other.name != null) return false;
} else if (!name.equals(other.name)) return false;
return true;
}
@Override
public int compareTo(Employee emp) {
if (emp.getLoginTime().before( this.loginTime) ){
return 1;
} else if (emp.getLoginTime().after(this.loginTime)) {
return -1;
}
return 0;
}
}
LRUObjectCacheExample
package com.test.example;
import java.sql.Timestamp;
import java.util.Calendar;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;
import com.test.example.dto.Employee;
public class LRUObjectCacheExample {
LinkedHashMap<Employee, Boolean> lruCacheLinkedQueue;
public LRUObjectCacheExample(int capacity) {
lruCacheLinkedQueue = new LinkedHashMap<Employee, Boolean>(capacity, 1.0f, true) {
private static final long serialVersionUID = 1L;
@Override
protected boolean removeEldestEntry(
//calling map's entry method
Map.Entry<Employee, Boolean> eldest) {
return this.size() > capacity;
}
};
}
void addDataIntoCache(Employee employee) {
lruCacheLinkedQueue.put(employee, true);
displayLRUQueue();
}
boolean checkIfDataPresentIntoLRUCaache(int data) {
return lruCacheLinkedQueue.get(data) != null;
}
void deletePageNo(int data) {
if (lruCacheLinkedQueue.get(data) != null){
lruCacheLinkedQueue.remove(data);
}
displayLRUQueue();
}
void displayLRUQueue() {
System.out.print("-------------------------------------------------------"+"\n");
System.out.print("Data into LRU Cache : ");
for (Entry<Employee, Boolean> mapEntry : lruCacheLinkedQueue.entrySet()) {
System.out.print("[" + mapEntry.getKey() + "]");
}
System.out.println("");
}
public static void main(String args[]) {
Employee employee1 = new Employee(1,"Shahbaz",29, getCurrentTimeStamp());
Employee employee2 = new Employee(2,"Amit",35,getCurrentTimeStamp());
Employee employee3 = new Employee(3,"viquar",36,getCurrentTimeStamp());
Employee employee4 = new Employee(4,"Sunny",20,getCurrentTimeStamp());
Employee employee5 = new Employee(5,"sachin",28,getCurrentTimeStamp());
Employee employee6 = new Employee(6,"Sneha",25,getCurrentTimeStamp());
Employee employee7 = new Employee(7,"chantan",19,getCurrentTimeStamp());
Employee employee8 = new Employee(8,"nitin",22,getCurrentTimeStamp());
Employee employee9 = new Employee(9,"sanuj",31,getCurrentTimeStamp());
//
LRUObjectCacheExample lru = new LRUObjectCacheExample(5);
lru.addDataIntoCache(employee5);//sachin
lru.addDataIntoCache(employee4);//Sunny
lru.addDataIntoCache(employee3);//viquar
lru.addDataIntoCache(employee2);//Amit
lru.addDataIntoCache(employee1);//Shahbaz -----capacity reached
//
lru.addDataIntoCache(employee6);/Sneha
lru.addDataIntoCache(employee7);//chantan
lru.addDataIntoCache(employee8);//nitin
lru.addDataIntoCache(employee9);//sanuj
//
lru.deletePageNo(3);
lru.deletePageNo(4);
}
private static Timestamp getCurrentTimeStamp(){
return new java.sql.Timestamp(Calendar.getInstance().getTime().getTime());
}
}
Results:
**Data into LRU Cache :**
[Employee [id=1, name=Shahbaz, age=29, loginTime=2015-10-15 18:47:28.1
[Employee [id=6, name=Sneha, age=25, loginTime=2015-10-15 18:47:28.125
[Employee [id=7, name=chantan, age=19, loginTime=2015-10-15 18:47:28.125
[Employee [id=8, name=nitin, age=22, loginTime=2015-10-15 18:47:28.125
[Employee [id=9, name=sanuj, age=31, loginTime=2015-10-15 18:47:28.125