题目描述:设计一个接口,将对象存在缓存中。替换策略用FIFO。
接口定义
/**
* @author jy
* @date 2018年6月27日
* <p>Description: </p>
*/
package datastruct;
/**
* @author jy
* @param <E>
*
*/
public interface Cache<E> {
public void put(String key, E value);
public E get(String key);
}
1.用hashmap来实现
/**
* @author jy
* @date 2018年6月27日
* <p>Description: </p>
*/
package datastruct;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
/**
* @author jy
* @param <E>
*
*/
public class FIFOCache<E> implements Cache<E> {
int size = 0;
int capacity;
Map<String, E> map = new HashMap();
List<String> list = new LinkedList();
public FIFOCache(int capacity) {
this.capacity = capacity;
}
/*
* (non-Javadoc)
*
* @see datastruct.Cache#put(java.lang.Object)
*/
@Override
public void put(String key, E value) {
if (size == capacity) {
replace();
}
map.put(key, value);
list.add(key);
size++;
}
/*
* (non-Javadoc)
*
* @see datastruct.Cache#get(java.lang.String)
*/
@Override
public E get(String key) {
return map.get(key);
}
/*
* (non-Javadoc)
*
* @see datastruct.Cache#replace()
*/
private void replace() {
String key = list.get(0);
map.remove(key);
list.remove(0);
size--;
}
public static void main(String args[]) {
FIFOCache<String> cache = new FIFOCache(3);
cache.put("1", "1");
cache.put("2", "2");
cache.put("3", "3");
cache.put("4", "4");
System.out.println(cache.size);
System.out.println(cache.get("1"));
System.out.println(cache.get("4"));
cache.put("5", "5");
System.out.println(cache.size);
System.out.println(cache.get("2"));
System.out.println(cache.get("3"));
System.out.println(cache.get("5"));
System.out.println(cache.get("4"));
}
}
2.用数组来实现
/**
* @author jy
* @date 2018年6月27日
* <p>Description: </p>
*/
package datastruct;
import java.util.HashMap;
import java.util.Map;
/**
* @author jy
*
*/
public class FIFOCacheWithArray<E> implements Cache<E> {
int size = 0;
int capacity;
Map[] objects;
/**
* @param args
* <p>
* Description:
* </p>
*/
public FIFOCacheWithArray(int capacity) {
this.capacity = capacity;
objects = new Map[capacity];
}
/*
* (non-Javadoc)
*
* @see datastruct.Cache#put(java.lang.String, java.lang.Object)
*/
@Override
public void put(String key, E value) {
if (size == capacity) {
replace();
}
Map map = new HashMap<String, Object>();
map.put(key, value);
objects[size++] = map;
}
/*
* (non-Javadoc)
*
* @see datastruct.Cache#get(java.lang.String)
*/
@Override
public E get(String key) {
for (int i = 0; i < capacity; i++) {
Map map = objects[i];
if (map.containsKey(key)) {
return (E) map.get(key);
}
}
return null;
}
/*
* (non-Javadoc)
*
* @see datastruct.Cache#setCacheCapcity(int)
*/
/*
* (non-Javadoc)
*
* @see datastruct.Cache#replace()
*/
private void replace() {
for (int i = 1; i < capacity; i++) {
objects[i - 1] = objects[i];
}
size--;
}
public static void main(String[] args) {
FIFOCacheWithArray cache = new FIFOCacheWithArray(3);
// cache.setCacheCapacity(3);
cache.put("1", "1");
cache.put("2", "2");
cache.put("3", "3");
cache.put("4", "4");
System.out.println(cache.size);
System.out.println(cache.get("1"));
System.out.println(cache.get("4"));
cache.put("5", "5");
System.out.println(cache.size);
System.out.println(cache.get("2"));
System.out.println(cache.get("3"));
System.out.println(cache.get("5"));
System.out.println(cache.get("4"));
}
}