Java程序 实现ConcurrentHashMap API

Java程序 实现ConcurrentHashMap API

ConcurrentHashMap 类遵守与HashTable相同的功能规范,并包括与HashTable的每个方法相对应的所有方法版本。HashTable支持检索的完全并发性和更新的可调并发性。ConcurrentHashMap的所有操作都是线程安全的,但检索操作不需要锁定,也就是说,它不支持以阻止所有访问的方式锁定整个表。这个API与程序中的HashTable是完全互通的。它存在于**java.util.concurrent包中。

ConcurrentHashMap扩展了AbstractMap<K,V> 和Object类。这个API不允许使用null作为键或值,它是Java集合框架的成员。

Type 参数:

  • K – 地图所维护的键的类型
  • V–映射到它的值的类型。

所有实现的接口。

Serializable, ConcurrentMap<K,V>, Map<K,V>

语法:

public class ConcurrentHashMap<K,V>
extends AbstractMap<K,V>
implements ConcurrentMap<K,V>, Serializable

构造函数:

  • ConcurrentHashMap() – 创建一个空的Map,初始容量为16,负载系数为0.75,并发级别为16。
  • ConcurrentHashMap(int initialCapacity) – 用给定的初始容量以及负载因子和并发级别的默认值创建一个空Map。
  • ConcurrentHashMap(int initialCapacity, float loadFactor) – 用给定的初始容量和给定的负载因子以及默认的并发级别创建一个空Map。
  • ConcurrentHashMap(int initialCapacity, float loadFactor, int concurrencyLevel) – 用给定的初始容量、负载系数和并发等级创建一个空的Map。
  • ConcurrentHashMap(Map<? extends K,? extends V> m) – 创建一个新的Map,其映射与Map中给出的映射相同。

代码:

// Java Program to Implement ConcurrentHashMap API
 
import java.util.Collection;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
 
public class ConcurrentMap<K, V> {
    private ConcurrentHashMap<K, V> hm;
 
    // creates an empty ConcurrentHashMap with initial
    // capacity 16 and load factor 0.75
    public ConcurrentMap()
    {
        hm = new ConcurrentHashMap<K, V>();
    }
 
    // creates an empty ConcurrentHashMap with given initial
    // capacity and load factor 0.75
    public ConcurrentMap(int incap)
    {
        hm = new ConcurrentHashMap<K, V>(incap);
    }
 
    // creates an empty ConcurrentHashMap with given initial
    // capacity and load factor
    public ConcurrentMap(int incap, float lf)
    {
        hm = new ConcurrentHashMap<K, V>(incap, lf);
    }
 
    // creates an empty ConcurrentHashMap with given initial
    // capacity, load factor and concurrency level
    public ConcurrentMap(int incap, float lf,
                         int concurrLevel)
    {
        hm = new ConcurrentHashMap<K, V>(incap, lf,
                                         concurrLevel);
    }
 
    // Creates an hashMap with the same values as the given
    // hashMap
    public ConcurrentMap(Map<? extends K, ? extends V> m)
    {
        hm = new ConcurrentHashMap<K, V>(m);
    }
 
    // Removes all the keys and values from the Map
    public void clear() { hm.clear(); }
 
    // Return true if the Map contains the given Key
    public boolean containsKey(Object k)
    {
        return hm.containsKey(k);
    }
 
    // Return true if the map contains keys that maps to the
    // given value
    public boolean containsValue(Object v)
    {
        return hm.containsValue(v);
    }
 
    // Returns a set view of the key-value pairs of the map
    public Set<Map.Entry<K, V> > entrySet()
    {
        return hm.entrySet();
    }
 
    // Return the value to which the given key is mapped
    public V get(Object k) { return hm.get(k); }
 
    // Return true if the map does not contains any
    // key-value pairs
    public boolean isEmpty() { return hm.isEmpty(); }
 
    // Returns a set view of the key-value pairs of the map
    public Set<K> keySet() { return hm.keySet(); }
 
    // Maps the given value to the given key in the map
    public V put(K k, V v) { return hm.put(k, v); }
 
    // Copies all the key-value pairs from one map to
    // another
    public void putAll(Map<? extends K, ? extends V> mp)
    {
        hm.putAll(mp);
    }
 
    // Removes the mapping of the given key from its value
    public V remove(Object k) { return hm.remove(k); }
 
    // Returns the size of the map
    public int size() { return hm.size(); }
 
    // Returns the collection view of the values of the map
    public Collection<V> values() { return hm.values(); }
 
    // Returns an enumeration of the given values of the map
    public Enumeration<V> elements()
    {
        return hm.elements();
    }
 
    // If the given key is not associated with the given
    // value then associate it.
    public V putIfAbsent(K k, V v)
    {
        return hm.putIfAbsent(k, v);
    }
 
    // Replaces the value for a key only if it is currently
    // mapped to some value.
    public V replace(K key, V value)
    {
        return hm.replace(key, value);
    }
 
    // Replaces the oldValue for a key only if it is
    // currently mapped to a given value. *
    public boolean replace(K key, V oValue, V nValue)
    {
        return hm.replace(key, oValue, nValue);
    }
 
    public static void main(String[] arg)
    {
        // Creating an object of the class
        ConcurrentMap<Integer, String> hm
            = new ConcurrentMap<Integer, String>();
 
        hm.put(1, "Amit");
        hm.put(2, "Ankush");
        hm.put(3, "Akshat");
        hm.put(4, "Tarun");
 
        // Creating another Map
        Map<Integer, String> hm2
            = new HashMap<Integer, String>();
        hm.putAll(hm2);
 
        System.out.println(
            "The Keys of the ConcurrentHashMap is ");
 
        Set<Integer> k = hm.keySet();
        Iterator<Integer> i = k.iterator();
 
        while (i.hasNext()) {
            System.out.print(i.next() + " ");
        }
        System.out.println();
 
        System.out.println(
            "The values of the ConcurrentHashMap is ");
 
        Collection<String> values = hm.values();
        Iterator<String> s = values.iterator();
 
        while (s.hasNext()) {
            System.out.print(s.next() + " ");
        }
        System.out.println();
 
        System.out.println(
            "The entry set of the ConcurrentHashMap is ");
 
        Iterator<Entry<Integer, String> > er;
        Set<Entry<Integer, String> > ent = hm.entrySet();
 
        er = ent.iterator();
 
        while (er.hasNext()) {
            System.out.println(er.next() + " ");
        }
 
        System.out.println(
            "The ConcurrentHashMap contains Key 3 :"
            + hm.containsKey(3));
 
        System.out.println(
            "The ConcurrentHashMap contains Tarun:"
            + hm.containsValue("Tarun"));
 
        System.out.println(
            "Put the key 10 with value Shikha  if not associated : "
            + hm.putIfAbsent(10, "Shikha"));
 
        System.out.println(
            "Replace key 3 oldvalue of Akshat and newvalue Pari :"
            + hm.replace(3, "Akshat", "Pari"));
 
        System.out.println(
            "The Size of the ConcurrentHashMap is "
            + hm.size());
 
        // Clearing the Concurrent map
        hm.clear();
 
        if (hm.isEmpty())
            System.out.println(
                "The ConcurrentHashMap is empty");
        else
            System.out.println(
                "The ConcurrentHashMap is not empty");
    }
}

输出

The Keys of the ConcurrentHashMap is 
1 2 3 4 
The values of the ConcurrentHashMap is 
Amit Ankush Akshat Tarun 
The entry set of the ConcurrentHashMap is 
1=Amit 
2=Ankush 
3=Akshat 
4=Tarun 
The ConcurrentHashMap contains Key 3 :true
The ConcurrentHashMap contains Tarun:true
Put the key 10 with value Shikha  if not associated : null
Replace key 3 oldvalue of Akshat and newvalue Pari :true
The Size of the ConcurrentHashMap is 5
The ConcurrentHashMap is empty

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程