欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

java固定大小隊列的幾種實現(xiàn)方式詳解

 更新時間:2021年07月15日 11:34:31   作者:wowojyc藝超  
隊列的特點是節(jié)點的排隊次序和出隊次序按入隊時間先后確定,即先入隊者先出隊,后入隊者后出隊,這篇文章主要給大家介紹了關(guān)于java固定大小隊列的幾種實現(xiàn)方式,需要的朋友可以參考下

前言

最近團(tuán)隊有同學(xué)在開發(fā)中,遇到一個需求,統(tǒng)計最近10次的異常次數(shù),咨詢有沒有類似的list。針對這個問題,記錄一下幾種處理方式。

基于Hutool中的FixedLinkedHashMap

引入maven依賴

<dependency>
   <groupId>cn.hutool</groupId>
   <artifactId>hutool-all</artifactId>
   <version>5.4.0</version>
</dependency>

使用示例

// 初始化時指定大小
private final FixedLinkedHashMap<String, Integer> fixedLinkedHashMap = new FixedLinkedHashMap<>(LIST_SIZE);

// 其余寫入和讀取操作同LinkedHashMap類似
// 對于key,可以按照自己的業(yè)務(wù)需求填寫
fixedLinkedHashMap.put(UUID.randomUUID().toString(), 1);

// 讀取操作
// 獲取元素個數(shù)
long size = fixedLinkedHashMap.values().size();
// 統(tǒng)計其中的總和
int sum = fixedLinkedHashMap.values().stream().mapToInt(value -> value).sum();

基于Guava的EvictingQueue

引入maven依賴

<dependency>
   <groupId>com.google.guava</groupId>
   <artifactId>guava</artifactId>
   <version>30.1.1-jre</version>
</dependency>

使用示例

// 初始化時指定大小
private final EvictingQueue<Integer> evictingQueue = EvictingQueue.create(LIST_SIZE);

// 添加元素
evictingQueue.add(MOCK_EXCEPTION_COUNT);

// 讀取元素
// 元素個數(shù)
size = evictingQueue.size();
// 統(tǒng)計其中的和
sum = evictingQueue.stream().mapToInt(value -> value).sum();

注意: 引入了目前(2021-07-12)最新的guava版本30.1.1-jre,EvictingQueue類還是標(biāo)記了@Beta。

基于Redis的list操作

示例在SpringBoot應(yīng)用中,使用RedisTemplate。

主要基于Redis列表的三個操作。

  1. LPUSH:將元素插入頭部
  2. LTRIM: 對列表進(jìn)行裁剪,可以指定起始位置
  3. LRANGE: 獲取列表指定范圍內(nèi)的元素

引入maven依賴

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

使用示例

// 初始化RedisTemplate
@Resource
private RedisTemplate<String, Integer> redisTemplate;


ListOperations<String, Integer> stringIntegerListOperations = redisTemplate.opsForList();
// LPUSH操作
stringIntegerListOperations.leftPush(REDIS_LIST_KEY, MOCK_EXCEPTION_COUNT);
// LTRIM
stringIntegerListOperations.trim(REDIS_LIST_KEY, 0, LIST_SIZE - 1);
// LRANGE操作
List<Integer> range = stringIntegerListOperations.range(REDIS_LIST_KEY, 0, LIST_SIZE - 1);

// 對結(jié)果操作
size = range.size();
sum = range.stream().mapToInt(value -> value).sum();

完整示例

@Service
@Slf4j
public class FixedListScheduler {

  private static final int LIST_SIZE = 10;

  private static final int MOCK_EXCEPTION_COUNT = 1;

  private static final String REDIS_LIST_KEY = "redis_fixed_list";

  private final FixedLinkedHashMap<String, Integer> fixedLinkedHashMap = new FixedLinkedHashMap<>(LIST_SIZE);

  private final EvictingQueue<Integer> evictingQueue = EvictingQueue.create(LIST_SIZE);

  @Resource
  private RedisTemplate<String, Integer> redisTemplate;

  @Scheduled(cron = "*/1 * * * * ?")
  public void schedule() {
    fixedLinkedHashMap.put(UUID.randomUUID().toString(), MOCK_EXCEPTION_COUNT);

    long size = fixedLinkedHashMap.values().size();
    int sum = fixedLinkedHashMap.values().stream().mapToInt(value -> value).sum();

    log.info("fixedLinkedHashMap size:{}, sum:{}", size, sum);
    evictingQueue.add(MOCK_EXCEPTION_COUNT);
    size = evictingQueue.size();
    sum = evictingQueue.stream().mapToInt(value -> value).sum();
    log.info("evictingQueue size:{}, sum:{}", size, sum);

    ListOperations<String, Integer> stringIntegerListOperations = redisTemplate.opsForList();
    stringIntegerListOperations.leftPush(REDIS_LIST_KEY, MOCK_EXCEPTION_COUNT);
    stringIntegerListOperations.trim(REDIS_LIST_KEY, 0, LIST_SIZE - 1);

    List<Integer> range = stringIntegerListOperations.range(REDIS_LIST_KEY, 0, LIST_SIZE - 1);
    if (!CollectionUtils.isEmpty(range)) {
      sum = range.stream().mapToInt(value -> value).sum();
      log.info("redis FixedList size:{}, sum:{}", range.size(), sum);
    }
  }
}

程序啟動一段時間后的日志,可以看到是滿足要求的。

2021-07-12 18:35:29.006  INFO 8622 --- [pool-2-thread-1] com.yichao.myblogs.FixedListScheduler    : evictingQueue size:10, sum:10
2021-07-12 18:35:29.009  INFO 8622 --- [pool-2-thread-1] com.yichao.myblogs.FixedListScheduler    : redis FixedList size:10, sum:10
2021-07-12 18:35:30.002  INFO 8622 --- [pool-2-thread-1] com.yichao.myblogs.FixedListScheduler    : fixedLinkedHashMap size:10, sum:10
2021-07-12 18:35:30.002  INFO 8622 --- [pool-2-thread-1] com.yichao.myblogs.FixedListScheduler    : evictingQueue size:10, sum:10
2021-07-12 18:35:30.005  INFO 8622 --- [pool-2-thread-1] com.yichao.myblogs.FixedListScheduler    : redis FixedList size:10, sum:10
2021-07-12 18:35:31.005  INFO 8622 --- [pool-2-thread-1] com.yichao.myblogs.FixedListScheduler    : fixedLinkedHashMap size:10, sum:10
2021-07-12 18:35:31.005  INFO 8622 --- [pool-2-thread-1] com.yichao.myblogs.FixedListScheduler    : evictingQueue size:10, sum:10
2021-07-12 18:35:31.008  INFO 8622 --- [pool-2-thread-1] com.yichao.myblogs.FixedListScheduler    : redis FixedList size:10, sum:10
2021-07-12 18:35:32.005  INFO 8622 --- [pool-2-thread-1] com.yichao.myblogs.FixedListScheduler    : fixedLinkedHashMap size:10, sum:10
2021-07-12 18:35:32.005  INFO 8622 --- [pool-2-thread-1] com.yichao.myblogs.FixedListScheduler    : evictingQueue size:10, sum:10
2021-07-12 18:35:32.009  INFO 8622 --- [pool-2-thread-1] com.yichao.myblogs.FixedListScheduler    : redis FixedList size:10, sum:10
2021-07-12 18:35:33.002  INFO 8622 --- [pool-2-thread-1] com.yichao.myblogs.FixedListScheduler    : fixedLinkedHashMap size:10, sum:10
2021-07-12 18:35:33.002  INFO 8622 --- [pool-2-thread-1] com.yichao.myblogs.FixedListScheduler    : evictingQueue size:10, sum:10
2021-07-12 18:35:33.005  INFO 8622 --- [pool-2-thread-1] com.yichao.myblogs.FixedListScheduler    : redis FixedList size:10, sum:10

總結(jié)

以上三種方式均可實現(xiàn)固定長度的list。FixedLinkedHashMap和EvictingQueue是基于內(nèi)存的,所以僅支持節(jié)點情況。而基于Redis的list除了單節(jié)點情況,同樣可以在分布式情況使用。

到此這篇關(guān)于java固定大小隊列的文章就介紹到這了,更多相關(guān)java固定大小隊列內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Springboot項目升級2.2.x升至2.7.x的示例代碼

    Springboot項目升級2.2.x升至2.7.x的示例代碼

    本文主要介紹了Springboot項目升級2.2.x升至2.7.x的示例代碼,會有很多的坑,具有一定的參考價值,感興趣的可以了解一下
    2023-09-09
  • Spring單數(shù)據(jù)源的配置詳解

    Spring單數(shù)據(jù)源的配置詳解

    spring數(shù)據(jù)源的配置網(wǎng)絡(luò)上有很多例子,這里我也來介紹一下單數(shù)據(jù)源配置的例子,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-08-08
  • Java定時任務(wù)schedule和scheduleAtFixedRate的異同

    Java定時任務(wù)schedule和scheduleAtFixedRate的異同

    本文主要介紹了Java定時任務(wù)schedule和scheduleAtFixedRate的異同,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-05-05
  • 解決springSecurity 使用默認(rèn)登陸界面登錄后無法跳轉(zhuǎn)問題

    解決springSecurity 使用默認(rèn)登陸界面登錄后無法跳轉(zhuǎn)問題

    這篇文章主要介紹了解決springSecurity 使用默認(rèn)登陸界面登錄后無法跳轉(zhuǎn)問題,項目環(huán)境springboot下使用springSecurity 版本2.7.8,本文通過實例代碼給大家介紹的非常詳細(xì),需要的朋友參考下吧
    2023-12-12
  • hibernate查詢緩存詳細(xì)分析

    hibernate查詢緩存詳細(xì)分析

    這篇文章主要介紹了hibernate查詢緩存詳細(xì)分析,包括查詢緩存配置方法及關(guān)閉二級緩存的詳細(xì)介紹,需要的朋友參考下本文吧
    2017-09-09
  • Java判斷當(dāng)前日期是周幾的方法匯總

    Java判斷當(dāng)前日期是周幾的方法匯總

    在Java編程中,我們經(jīng)常會遇到需要獲取當(dāng)前日期是周幾的需求。根據(jù)國際慣例,一周通常是從周一開始,到周日結(jié)束,記作1至7,本文將介紹幾種常用的Java方法,讓你能夠準(zhǔn)確地判斷當(dāng)前日期是周幾,感興趣的朋友一起看看吧
    2024-03-03
  • Spring ApplicationListener的使用詳解

    Spring ApplicationListener的使用詳解

    這篇文章主要介紹了Spring ApplicationListener的使用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-06-06
  • Deepin系統(tǒng)安裝eclipse2021-03及CDT插件的安裝教程

    Deepin系統(tǒng)安裝eclipse2021-03及CDT插件的安裝教程

    本教程教大家deepin20.1操作系統(tǒng)上安裝eclipse_2021-03版的詳細(xì)步驟及CDT插件的安裝方法,通過圖文展示的非常明了,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧
    2021-06-06
  • 一文帶你徹底理解Java序列化和反序列化

    一文帶你徹底理解Java序列化和反序列化

    這篇文章主要介紹了Java序列化和反序列化的相關(guān)資料,幫助大家更好的理解和學(xué)習(xí)Java,感興趣的朋友可以了解下
    2020-09-09
  • Java實現(xiàn)字符串反轉(zhuǎn)

    Java實現(xiàn)字符串反轉(zhuǎn)

    這篇文章介紹了Java實現(xiàn)字符串反轉(zhuǎn)的方法,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-04-04

最新評論