All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.qiniu.android.dns.util.LruCache Maven / Gradle / Ivy

There is a newer version: 2.0.1
Show newest version
package com.qiniu.android.dns.util;

import java.util.HashMap;
import java.util.LinkedList;

/**
 * Created by bailong on 15/6/18.
 */
public final class LruCache {
    private LinkedList list;
    private HashMap map;
    private int size;

    public LruCache() {
        this(256);
    }

    public LruCache(int size) {
        list = new LinkedList<>();
        map = new HashMap<>();
        this.size = size;
    }

    public LruCache put(K k, V v) {
        if (list.size() == size) {
            K old = list.pollLast();
            map.remove(old);
        }
        map.put(k, v);
        list.push(k);
        return this;
    }

    public LruCache delete(K k) {
        list.remove(k);
        map.remove(k);
        return this;
    }

    public V get(K k) {
        V v = map.get(k);
        list.remove(k);
        list.push(k);
        return v;
    }

    public void clear() {
        list.clear();
        map.clear();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy