LRUCache 原理,阿里面试题前端

public class LruCache<T, Y> {

private final LinkedHashMap<T, Y> cache = new LinkedHashMap<>(100, 0.75f, true);

private final int initialMaxSize;

private int maxSize;

private int currentSize = 0;

/**

  • Constructor for LruCache.

  • @param size The maximum size of the cache, the units must match the units used in {@link

  •         #getSize(Object)}.
    

*/

public LruCache(int size) {

this.initialMaxSize = size;

this.maxSize = size;

}

/**

  • Sets a size multiplier that will be applied to the size provided in the constructor to put the

  • new size of the cache. If the new size is less than the current size, entries will be evicted

  • until the current size is less than or equal to the new size.

  • @param multiplier The multiplier to apply.

*/

public synchronized void setSizeMultiplier(float multiplier) {

if (multiplier < 0) {

throw new IllegalArgumentException(“Multiplier must be >= 0”);

}

maxSize = Math.round(initialMaxSize * multiplier);

evict();

}

/**

  • Returns the size of a given item, defaulting to one. The units must match those used in the

  • size passed in to the constructor. Subclasses can override this method to return sizes in

  • various units, usually bytes.

  • @param item The item to get the size of.

*/

protected int getSize(Y item) {

return 1;

}

/**

  • A callback called whenever an item is evicted from the cache. Subclasses can override.

  • @param key The key of the evicted item.

  • @param item The evicted item.

*/

protected void onItemEvicted(T key, Y item) {

// optional override

}

/**

  • Returns the current maximum size of the cache in bytes.

*/

public synchronized int getMaxSize() {

return maxSize;

}

/**

  • Returns the sum of the sizes of all items in the cache.

*/

public synchronized int getCurrentSize() {

return currentSize;

}

/**

  • Returns true if there is a value for the given key in the cache.

  • @param key The key to check.

*/

public synchronized boolean contains(T key) {

return cache.containsKey(key);

}

/**

  • Returns the item in the cache for the given key or null if no such item exists.

  • @param key The key to check.

*/

@Nullable

public synchronized Y get(T key) {

return cache.get(key);

}

/**

  • Adds the given item to the cache with the given key and returns any previous entry for the

  • given key that may have already been in the cache.

  • If the size of the item is larger than the total cache size, the item will not be added to

  • the cache and instead {@link #onItemEvicted(Object, Object)} will be called synchronously with

  • the given key and item.

  • @param key The key to add the item at.

  • @param item The item to add.

*/

public synchronized Y put(T key, Y item) {

final int itemSize = getSize(item);

if (itemSize >= maxSize) {

onItemEvicted(key, item);

return null;

}

final Y result = cache.put(key, item);

if (item != null) {

currentSize += getSize(item);

}

if (result != null) {

// TODO: should we call onItemEvicted here?

currentSize -= getSize(result);

}

evict();

return result;

}

/**

  • Removes the item at the given key and returns the removed item if present, and null otherwise.

  • @param key The key to remove the item at.

*/

@Nullable

public synchronized Y remove(T key) {

final Y value = cache.remove(key);

if (value != null) {

currentSize -= getSize(value);

}

return value;

}

/**

  • Clears all items in the cache.

*/

public void clearMemory() {

trimToSize(0);

}

/**

  • Removes the least recently used items from the cache until the current size is less than the

  • given size.

  • @param size The size the cache should be less than.

*/

protected synchronized void trimToSize(int size) {

Map.Entry<T, Y> last;

while (currentSize > size) {

last = cache.entrySet().iterator().next();

final Y toRemove = last.getValue();

currentSize -= getSize(toRemove);

final T key = last.getKey();

cache.remove(key);

onItemEvicted(key, toRemove);

}

}

private void evict() {

trimToSize(maxSize);

}

}

LruCache采用的集合是LinkedHashMap,这个集合是HashMap的基础上增加了 数据链表的功能,可以看到下面这个构造函数,第一个是初始容量100, 第二个是碰撞因子0.75(即真实容量到达总容量的75%就开始扩容),第三个是链表顺序是否按访问顺序,关于这个容器的代码分析我们放在下一篇文章,在这里我们只需要知道这个集合能记录到你访问数据的次序,最近的访问的会放在链表的前面

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数Android工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
img
img
img
img
img
img
img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点,真正体系化!

由于文件比较大,这里只是将部分目录大纲截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且后续会持续更新

如果你觉得这些内容对你有帮助,可以添加V获取:vip204888 (备注Android)
img

最后

以前一直是自己在网上东平西凑的找,找到的东西也是零零散散,很多时候都是看着看着就没了,时间浪费了,问题却还没得到解决,很让人抓狂。

后面我就自己整理了一套资料,还别说,真香!

资料有条理,有系统,还很全面,我不方便直接放出来,大家可以先看看有没有用得到的地方吧。

系列教程图片

2020Android复习资料汇总.png

flutter

NDK

设计思想开源框架

微信小程序

一个人可以走的很快,但一群人才能走的更远。不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎扫码加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
img
2655694984)]

[外链图片转存中…(img-eBNAKiIH-1712655694985)]

[外链图片转存中…(img-5XGw6yRt-1712655694985)]

[外链图片转存中…(img-TSK7Vjud-1712655694986)]

[外链图片转存中…(img-OpPr0WSR-1712655694986)]

[外链图片转存中…(img-8mGGh7Rs-1712655694986)]

一个人可以走的很快,但一群人才能走的更远。不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎扫码加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
[外链图片转存中…(img-LyZ2Q38M-1712655694987)]

  • 10
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值