栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

chm使用 1.removeIf之 满足某个条件,则移除指定的key 2.使用场景是匹配队列中满4人开赛

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

chm使用 1.removeIf之 满足某个条件,则移除指定的key 2.使用场景是匹配队列中满4人开赛

package org.example.testremoveif;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public class Main {
    public static void main(String[] args) {
        Map map = new ConcurrentHashMap<>();
        map.put(1, "1");
        map.put(2, "2");
        map.put(55, "55");
        map.put(3, "3");
        map.put(4, "4");

        ScheduledExecutorService schedule = Executors.newScheduledThreadPool(5);
        schedule.scheduleAtFixedRate(() -> {
            // 满足条件,则移除
            map.entrySet().removeIf(entry -> {
                Integer key = entry.getKey();
                String value = entry.getValue();
                if (key < 4) {
                    System.out.println("移除key=" + key + " value=" + value + "剩余:" + map.size() + "  " + Thread.currentThread().getName());
                    return true;
                }

                System.out.println(key + " value=" + value + "剩余:" + map.size() + "  " + Thread.currentThread().getName());
                return false;
            });
        }, 1, 1, TimeUnit.SECONDS);
    }
}

使用场景:

如:匹配中,value是一个Room,则满4人开启一个房间。则匹配队列就可以使用这个Chm存储,然后开启定时器,进行定时移除房间满的人。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/336835.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号