热门标签 | 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


推荐阅读
  • XML介绍与使用的概述及标签规则
    本文介绍了XML的基本概念和用途,包括XML的可扩展性和标签的自定义特性。同时还详细解释了XML标签的规则,包括标签的尖括号和合法标识符的组成,标签必须成对出现的原则以及特殊标签的使用方法。通过本文的阅读,读者可以对XML的基本知识有一个全面的了解。 ... [详细]
  • Iamtryingtomakeaclassthatwillreadatextfileofnamesintoanarray,thenreturnthatarra ... [详细]
  • 云原生边缘计算之KubeEdge简介及功能特点
    本文介绍了云原生边缘计算中的KubeEdge系统,该系统是一个开源系统,用于将容器化应用程序编排功能扩展到Edge的主机。它基于Kubernetes构建,并为网络应用程序提供基础架构支持。同时,KubeEdge具有离线模式、基于Kubernetes的节点、群集、应用程序和设备管理、资源优化等特点。此外,KubeEdge还支持跨平台工作,在私有、公共和混合云中都可以运行。同时,KubeEdge还提供数据管理和数据分析管道引擎的支持。最后,本文还介绍了KubeEdge系统生成证书的方法。 ... [详细]
  • Java容器中的compareto方法排序原理解析
    本文从源码解析Java容器中的compareto方法的排序原理,讲解了在使用数组存储数据时的限制以及存储效率的问题。同时提到了Redis的五大数据结构和list、set等知识点,回忆了作者大学时代的Java学习经历。文章以作者做的思维导图作为目录,展示了整个讲解过程。 ... [详细]
  • 阿,里,云,物,联网,net,core,客户端,czgl,aliiotclient, ... [详细]
  • 使用Ubuntu中的Python获取浏览器历史记录原文: ... [详细]
  • 本文介绍了Hyperledger Fabric外部链码构建与运行的相关知识,包括在Hyperledger Fabric 2.0版本之前链码构建和运行的困难性,外部构建模式的实现原理以及外部构建和运行API的使用方法。通过本文的介绍,读者可以了解到如何利用外部构建和运行的方式来实现链码的构建和运行,并且不再受限于特定的语言和部署环境。 ... [详细]
  • 使用在线工具jsonschema2pojo根据json生成java对象
    本文介绍了使用在线工具jsonschema2pojo根据json生成java对象的方法。通过该工具,用户只需将json字符串复制到输入框中,即可自动将其转换成java对象。该工具还能解析列表式的json数据,并将嵌套在内层的对象也解析出来。本文以请求github的api为例,展示了使用该工具的步骤和效果。 ... [详细]
  • Java学习笔记之面向对象编程(OOP)
    本文介绍了Java学习笔记中的面向对象编程(OOP)内容,包括OOP的三大特性(封装、继承、多态)和五大原则(单一职责原则、开放封闭原则、里式替换原则、依赖倒置原则)。通过学习OOP,可以提高代码复用性、拓展性和安全性。 ... [详细]
  • Go GUIlxn/walk 学习3.菜单栏和工具栏的具体实现
    本文介绍了使用Go语言的GUI库lxn/walk实现菜单栏和工具栏的具体方法,包括消息窗口的产生、文件放置动作响应和提示框的应用。部分代码来自上一篇博客和lxn/walk官方示例。文章提供了学习GUI开发的实际案例和代码示例。 ... [详细]
  • Go Cobra命令行工具入门教程
    本文介绍了Go语言实现的命令行工具Cobra的基本概念、安装方法和入门实践。Cobra被广泛应用于各种项目中,如Kubernetes、Hugo和Github CLI等。通过使用Cobra,我们可以快速创建命令行工具,适用于写测试脚本和各种服务的Admin CLI。文章还通过一个简单的demo演示了Cobra的使用方法。 ... [详细]
  • 本文讨论了在openwrt-17.01版本中,mt7628设备上初始化启动时eth0的mac地址总是随机生成的问题。每次随机生成的eth0的mac地址都会写到/sys/class/net/eth0/address目录下,而openwrt-17.01原版的SDK会根据随机生成的eth0的mac地址再生成eth0.1、eth0.2等,生成后的mac地址会保存在/etc/config/network下。 ... [详细]
  • r2dbc配置多数据源
    R2dbc配置多数据源问题根据官网配置r2dbc连接mysql多数据源所遇到的问题pom配置可以参考官网,不过我这样配置会报错我并没有这样配置将以下内容添加到pom.xml文件d ... [详细]
  • Final关键字的含义及用法详解
    本文详细介绍了Java中final关键字的含义和用法。final关键字可以修饰非抽象类、非抽象类成员方法和变量。final类不能被继承,final类中的方法默认是final的。final方法不能被子类的方法覆盖,但可以被继承。final成员变量表示常量,只能被赋值一次,赋值后值不再改变。文章还讨论了final类和final方法的应用场景,以及使用final方法的两个原因:锁定方法防止修改和提高执行效率。 ... [详细]
  • 个人学习使用:谨慎参考1Client类importcom.thoughtworks.gauge.Step;importcom.thoughtworks.gauge.T ... [详细]
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社区 版权所有