您可以基于单个TreeMap
.
树状图 |
|
钥匙 |
Integer 访问请求数 |
价值 |
List<T> 请求访问的对象列表 |
班级CircularList<T>
方法 |
此代码可在 Java 7 中使用,无需额外的库 |
得到一个() |
返回T 访问请求数最少的第一个元素 |
getOne(List<T> 过滤器) |
返回T 过滤器中未包含的访问请求数最少的第一个元素 |
getOne(T 过滤器输入) |
返回T 过滤后的元素 |
getCount(T 元素) |
返回int 搜索元素的访问请求数,或者-1 如果没有这样的元素 |
地位() |
返回String 地图的当前状态 |
在线尝试!
public class CircularList<T> {
private final TreeMap<Integer, List<T>> elements = new TreeMap<>();
/**
* @param list required.
*/
public CircularList(List<T> list) {
if (list == null || list.size() == 0) return;
this.elements.put(0, new ArrayList<>(list));
}
/**
* @return the first element with the least number of access requests.
*/
public synchronized T getOne() {
// pull out the entry with the least number of access requests
Map.Entry<Integer, List<T>> entry = this.elements.pollFirstEntry();
Integer key = entry.getKey();
List<T> value = entry.getValue();
// pull out the first element from the list
T element = value.remove(0);
// if there is something left in the list, then put it back
if (value.size() > 0) this.elements.put(key, value);
// take the next list with greater number of access requests
List<T> newValue = this.elements.get(key + 1);
// create it if it doesn't exist
if (newValue == null) newValue = new ArrayList<>();
// add the current element to this list
newValue.add(element);
// update the map
this.elements.put(key + 1, newValue);
// return the first element with the least number of access requests
return element;
}
/**
* @param filter elements list that should be filtered.
* @return the first element with the least number of
* access requests that is not contained in the filter.
*/
public synchronized T getOne(List<T> filter) {
// incorrect filter is not applied
if (filter == null || filter.size() == 0) return getOne();
Integer key = -1;
List<T> value;
T element = null;
// iterate over the entries of the map
for (Map.Entry<Integer, List<T>> entry : this.elements.entrySet()) {
key = entry.getKey();
value = entry.getValue();
element = null;
// iterate over the elements of the list
for (T el : value) {
// the first element not contained in the filter
if (!filter.contains(el)) {
element = el;
// remove this element from the list
value.remove(el);
// if there is nothing left in the list, remove the entry
if (value.size() == 0) this.elements.remove(key);
break;
}
}
// if the element is found
if (element != null) break;
}
// if no element is found, no filter is applied
if (element == null) return getOne();
// take the next list with greater number of access requests
List<T> newValue = this.elements.get(key + 1);
// create it if it doesn't exist
if (newValue == null) newValue = new ArrayList<>();
// add the current element to this list
newValue.add(element);
// update the map
this.elements.put(key + 1, newValue);
// return the first element with the least number of access requests
return element;
}
/**
* @param filterIn element that should be filtered.
* @return the filtered element.
*/
public synchronized T getOne(T filterIn) {
// incorrect filter is not applied
if (filterIn == null) return getOne();
// iterate over the entries of the map
for (Map.Entry<Integer, List<T>> entry : this.elements.entrySet()) {
Integer key = entry.getKey();
List<T> value = entry.getValue();
// iterate over the elements of the list
for (T element : value) {
// if element is found
if (filterIn.equals(element)) {
// remove this element from the list
value.remove(element);
// if there is nothing left in the list, remove the entry
if (value.size() == 0) this.elements.remove(key);
// take the next list with greater number of access requests
List<T> newValue = this.elements.get(key + 1);
// create it if it doesn't exist
if (newValue == null) newValue = new ArrayList<>();
// add the current element to this list
newValue.add(element);
// update the map
this.elements.put(key + 1, newValue);
// return filtered element
return element;
}
}
}
// if no element is found, no filter is applied
return getOne();
}
/**
* Search for the element in the lists of the map.
*
* @param element search element.
* @return the number of access requests of the
* search element, or -1 if there is no such element.
*/
public int getCount(T element) {
for (Map.Entry<Integer, List<T>> entry : this.elements.entrySet()) {
if (entry.getValue().contains(element)) {
return entry.getKey();
}
}
return -1;
}
/**
* @return the current status of the map.
*/
public String status() {
return elements.toString();
}
@Override
public String toString() {
return elements.toString();
}
}
// Test
public static void main(String[] args) {
CircularList<String> list =
new CircularList<>(Arrays.asList("A", "B", "C", "D"));
System.out.println(list); // {0=[A, B, C, D]}
for (int i = 0; i < 10; i++) {
System.out.print(list.getOne(Arrays.asList("A")) + " ");
// B C D B C D B C D B
}
System.out.println();
System.out.println(list.status()); // {0=[A], 3=[C, D], 4=[B]}
for (int i = 0; i < 3; i++) {
System.out.print(list.getOne("D") + " ");
// D D D
}
System.out.println();
System.out.println(list.status()); // {0=[A], 3=[C], 4=[B], 6=[D]}
for (int i = 0; i < 14; i++) {
System.out.print(list.getOne() + " ");
// A A A C A B C A B C A D B C
}
System.out.println();
System.out.println(list.status()); // {6=[A], 7=[D, B, C]}
System.out.println(list.getCount("A")); // 6
System.out.println(list.getCount("E")); // -1
}