Hutool Hutool
(opens new window)
🏡首页
📖指南
🎢最佳实践
💖支持
💡javaDoc (opens new window)
⏳更新记录 (opens new window)
  • 🍎gitee (opens new window)
  • 🍏github (opens new window)
(opens new window)
🏡首页
📖指南
🎢最佳实践
💖支持
💡javaDoc (opens new window)
⏳更新记录 (opens new window)
  • 🍎gitee (opens new window)
  • 🍏github (opens new window)
  • 快速入门

  • 核心(hutool-core)

    • AI(Hutool-ai)

    • 注解(annotation)

    • 数组(array)

    • JavaBean(bean)

    • 缓存(cache)

    • 类加载器(classloader)

    • 编码解码(codec)

    • 集合(collection)

    • 比较器(comparator)

    • 压缩(compress)

    • 类型转换(convert)

    • 数据(data)

    • 日期时间(date)

    • 异常(exception)

    • 函数(func)

    • IO流(io)

    • 语言特性(lang)

    • Map(map)

      • 简介
      • MapUtil
      • MapBuilder
      • MapJoiner
      • MapGetUtil
      • CaseInsensitiveMap
      • CamelCaseMap
      • BiMap
      • FixedLinkedHashMap
        • 功能介绍
        • 主要特性
        • 适用场景
        • 使用示例
          • 示例1:基本用法
          • 示例2:遍历FixedLinkedHashMap
          • 示例3:获取和设置容量
          • 示例4:与MapUtil结合使用
        • 注意事项
        • 方法列表
      • ForestMap
      • Dict
      • MultiValueMap
      • ListValueMap
      • SetValueMap
      • Table
      • SoftConcurrentMap
      • WeakConcurrentMap
      • WeakKeyConcurrentMap
      • ConcurrentLinkedHashMap
    • 数字数学(math)

    • 网络(net)

    • 对象池(pool)

    • 反射(reflect)

    • 正则(regex)

    • 服务提供(spi)

    • 聚合操作(stream)

    • 字符串文本(text)

    • 并发和线程(thread)

    • 树结构(tree)

    • 工具集合(util)

    • XML操作(xml)

  • 定时任务(hutool-cron)

  • 加密(hutool-crypto)

  • 数据库(hutool-db)

  • 扩展(hutool-extra)

  • HTTP(hutool-http)

  • 日志(hutool-log)

  • 配置文件(hutool-setting)

  • JSON(hutool-json)

  • Office办公(hutool-poi)

  • 套接字(hutool-socket)

  • GUI(hutool-swing)

  • 指南
  • 核心(hutool-core)
  • Map(map)
Hutool
2026-01-19
目录

FixedLinkedHashMap

# 功能介绍

FixedLinkedHashMap是Hutool中提供的固定大小的Map实现,它继承自LinkedHashMap,当添加新条目导致超过设定的最大容量时,会自动移除最旧的条目(按照插入顺序)。这种特性使其非常适合用作缓存或最近使用项的跟踪。

# 主要特性

  • 固定大小,超过容量时自动移除最旧条目
  • 保持插入顺序,移除时优先移除最早插入的条目
  • 基于LinkedHashMap实现,性能优异
  • 支持所有Map接口方法
  • 支持null键和null值
  • 线程不安全,建议在单线程环境下使用
  • 支持动态调整容量

# 适用场景

  1. 实现简单的缓存系统
  2. 跟踪最近使用的项
  3. 限制内存使用,避免Map无限增长
  4. 需要固定大小的数据集存储
  5. 实现LRU(最近最少使用)缓存的简化版本

# 使用示例

# 示例1:基本用法

// 创建容量为3的FixedLinkedHashMap
FixedLinkedHashMap<String, Object> fixedMap = new FixedLinkedHashMap<>(3);

// 添加3个键值对
fixedMap.put("a", 1);
fixedMap.put("b", 2);
fixedMap.put("c", 3);
// 此时Map内容: {a=1, b=2, c=3}

// 添加第4个键值对,超过容量,自动移除最旧的"a"
fixedMap.put("d", 4);
// 此时Map内容: {b=2, c=3, d=4}

// 添加已存在的键,会更新值,但不会改变插入顺序
fixedMap.put("b", 22);
// 此时Map内容: {b=22, c=3, d=4}

// 添加第5个键值对,自动移除最旧的"c"
fixedMap.put("e", 5);
// 此时Map内容: {b=22, d=4, e=5}

# 示例2:遍历FixedLinkedHashMap

FixedLinkedHashMap<String, Object> fixedMap = new FixedLinkedHashMap<>(3);
fixedMap.put("a", 1);
fixedMap.put("b", 2);
fixedMap.put("c", 3);
fixedMap.put("d", 4); // 移除"a"

// 遍历Map,保持插入顺序
fixedMap.forEach((key, value) -> {
    Console.log(key + "=" + value);
});
// 输出:
// b=2
// c=3
// d=4

# 示例3:获取和设置容量

FixedLinkedHashMap<String, Object> fixedMap = new FixedLinkedHashMap<>(3);

// 获取当前容量
int capacity = fixedMap.getCapacity();
// 输出: 3

// 动态调整容量为5
fixedMap.setCapacity(5);

// 添加5个键值对
for (int i = 1; i <= 5; i++) {
    fixedMap.put("key" + i, i);
}
// 此时Map内容: {key1=1, key2=2, key3=3, key4=4, key5=5}

// 再次调整容量为2
fixedMap.setCapacity(2);
// 此时Map内容: {key4=4, key5=5}(保留最后插入的2个条目)

# 示例4:与MapUtil结合使用

// 使用MapUtil创建FixedLinkedHashMap
Map<String, Object> fixedMap = MapUtil.newFixedLinkedHashMap(3);
fixedMap.put("a", 1);
fixedMap.put("b", 2);
fixedMap.put("c", 3);
fixedMap.put("d", 4); // 移除"a"

// 获取值
Object d = fixedMap.get("d");
// 输出: 4

# 注意事项

  1. FixedLinkedHashMap是线程不安全的,在多线程环境下使用需要额外同步
  2. 容量设置为0时,添加任何条目都会立即被移除
  3. 调整容量为更小时,会立即移除多余的最旧条目
  4. 基于插入顺序移除条目,而不是访问顺序(如需访问顺序,可使用LinkedHashMap的accessOrder=true)
  5. 性能与LinkedHashMap相当,适合大多数场景
  6. 支持null键和null值,但null键也会占用容量

# 方法列表

FixedLinkedHashMap继承自LinkedHashMap,除了支持所有Map接口方法外,还提供了以下特有方法:

方法名 描述 参数 返回值
FixedLinkedHashMap(int capacity) 创建指定容量的FixedLinkedHashMap capacity: 最大容量 无
getCapacity() 获取当前容量 无 int
setCapacity(int capacity) 设置新容量,超过当前大小时会立即移除多余条目 capacity: 新容量 void
put(K key, V value) 添加键值对,超过容量时自动移除最旧条目 key: 键, value: 值 V
putAll(Map<? extends K, ? extends V> map) 批量添加键值对,超过容量时自动移除最旧条目 map: 要添加的Map void
size() 获取当前Map大小 无 int
isEmpty() 判断Map是否为空 无 boolean
clear() 清空Map 无 void
BiMap
ForestMap

← BiMap ForestMap→

Theme by Vdoing | Copyright © 2025-2026 Hutool | Apache-2.0
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式