Android提供的LruCache类简介,阿里三面

  1. private int maxSize; //规定的最大存储空间

  2. private int putCount;  //put的次数

  3. private int createCount;  //create的次数

  4. private int evictionCount;  //回收的次数

  5. private int hitCount;  //命中的次数

  6. private int missCount;  //丢失的次数

  7. /**

  8. * @param maxSize for caches that do not override {@link #sizeOf}, this is

  9. *     the maximum number of entries in the cache. For all other caches,

  10. *     this is the maximum sum of the sizes of the entries in this cache.

  11. */

  12. public LruCache(int maxSize) {

  13. if (maxSize <= 0) {

  14. throw new IllegalArgumentException(“maxSize <= 0”);

  15. }

  16. this.maxSize = maxSize;

  17. this.map = new LinkedHashMap<K, V>(0, 0.75f, true);

  18. }

  19. /**

  20. * Returns the value for {@code key} if it exists in the cache or can be

  21. * created by {@code #create}. If a value was returned, it is moved to the

  22. * head of the queue. This returns null if a value is not cached and cannot

  23. * be created. 通过key返回相应的item,或者创建返回相应的item。相应的item会移动到队列的头部,

  24. * 如果item的value没有被cache或者不能被创建,则返回null。

  25. */

  26. public final V get(K key) {

  27. if (key == null) {

  28. throw new NullPointerException(“key == null”);

  29. }

  30. V mapValue;

  31. synchronized (this) {

  32. mapValue = map.get(key);

  33. if (mapValue != null) {

  34. hitCount++;  //命中

  35. return mapValue;

  36. }

  37. missCount++;  //丢失

  38. }

  39. /*

  40. * Attempt to create a value. This may take a long time, and the map

  41. * may be different when create() returns. If a conflicting value was

  42. * added to the map while create() was working, we leave that value in

  43. * the map and release the created value.

  44. * 如果丢失了就试图创建一个item

  45. */

  46. V createdValue = create(key);

  47. if (createdValue == null) {

  48. return null;

  49. }

  50. synchronized (this) {

  51. createCount++;//创建++

  52. mapValue = map.put(key, createdValue);

  53. if (mapValue != null) {

  54. // There was a conflict so undo that last put

  55. //如果前面存在oldValue,那么撤销put()

  56. map.put(key, mapValue);

  57. } else {

  58. size += safeSizeOf(key, createdValue);

  59. }

  60. }

  61. if (mapValue != null) {

  62. entryRemoved(false, key, createdValue, mapValue);

  63. return mapValue;

  64. } else {

  65. trimToSize(maxSize);

  66. return createdValue;

  67. }

  68. }

  69. /**

  70. * Caches {@code value} for {@code key}. The value is moved to the head of

  71. * the queue.

  72. *

  73. * @return the previous value mapped by {@code key}.

  74. */

  75. public final V put(K key, V value) {

  76. if (key == null || value == null) {

  77. throw new NullPointerException(“key == null || value == null”);

  78. }

  79. V previous;

  80. synchronized (this) {

  81. putCount++;

  82. size += safeSizeOf(key, value);

  83. previous = map.put(key, value);

  84. if (previous != null) {  //返回的先前的value值

  85. size -= safeSizeOf(key, previous);

  86. }

  87. }

  88. if (previous != null) {

  89. entryRemoved(false, key, previous, value);

  90. }

  91. trimToSize(maxSize);

  92. return previous;

  93. }

  94. /**

  95. * @param maxSize the maximum size of the cache before returning. May be -1

  96. *     to evict even 0-sized elements.

  97. *  清空cache空间

  98. */

  99. private void trimToSize(int maxSize) {

  100. while (true) {

  101. K key;

  102. V value;

  103. synchronized (this) {

  104. if (size < 0 || (map.isEmpty() && size != 0)) {

  105. throw new IllegalStateException(getClass().getName()

  106. + “.sizeOf() is reporting inconsistent results!”);

  107. }

  108. if (size <= maxSize) {

  109. break;

  110. }

  111. Map.Entry<K, V> toEvict = map.eldest();

  112. if (toEvict == null) {

  113. break;

  114. }

  115. key = toEvict.getKey();

  116. value = toEvict.getValue();

  117. map.remove(key);

  118. size -= safeSizeOf(key, value);

  119. evictionCount++;

  120. }

  121. entryRemoved(true, key, value, null);

  122. }

  123. }

  124. /**

  125. * Removes the entry for {@code key} if it exists.

  126. * 删除key相应的cache项,返回相应的value

  127. * @return the previous value mapped by {@code key}.

  128. */

  129. public final V remove(K key) {

  130. if (key == null) {

  131. throw new NullPointerException(“key == null”);

  132. }

  133. V previous;

  134. synchronized (this) {

  135. previous = map.remove(key);

  136. if (previous != null) {

  137. size -= safeSizeOf(key, previous);

  138. }

  139. }

  140. if (previous != null) {

  141. entryRemoved(false, key, previous, null);

  142. }

  143. return previous;

  144. }

  145. /**

  146. * Called for entries that have been evicted or removed. This method is

  147. * invoked when a value is evicted to make space, removed by a call to

  148. * {@link #remove}, or replaced by a call to {@link #put}. The default

  149. * implementation does nothing.

  150. * 当item被回收或者删掉时调用。改方法当value被回收释放存储空间时被remove调用,

  151. * 或者替换item值时put调用,默认实现什么都没做。

  152. The method is called without synchronization: other threads may

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

深知大多数初中级安卓工程师,想要提升技能,往往是自己摸索成长,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

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

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频
如果你觉得这些内容对你有帮助,可以添加下面V无偿领取!(备注Android)
img

总结

其实要轻松掌握很简单,要点就两个:

  1. 找到一套好的视频资料,紧跟大牛梳理好的知识框架进行学习。
  2. 多练。 (视频优势是互动感强,容易集中注意力)

你不需要是天才,也不需要具备强悍的天赋,只要做到这两点,短期内成功的概率是非常高的。

对于很多初中级Android工程师而言,想要提升技能,往往是自己摸索成长,不成体系的学习效果低效漫长且无助。下面资料部分截图是我花费几个月时间整理的,诚意满满:特别适合有3-5年开发经验的Android程序员们学习。

你不需要是天才,也不需要具备强悍的天赋,只要做到这两点,短期内成功的概率是非常高的。

对于很多初中级Android工程师而言,想要提升技能,往往是自己摸索成长,不成体系的学习效果低效漫长且无助。下面资料部分截图是我花费几个月时间整理的,诚意满满:特别适合有3-5年开发经验的Android程序员们学习。

[外链图片转存中…(img-b1oU62nS-1710568646109)]

  • 19
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
A:Android中的LruCache是一种用于缓存的,可以在内存有限的情况下有效的管理缓存。以下是Android如何使用LruCache: 1. 首先,需要创建一个LruCache对象。在创建时,必须指定LruCache的大小,单位是字节。 2. 接着,在应用程序的代码中,可以将需要缓存的对象添加到LruCache中。 3. 当从LruCache中查询对象时,如果发现缓存中有该对象,就可以将其返回。 4. 如果缓存中没有所需对象,则需要重新生成该对象,并将其添加到缓存中。 下面是使用LruCache的示例代码: // 创建一个LruCache对象,指定大小为10MB LruCache<String, Bitmap> mMemoryCache = new LruCache<String, Bitmap>(10 * 1024 * 1024) { @Override protected int sizeOf(String key, Bitmap value) { // 返回一个值,表示缓存对象的大小 return value.getByteCount(); } }; // 将一个Bitmap对象添加到缓存中 String key = "imageKey"; Bitmap bitmap = loadBitmapFromSomeWhere(); mMemoryCache.put(key, bitmap); // 从缓存中获取一个Bitmap对象 Bitmap cachedBitmap = mMemoryCache.get(key); if (cachedBitmap != null) { // 如果缓存中有,则直接使用 showBitmap(cachedBitmap); } else { // 如果缓存中没有,则重新生成并添加到缓存中 Bitmap newBitmap = loadBitmapFromSomeWhere(); mMemoryCache.put(key, newBitmap); showBitmap(newBitmap); } 以上是Android如何使用LruCache的基本操作,具体的使用方法和实现细节可以根据具体业务需求进行调整。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值