株洲阿里云代理商:ArrayMap源码

株洲阿里云代理商是指阿里云的一个代理商机构,提供阿里云产品的代理和销售服务。而”ArrayMap”是一个 Android 框架中的一个数据结构,用于存储键值对的集合。

以下是 ArrayMap 的部分源码:

public class ArrayMap<K, V> {
    private static final int BASE_SIZE = 4;
    private static final int CACHE_SIZE = 10;
    
    private int[] mHashes;
    private Object[] mArray;
    private int mSize;

    public ArrayMap() {
        mHashes = ContainerHelpers.EMPTY_INTS;
        mArray = ContainerHelpers.EMPTY_OBJECTS;
        mSize = 0;
    }

    public int size() {
        return mSize;
    }

    public V put(K key, V value) {
        int hash = key == null ? 0 : key.hashCode();
        int index = indexOfKey(hash);

        if (index >= 0) {
            return setValueAt(index, value);
        }

        index = ~index;
        if (mSize >= mHashes.length) {
            int n = mSize >= (BASE_SIZE * 2) ? (mSize + (mSize >> 1))
                    : (mSize >= BASE_SIZE ? (BASE_SIZE * 2) : BASE_SIZE);

            int[] ohashes = mHashes;
            Object[] oarray = mArray;
            
            allocArrays(n);
            
            if (mHashes.length > 0) {
                System.arraycopy(ohashes, 0, mHashes, 0, ohashes.length);
                System.arraycopy(
                        oarray, 0, mArray, 0, oarray.length);
            }

            freeArrays(ohashes, oarray, mSize);
        }

        if (index < mSize) {
            System.arraycopy(mHashes, index, mHashes, index + 1,
                    mSize - index);
            System.arraycopy(mArray, index, mArray, index + 1,
                    mSize - index);
        }

        mHashes[index] = hash;
        mArray[index] = value;
        mSize++;

        return null;
    }

    private void allocArrays(int size) {
        if (size == (BASE_SIZE * 2)) {
            synchronized (ArrayMap.class) {
                if (ArrayMap_HashFunctions.isSmall(mSize, 0, BASE_SIZE) && 
                    mSize < (BASE_SIZE * 2)) {
                    if (Cache.kArrayMapSizeErrorCache != null) {
                        mHashes = Cache.kArrayMapSizeErrorCache.mHashes;
                        mArray = Cache.kArrayMapSizeErrorCache.mArray;
                        // Reset everything in the cache since we're taking it
                        // over.  Also reset the cache to a new object.  (New
                        // objects are clearly smaller than 2 * BASE_SIZE.)
                        Cache.kArrayMapSizeErrorCache.mHashes = null;
                        Cache.kArrayMapSizeErrorCache.mArray = null;
                        Cache.kArrayMapSizeErrorCache = new Cache();
                    }
                }
            }
        }
        mHashes = new int[size];
        mArray = new Object[size << 1];
    }

    private void freeArrays(int[] hashes, Object[] array, int size) {
        if (Cache.kArrayMapSizeErrorCache != null) {
            if (size == (BASE_SIZE * 2)) {
                Cache.kArrayMapSizeErrorCache.mHashes = hashes;
                Cache.kArrayMapSizeErrorCache.mArray = array;
            }
        }
        mSize = size;
    }
}

此源码为 ArrayMap 的简化版,仅显示了部分方法和变量。ArrayMap 通过使用两个数组 mHashes 和 mArray 来存储键值对,其中 mHashes 数组用于存储键的 hash 值,mArray 数组则用于存储键值对的实际数据。

ArrayMap 主要提供以下功能:

  • put(): 将键值对存储到 ArrayMap 中。如果键已经存在,则更新对应的值;如果键不存在,则添加新的键值对。
  • size(): 返回 ArrayMap 中键值对的数量。

注意:上述源码为简化版,实际 ArrayMap 的源码较为复杂,包含多个其他方法和内部类实现了不同的功能。完整的 ArrayMap 源码可以在 Android 源码中找到。

株洲阿里云代理商:ArrayMap源码

株洲阿里云代理商提供的ArrayMap源码如下:

package com.aliyun.demo;

import java.util.HashMap;
import java.util.Map;

public class ArrayMap<K, V> {

    private Entry<K, V>[] table;
    private int size;

    public ArrayMap() {
        table = new Entry[16];
        size = 0;
    }

    public V get(K key) {
        int hash = key.hashCode() % table.length;
        Entry<K, V> entry = table[hash];

        while(entry != null) {
            if(entry.key.equals(key)) {
                return entry.value;
            }
            entry = entry.next;
        }

        return null;
    }

    public void put(K key, V value) {
        int hash = key.hashCode() % table.length;
        Entry<K, V> entry = table[hash];

        if(entry == null) {
            table[hash] = new Entry<>(key, value);
            size++;
        } else {
            while(entry.next != null) {
                if(entry.key.equals(key)) {
                    entry.value = value;
                    return;
                }
                entry = entry.next;
            }

            if(entry.key.equals(key)) {
                entry.value = value;
            } else {
                entry.next = new Entry<>(key, value);
                size++;
            }
        }
    }

    public void remove(K key) {
        int hash = key.hashCode() % table.length;
        Entry<K, V> entry = table[hash];
        Entry<K, V> prev = null;

        while(entry != null) {
            if(entry.key.equals(key)) {
                if(prev == null) {
                    table[hash] = entry.next;
                } else {
                    prev.next = entry.next;
                }
                size--;
                return;
            }

            prev = entry;
            entry = entry.next;
        }
    }

    public int size() {
        return size;
    }

    private static class Entry<K, V> {
        private K key;
        private V value;
        private Entry<K, V> next;

        public Entry(K key, V value) {
            this.key = key;
            this.value = value;
            this.next = null;
        }
    }
}

这是一个简化的ArrayMap实现,用于存储键值对。该ArrayMap内部使用数组和链表来实现。具体实现方式如下:

  • 内部类Entry定义了键值对的结构,包含了一个键key,一个值value,以及一个指向下一个Entry的指针next。
  • ArrayMap类内部维护了一个Entry类型的数组table,用于存储实际的数据。
  • put()方法用于将指定的键值对加入到ArrayMap中。首先根据key的hashCode值计算出存储位置,然后检查该位置上是否已经存在Entry。如果不存在,则直接加入;如果存在,则需要遍历链表来找到对应的Entry,如果找到则更新value,否则插入到链表末尾。
  • get()方法根据key查找对应的value。首先根据key的hashCode值计算出存储位置,然后遍历链表来找到对应的Entry并返回其value。
  • remove()方法根据key删除对应的键值对。首先根据key的hashCode值计算出存储位置,然后遍历链表来找到对应的Entry并从链表中删除。
  • size()方法返回ArrayMap中键值对的个数。

请注意,这只是一个简单的ArrayMap实现,用于演示和理解ArrayMap的基本原理。实际上,Android平台上的ArrayMap实现更为复杂和高效。

发布者:luotuoemo,转转请注明出处:https://www.jintuiyun.com/115812.html

(0)
luotuoemo的头像luotuoemo
上一篇 2023年12月28日 12:19
下一篇 2023年12月28日 12:42

相关推荐

  • 威海阿里云代理商:阿里云只有内网ip

    阿里云:内网IP的优势与应用 引言 阿里云作为国内领先的云计算服务提供商,其提供的内网IP功能在企业应用和网络架构中发挥着重要作用。本文将介绍阿里云内网IP的优势和应用场景,以帮助读者更好地了解和使用阿里云。 内网IP的优势 1. 网络安全性加强 使用阿里云内网IP可以避免公网暴露,减少网络攻击的风险。内网IP只在阿里云内部通信,不会直接面向公网,大大增强了…

    2024年1月22日
    15600
  • 张掖阿里云企业邮箱代理商:钉钉设置邮箱签名

    阿里云企业邮箱代理商:钉钉设置邮箱签名 引言 在当今互联网时代,电子邮件已成为商务沟通的重要工具之一。阿里云企业邮箱作为一款强大稳定的企业级邮箱服务,其与钉钉的整合更是为企业提供了更高效的沟通协作方式。本文将介绍如何在钉钉上设置邮箱签名,并结合阿里云企业邮箱的优势和好用之处进行说明。 设置钉钉邮箱签名 1. 打开钉钉应用,进入工作台页面。 2. 点击右上角个…

    2024年1月26日
    16100
  • 阿里云短信签名规范

    阿里云短信签名规范如下: 个人用户签名要求: 需要提供个人证件的正反面扫描件,并保证所提供的信息真实有效。 签名长度不能超过15个字符。 企业用户签名要求: 需要提供企业营业执照的副本,并保证所提供的信息真实有效。 签名长度不能超过10个字符。 签名规范要求: 签名只能包含中文、英文大小写、数字和部分特殊字符(冒号、英文组合符号、全角符号等)。 签名不能包含…

    2023年10月22日
    16900
  • 盘锦阿里云企业邮箱代理商:阿里巴巴企业邮箱登陆入口

    盘锦阿里云企业邮箱代理商:阿里巴巴企业邮箱登陆入口 阿里巴巴企业邮箱是中国知名的企业级邮件服务提供商,而在盘锦地区,搭配阿里云企业邮箱代理商,可以享受更多优势。本文将介绍阿里云企业邮箱和阿里云企业邮箱代理商的优势,并给出一段总结。 阿里云企业邮箱的优势 阿里云企业邮箱是由阿里云推出的一款专业邮件服务,在市场上拥有广泛的用户群体。其优势如下: 稳定可靠:阿里云…

    2024年2月13日
    17100
  • 新沂阿里云代理商:阿里云云数据库RDS MySQL如何进行数据备份和恢复的监控和报警?

    阿里云云数据库RDS MySQL的数据备份和恢复监控以及报警可以通过以下步骤进行设置: 登录阿里云控制台,选择RDS实例所在的地域和左侧导航栏中的“云数据库RDS”模块。 在RDS实例列表中选择目标实例,进入实例详情页面。 在实例详情页面,选择“备份与恢复”标签页,可以进行备份设置和监控报警的配置。 点击“备份策略设置”进入备份策略页面,可以设置备份周期、备…

    2023年11月14日
    19400

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

联系我们

4000-747-360

在线咨询: QQ交谈

邮件:ixuntao@qq.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信
购买阿里云服务器请访问:https://www.4526.cn/