java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > java LFU算法

java实现LFU算法的示例代码

作者:技术驿站

LFU(Least Frequently Used)算法根据数据的历史访问频率来淘汰数据,其核心思想是“如果数据过去被访问多次,那么将来被访问的频率也更高”,本文为大家整理了Java实现LFU算法的示例代码,需要的可以参考下

使用JAVA语言实现常用的LFU(Least Frequently Used)算法

定义一个LFUNode类来表示LFU缓存中的节点

private static class LFUNode<K, V> {
    K key;
    V value;
    int frequency;

    public LFUNode(K k, V v) {
        key = k;
        value = v;
        frequency = 1;
    }
}

接下来,我们创建一个LFUCache类来实现LFU缓存。在初始化函数中,我们需要指定缓存的最大容量。

public class LFUCache1<K, V> {
    private int capacity;
    private Map<K, LFUNode<K, V>> cacheMap;
    private Map<Integer, LinkedHashSet<LFUNode<K, V>>> frequencyMap;
    private int minFrequency;

    public void init(int size) {
        this.capacity = size;
        cacheMap = new HashMap<>();
        frequencyMap = new HashMap<>();
        minFrequency = 0;
    }
    ...

接下来,我们实现两个辅助方法:

private void addToFrequencyMap(LFUNode<K, V> node) {
    int frequency = node.frequency;
    frequencyMap.putIfAbsent(frequency, new LinkedHashSet<>());
    frequencyMap.get(frequency).add(node);

    if (frequency == 1) {
        minFrequency = 1;
    }
}
private void removeFromFrequencyMap(LFUNode<K, V> node) {
    int frequency = node.frequency;
    frequencyMap.get(frequency).remove(node);

    if (frequency == minFrequency && frequencyMap.get(frequency).size() == 0) {
        minFrequency++;
    }
}

最后实现核心方法:getput

public V get(K key) {
    if (cacheMap.containsKey(key)) {
        LFUNode<K, V> node = cacheMap.get(key);
        removeFromFrequencyMap(node);
        node.frequency++;
        addToFrequencyMap(node);
        return node.value;
    }
    return null;
}
public void put(K key, V value) {
    if (capacity <= 0) {
        return;
    }

    if (cacheMap.containsKey(key)) {
        LFUNode<K, V> node = cacheMap.get(key);
        removeFromFrequencyMap(node);
        node.value = value;
        node.frequency++;
        addToFrequencyMap(node);
    } else {
        if (cacheMap.size() >= capacity) {
            LinkedHashSet<LFUNode<K, V>> minFrequencySet = frequencyMap.get(minFrequency);
            LFUNode<K, V> evictNode = minFrequencySet.iterator().next();
            minFrequencySet.remove(evictNode);
            cacheMap.remove(evictNode.key);
        }

        LFUNode<K, V> newNode = new LFUNode<>(key, value);
        cacheMap.put(key, newNode);
        addToFrequencyMap(newNode);
        minFrequency = 1;
    }
}

最终验证结果如下:

@Test
public void testCase() {
    LFUCache1<Integer, String> cache = new LFUCache1<>();
    cache.init(2);
    cache.put(1, "Hello");
    cache.put(2, "World");
    System.out.println(cache.get(1));  // Output: Hello
    cache.put(3, "Foo");
    System.out.println(cache.get(2));  // Output: null (evicted)
    System.out.println(cache.get(3));  // Output: Foo
}

到此这篇关于java实现LFU算法的示例代码的文章就介绍到这了,更多相关java LFU算法内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文