【问题标题】:How to declare final HashMap that should not allow to update or remove element如何声明不允许更新或删除元素的最终 HashMap
【发布时间】:2013-07-30 10:56:18
【问题描述】:

我有一个类似的哈希映射,

public static void main(String[] args) {
    final Map<String, String> daysMap = new HashMap(7);
    daysMap.put("1", "Sunday");
    daysMap.put("2", "Monday");
    daysMap.put("3", "Tuesday");
    daysMap.put("4", "Wednesday");
    daysMap.put("5", "Thursday");
    daysMap.put("6", "Friday");
    daysMap.put("7", "Saturday");
}

在这张地图中
1. 不允许放置超过 7 个元素
2. 不应该更新对应键的值 [like daysMap.put("5", "xxx");]
3. 不允许删除任何键

怎么办?

【问题讨论】:

  • Java?所有值在编译时都已知吗?
  • 是的java。我们在编译时不知道。
  • 顺便说一句,容量最终将成为16 的默认值,因此将其设置为7 将无济于事。

标签: java collections hashmap


【解决方案1】:

Collections.unmodifiableMap 涵盖了第 2 点和第 3 点。为了涵盖第一点,您可以添加手写测试。

【讨论】:

    【解决方案2】:

    你可以实现一个新的HashMap

    public class CoolMap<K, V> extends HashMap<K, V> {
    
    @Override
    public V put(K key, V value) {
    
        if (size() == 7) {
            throw new IllegalStateException("Size is at max!");
        } else {
    
            // If there is something already with that key
            if (containsKey(value)) {
                // do nothing
                return value;
            } else {
                // put inside
                return super.put(key, value);
            }
    
        }
    
    }
    
    @Override
    public void putAll(Map<? extends K, ? extends V> collection) {
    
        if (collection.size() > 7) {
            throw new IllegalStateException("Size is at max!");
        } else {
            super.putAll(collection);
        }
    
    }
    
    @Override
    public V remove(Object key) {
        return null;// doesn't remove anything
    }
    

    【讨论】:

      【解决方案3】:

      正如已经讨论过的,第 2 点和第 3 点是这样介绍的

      import java.util.*;
      
          public class OP2 {
             public static void main(String[] s) {
                //object hash table 
                Hashtable<String,String> table = new Hashtable<String,String>();
                table.
      
                // populate the table
                table.put("1", "Sunday");
                table.put("2", "Monday");
                table.put("3", "Tuesday");
                table.put("4", "Wednesday");
                table.put("5", "Thursday");
                table.put("6", "Friday");
                table.put("7", "Saturday");
      
                System.out.println("Initial collection: "+table);
      
                // create unmodifiable map
                Map m = Collections.unmodifiableMap(table);
      
                // try to modify the collection
               // m.put("key3", "value3");
               //Uncomment the above line and an error is obtained
             }
          }
      

      此外,对于第一个问题,最好在填充地图时调用函数:-

       public boolean putAndTest(MyKey key, MyValue value) {
                  if (map.size() >= MAX && !map.containsKey(key)) {
                       return false;
                  } else {
                       map.put("Whatever you want");
                       return true;
                  }
              }
      

      【讨论】:

        【解决方案4】:

        您为什么不创建自己的包含私有哈希映射的对象,然后您可以允许该私有哈希映射上的哪些方法被公开?

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2022-09-27
          • 1970-01-01
          • 1970-01-01
          • 2022-08-17
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多