Class LinkedForestMap<K,V>

java.lang.Object
org.miaixz.bus.core.center.map.LinkedForestMap<K,V>
Type Parameters:
K - key类型
V - 值类型
All Implemented Interfaces:
Map<K,TreeEntry<K,V>>, ForestMap<K,V>

public class LinkedForestMap<K,V> extends Object implements ForestMap<K,V>
ForestMap的基本实现。

该集合可以被视为以LinkedForestMap.TreeEntryNode.getKey()作为key,LinkedForestMap.TreeEntryNode实例作为value的LinkedHashMap。 使用时,将每一对键与值对视为一个LinkedForestMap.TreeEntryNode节点,节点的id即为LinkedForestMap.TreeEntryNode.getKey(), 任何情况下使用相同的key都将会访问到同一个节点。

节点通过key形成父子关系,并最终构成多叉树结构,多组平行的多叉树将在当前集合中构成森林。 使用者可以通过ForestMap本身的方法来对森林进行操作或访问, 也可以在获取到TreeEntry后,使用节点本身的方法对数进行操作或访问。

Since:
Java 17+
Author:
Kimi Liu
  • Constructor Details

    • LinkedForestMap

      public LinkedForestMap(boolean allowOverrideParent)
      构建LinkedForestMap
      Parameters:
      allowOverrideParent - 当指定节点已经与其他节点构成了父子关系,是否允许将该节点的父节点强制替换为指定节点
  • Method Details

    • size

      public int size()
      获取当前实例中的节点个数
      Specified by:
      size in interface Map<K,V>
      Returns:
      节点个数
    • isEmpty

      public boolean isEmpty()
      当前实例是否为空
      Specified by:
      isEmpty in interface Map<K,V>
      Returns:
      是否
    • containsKey

      public boolean containsKey(Object key)
      当前实例中是否存在key对应的节点
      Specified by:
      containsKey in interface Map<K,V>
      Parameters:
      key - data
      Returns:
      是否
    • containsValue

      public boolean containsValue(Object value)
      当前实例中是否存在对应的TreeEntry实例
      Specified by:
      containsValue in interface Map<K,V>
      Parameters:
      value - TreeEntry实例
      Returns:
      是否
    • get

      public TreeEntry<K,V> get(Object key)
      获取key对应的节点
      Specified by:
      get in interface Map<K,V>
      Parameters:
      key - data
      Returns:
      节点
    • remove

      public TreeEntry<K,V> remove(Object key)
      将指定节点从当前Map中删除
      • 若存在父节点或子节点,则将其断开其与父节点或子节点的引用关系;
      • 若同时存在父节点或子节点,则会在删除后将让子节点直接成为父节点的子节点,比如: 现有引用关系 a -> b -> c,删除 b 后,将有 a -> c
      Specified by:
      remove in interface Map<K,V>
      Parameters:
      key - 节点的key
      Returns:
      删除的且引用关系已经改变的节点,若key没有对应节点,则返回null
    • clear

      public void clear()
      将当前集合清空,并清除全部节点间的引用关系
      Specified by:
      clear in interface Map<K,V>
    • keySet

      public Set<K> keySet()
      返回当前实例中全部的key组成的Set集合
      Specified by:
      keySet in interface Map<K,V>
      Returns:
      集合
    • values

      public Collection<TreeEntry<K,V>> values()
      返回当前实例中全部TreeEntry组成的Collection集合
      Specified by:
      values in interface Map<K,V>
      Returns:
      集合
    • entrySet

      public Set<Map.Entry<K,TreeEntry<K,V>>> entrySet()
      由key与TreeEntry组成的键值对实体的Set集合。 注意,返回集合中Map.Entry.setValue(Object)不支持调用。
      Specified by:
      entrySet in interface Map<K,V>
      Returns:
      集合
    • putNode

      public LinkedForestMap.TreeEntryNode<K,V> putNode(K key, V value)
      添加一个节点
      • 若key对应节点不存在,则以传入的键值创建一个新的节点;
      • 若key对应节点存在,则将该节点的值替换为node指定的值;
      Specified by:
      putNode in interface ForestMap<K,V>
      Parameters:
      key - 节点的key
      value - 节点的value
      Returns:
      节点,若key已有对应节点,则返回具有旧值的节点,否则返回null
    • putLinkedNodes

      public void putLinkedNodes(K parentKey, V parentValue, K childKey, V childValue)
      同时添加父子节点:
      • parentKeychildKey对应的节点不存在,则会根据键值创建一个对应的节点;
      • parentKeychildKey对应的节点存在,则会更新对应节点的值;
      该操作等同于:
           TreeEntry<K, V>  parent = putNode(parentKey, parentValue);
           TreeEntry<K, V>  child = putNode(childKey, childValue);
           linkNodes(parentKey, childKey);
       
      Specified by:
      putLinkedNodes in interface ForestMap<K,V>
      Parameters:
      parentKey - 父节点的key
      parentValue - 父节点的value
      childKey - 子节点的key
      childValue - 子节点的值
    • putLinkedNodes

      public void putLinkedNodes(K parentKey, K childKey, V childValue)
      添加子节点,并为子节点指定父节点:
      • parentKeychildKey对应的节点不存在,则会根据键值创建一个对应的节点;
      • parentKeychildKey对应的节点存在,则会更新对应节点的值;
      Specified by:
      putLinkedNodes in interface ForestMap<K,V>
      Parameters:
      parentKey - 父节点的key
      childKey - 子节点的key
      childValue - 子节点的值
    • linkNodes

      public void linkNodes(K parentKey, K childKey, BiConsumer<TreeEntry<K,V>,TreeEntry<K,V>> consumer)
      为指定的节点建立父子关系,若parentKeychildKey对应节点不存在,则会创建一个对应的值为null的空节点
      Specified by:
      linkNodes in interface ForestMap<K,V>
      Parameters:
      parentKey - 父节点的key
      childKey - 子节点的key
      consumer - 对父节点和子节点的操作,允许为null
    • unlinkNode

      public void unlinkNode(K parentKey, K childKey)
      移除指定父节点与其直接关联的子节点间的引用关系,但是不会将该节点从集合中删除
      Specified by:
      unlinkNode in interface ForestMap<K,V>
      Parameters:
      parentKey - 父节点的key
      childKey - 子节点