热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

实现ConcurrentHashMapAPI的Java程序

实现ConcurrentHashMapAPI的Java程序

实现 ConcurrentHashMap API 的 Java 程序

原文:https://www . geesforgeks . org/Java-程序到实现-concurrenthashmap-api/

【ConcurrentHashMap】类遵循与 HashTable 相同的功能规范,包含了 HashTable 中每个方法对应的所有版本的方法。哈希表支持检索的完全并发和更新的可调并发。ConcurrentHashMap 的所有操作都是线程安全的,但是检索操作不需要锁定,即它不支持以阻止所有访问的方式锁定整个表。这个应用编程接口对于程序中的哈希表是完全可互操作的。它存在于Java . util . concurrent package中。

ConcurrentHashMap 扩展了抽象映射和对象类。此应用编程接口不允许将空值用作键或值,并且它是 Java 集合框架的成员。

类型参数:


  • k–地图维护的按键类型

  • v–映射到它的值的类型。

所有实现的接口:

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

语法:

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

施工人员:


  • ConcurrentHashMap()–创建一个初始容量为 16、负载系数为 0.75、并发级别为 16 的空映射。

  • ConcurrentHashMap(int initial capacity)–使用给定的初始容量以及负载系数和并发级别的默认值创建一个空映射。

  • ConcurrentHashMap(int initial capacity,float load factor)–使用给定的初始容量和给定的负载系数以及默认的 concurrencyLevel 创建一个空映射。

  • ConcurrentHashMap(int initial capacity,float loadFactor,int concurrencyLevel)–用给定的初始容量、加载因子和 concurrency level 创建一个空映射。

  • ConcurrentHashMap(地图<?延伸 K,?扩展 V>m)–使用地图中给出的相同映射创建新地图。

代码:

Java 语言(一种计算机语言,尤用于创建网站)


// 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 asscociated : "
            + 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");
    }
}

Output

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 asscociated : null
Replace key 3 oldvalue of Akshat and newvalue Pari :true
The Size of the ConcurrentHashMap is 5
The ConcurrentHashMap is empty


推荐阅读
  • 本文详细介绍了Java中vector的使用方法和相关知识,包括vector类的功能、构造方法和使用注意事项。通过使用vector类,可以方便地实现动态数组的功能,并且可以随意插入不同类型的对象,进行查找、插入和删除操作。这篇文章对于需要频繁进行查找、插入和删除操作的情况下,使用vector类是一个很好的选择。 ... [详细]
  • 开发笔记:加密&json&StringIO模块&BytesIO模块
    篇首语:本文由编程笔记#小编为大家整理,主要介绍了加密&json&StringIO模块&BytesIO模块相关的知识,希望对你有一定的参考价值。一、加密加密 ... [详细]
  • Android中高级面试必知必会,积累总结
    本文介绍了Android中高级面试的必知必会内容,并总结了相关经验。文章指出,如今的Android市场对开发人员的要求更高,需要更专业的人才。同时,文章还给出了针对Android岗位的职责和要求,并提供了简历突出的建议。 ... [详细]
  • 本文介绍了OC学习笔记中的@property和@synthesize,包括属性的定义和合成的使用方法。通过示例代码详细讲解了@property和@synthesize的作用和用法。 ... [详细]
  • Java学习笔记之面向对象编程(OOP)
    本文介绍了Java学习笔记中的面向对象编程(OOP)内容,包括OOP的三大特性(封装、继承、多态)和五大原则(单一职责原则、开放封闭原则、里式替换原则、依赖倒置原则)。通过学习OOP,可以提高代码复用性、拓展性和安全性。 ... [详细]
  • 本文介绍了使用kotlin实现动画效果的方法,包括上下移动、放大缩小、旋转等功能。通过代码示例演示了如何使用ObjectAnimator和AnimatorSet来实现动画效果,并提供了实现抖动效果的代码。同时还介绍了如何使用translationY和translationX来实现上下和左右移动的效果。最后还提供了一个anim_small.xml文件的代码示例,可以用来实现放大缩小的效果。 ... [详细]
  • 本文介绍了使用Java实现大数乘法的分治算法,包括输入数据的处理、普通大数乘法的结果和Karatsuba大数乘法的结果。通过改变long类型可以适应不同范围的大数乘法计算。 ... [详细]
  • Java序列化对象传给PHP的方法及原理解析
    本文介绍了Java序列化对象传给PHP的方法及原理,包括Java对象传递的方式、序列化的方式、PHP中的序列化用法介绍、Java是否能反序列化PHP的数据、Java序列化的原理以及解决Java序列化中的问题。同时还解释了序列化的概念和作用,以及代码执行序列化所需要的权限。最后指出,序列化会将对象实例的所有字段都进行序列化,使得数据能够被表示为实例的序列化数据,但只有能够解释该格式的代码才能够确定数据的内容。 ... [详细]
  • 本文介绍了Java高并发程序设计中线程安全的概念与synchronized关键字的使用。通过一个计数器的例子,演示了多线程同时对变量进行累加操作时可能出现的问题。最终值会小于预期的原因是因为两个线程同时对变量进行写入时,其中一个线程的结果会覆盖另一个线程的结果。为了解决这个问题,可以使用synchronized关键字来保证线程安全。 ... [详细]
  • ASP.NET2.0数据教程之十四:使用FormView的模板
    本文介绍了在ASP.NET 2.0中使用FormView控件来实现自定义的显示外观,与GridView和DetailsView不同,FormView使用模板来呈现,可以实现不规则的外观呈现。同时还介绍了TemplateField的用法和FormView与DetailsView的区别。 ... [详细]
  • 本文讨论了clone的fork与pthread_create创建线程的不同之处。进程是一个指令执行流及其执行环境,其执行环境是一个系统资源的集合。在调用系统调用fork创建一个进程时,子进程只是完全复制父进程的资源,这样得到的子进程独立于父进程,具有良好的并发性。但是二者之间的通讯需要通过专门的通讯机制,另外通过fork创建子进程系统开销很大。因此,在某些情况下,使用clone或pthread_create创建线程可能更加高效。 ... [详细]
  • 本文介绍了如何在方法参数中指定一个对象的协议,以及如何调用符合该协议的方法。以一个具体的示例说明了如何在方法参数中指定一个UIView子类对象,并且该对象需要符合PixelUI协议,同时方法需要能够访问该对象的属性。 ... [详细]
  • Oracle seg,V$TEMPSEG_USAGE与Oracle排序的关系及使用方法
    本文介绍了Oracle seg,V$TEMPSEG_USAGE与Oracle排序之间的关系,V$TEMPSEG_USAGE是V_$SORT_USAGE的同义词,通过查询dba_objects和dba_synonyms视图可以了解到它们的详细信息。同时,还探讨了V$TEMPSEG_USAGE的使用方法。 ... [详细]
  • 本文介绍了Python爬虫技术基础篇面向对象高级编程(中)中的多重继承概念。通过继承,子类可以扩展父类的功能。文章以动物类层次的设计为例,讨论了按照不同分类方式设计类层次的复杂性和多重继承的优势。最后给出了哺乳动物和鸟类的设计示例,以及能跑、能飞、宠物类和非宠物类的增加对类数量的影响。 ... [详细]
  • OO第一单元自白:简单多项式导函数的设计与bug分析
    本文介绍了作者在学习OO的第一次作业中所遇到的问题及其解决方案。作者通过建立Multinomial和Monomial两个类来实现多项式和单项式,并通过append方法将单项式组合为多项式,并在此过程中合并同类项。作者还介绍了单项式和多项式的求导方法,并解释了如何利用正则表达式提取各个单项式并进行求导。同时,作者还对自己在输入合法性判断上的不足进行了bug分析,指出了自己在处理指数情况时出现的问题,并总结了被hack的原因。 ... [详细]
author-avatar
____L振豪
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有