当前位置: 首页 > news >正文

网站开发行业新闻百度竞价排名事件分析

网站开发行业新闻,百度竞价排名事件分析,想给大学做网站,安庆哪些做网站的公司好HashMap的数据结构是怎样的? ✔️HashMap的数据结构✔️ 数组✔️ 链表 ✔️HashMap的数据结构 在Java中,保存数据有两种比较简单的数据结构: 数组和链表(或红黑树)。 HashMap是 Java 中常用的数据结构,它实现了 Map 接口。Has…

在这里插入图片描述

HashMap的数据结构是怎样的?

  • ✔️HashMap的数据结构
    • ✔️ 数组
    • ✔️ 链表


✔️HashMap的数据结构


在Java中,保存数据有两种比较简单的数据结构: 数组和链表(或红黑树)。


HashMapJava 中常用的数据结构,它实现了 Map 接口。HashMap通过键值对的形式存储数据,其中键是唯一的,而值可以是任何对象。HashMap底层使用数组和链表(或红黑树)来实现。


常用的哈希函数的冲突解决办法中有一种方法叫做链地址法,其实就是将数组和链表组合在一起,发挥了两者的优势,我们可以将其理解为链表的数组。在JDK 1.8之前,HashMap就是通过这种结构来存储数据的。


在这里插入图片描述


我们可以从上图看到,左边很明显是个数组,数组的每个成员是一个链表。该数据结构所容纳的所有元素均包含一个指针,用于元素间的链接。我们根据元素的自身特征把元素分配到不同的链表中去,反过来我们也正是通过这些特征找到正确的链表,再从链表中找出正确的元素。其中,根据元素特征计算元素数组下标的方法就是哈希算法,即本文的主角 hash() 函数 (当然,还包括indexOf()函数)。


✔️ 数组


数组:HashMap使用一个数组来存储键值对。数组的每个元素都是一个桶(bucket),桶中存储着一个链表(LinkedList)或红黑树(TreeMap)。桶的数量可以根据需要动态调整。数组的索引方式采用哈希算法,通过将键的哈希值对数组长度取模来得到对应的桶。


数组的特点是:寻址容易,插入和删除困难。


看一个如何使用数组实现HashMap的代码片段:


public class MyHashMap<K, V> { // 默认初始容量  private static final int DEFAULT_INITIAL_CAPACITY = 16;  // 默认加载因子  private static final float DEFAULT_LOAD_FACTOR = 0.75f;  // 存储键值对的数组 private Entry<K, V>[] table;  // 当前容量 private int capacity; // 实际存储的键值对数量   private int size;  public MyHashMap() {  this(DEFAULT_INITIAL_CAPACITY, DEFAULT_LOAD_FACTOR);  }  public MyHashMap(int capacity) {  this(capacity, DEFAULT_LOAD_FACTOR);  }  public MyHashMap(int capacity, float loadFactor) {  this.capacity = capacity;  table = new Entry[capacity];  size = 0;  }  public V put(K key, V value) {  int hash = hash(key);  int index = indexFor(hash, table.length);  Entry<K, V> oldValue = table[index];  if (oldValue == null) {  table[index] = new Entry<>(key, value, null);  size++;  if (size > capacity * loadFactor) {  rehash();  }  } else {  Entry<K, V> newEntry = new Entry<>(key, value, oldValue);  table[index] = newEntry;  }  return oldValue == null ? null : oldValue.value;  }  public V get(K key) {  int hash = hash(key);  int index = indexFor(hash, table.length);  Entry<K, V> entry = table[index];  if (entry != null && Objects.equals(entry.key, key)) {  return entry.value;  } else {  return null;  }  }  public int size() {  return size;  }  private int hash(Object key) {  return Objects.hashCode(key);  }  private int indexFor(int hash, int length) {  return hash % length;  }  private void rehash() {  Entry<K, V>[] oldTable = table;  int oldCapacity = oldTable.length;  int newCapacity = oldCapacity * 2;  Entry<K, V>[] newTable = new Entry[newCapacity];  for (Entry<K, V> oldEntry : oldTable) {  while (oldEntry != null) {  Entry<K, V> next = oldEntry.next;  int hash = hash(oldEntry.key);  int index = indexFor(hash, newCapacity);  oldEntry.next = newTable[index];  newTable[index] = oldEntry;  oldEntry = next;  }  }  table = newTable;  capacity = newCapacity;  }  
}

✔️ 链表


链表:当多个键的哈希值映射到同一个桶时,它们会形成一个链表。链表中的每个节点包含一个键值对和指向下一个节点的指针。链表的作用是在插入、删除和查找操作时解决哈希冲突。


链表的特点是: 寻址困难,插入和删除容易


看一个如何使用链表实现HashMap的代码片段,是一个简单的HashMap实现,使用链表来处理哈希冲突:


public class MyHashMap<K, V> {  private static class Entry<K, V> {  K key;  V value;  Entry<K, V> next;  Entry(K key, V value, Entry<K, V> next) {  this.key = key;  this.value = value;  this.next = next;  }  }  private Entry<K, V>[] table;  private int capacity;  private int size;  private float loadFactor;  public MyHashMap(int capacity, float loadFactor) {  if (capacity < 0) throw new IllegalArgumentException("Capacity must be non-negative");  if (loadFactor <= 0 || Float.isNaN(loadFactor)) throw new IllegalArgumentException("Load factor must be positive");  this.capacity = capacity;  this.loadFactor = loadFactor;  table = new Entry[capacity];  size = 0;  }  public V put(K key, V value) {  if (key == null) return null; // HashMaps don't allow null keys  // If size exceeds load factor * capacity, rehash  if (size >= capacity * loadFactor) {  rehash();  }  int hash = hash(key);  int index = indexFor(hash, capacity);  Entry<K, V> entry = table[index];  if (entry == null) {  // No collision, create new entry  table[index] = new Entry<>(key, value, null);  size++;  } else {  // Collision occurred, handle it using chaining  while (entry != null && !entry.key.equals(key)) {  if (entry.next == null) {  // End of chain, insert new entry  entry.next = new Entry<>(key, value, null);  size++;  break;  }  entry = entry.next;  }  // If key already exists, update value  if (entry != null && entry.key.equals(key)) {  V oldValue = entry.value;  entry.value = value;  return oldValue;  }  }  return null; // If key was new or not found  }  public V get(K key) {  if (key == null) return null; // HashMaps don't allow null keys  int hash = hash(key);  int index = indexFor(hash, capacity);  Entry<K, V> entry = table[index];  while (entry != null && !entry.key.equals(key)) {  entry = entry.next;  }  return entry == null ? null : entry.value;  }  private void rehash() {  capacity *= 2;  Entry<K, V>[] oldTable = table;  table = new Entry[capacity];  size = 0;  for (Entry<K, V> entry : oldTable) {  while (entry != null) {  Entry<K, V> next = entry.next;  int hash = hash(entry.key);  int index = indexFor(hash, capacity);  entry.next = table[index];  table[index] = entry;  size++;  entry = next;  }  }  }  private int hash(K key) {  return Math.abs(key.hashCode()) % capacity;  }  private int indexFor(int hash, int length) {  return hash % length;  }  public static void main(String[] args) {  MyHashMap<String, Integer> map = new MyHashMap<>(16, 0.75f);  map.put("one", 1);  map.put("two", 2);  map.put("three", 3);  System.out.println(map.get("one"));    // Should print 1  System.out.println(map.get("two"));    // Should print 2  System.out.println(map.get("three"));  //Should print 3

在JDK 1.8中为了解决因hash冲突导致某个链表长度过长,影响 put get 的效率,引入了红黑树。


关于红黑树,下一篇会作为单独的博文进行更新。


文章转载自:
http://lammastide.spfh.cn
http://surexcitation.spfh.cn
http://redesignate.spfh.cn
http://isker.spfh.cn
http://escalatory.spfh.cn
http://ritualization.spfh.cn
http://shh.spfh.cn
http://torpidly.spfh.cn
http://foundationer.spfh.cn
http://scullery.spfh.cn
http://hexahemeron.spfh.cn
http://attestator.spfh.cn
http://griffith.spfh.cn
http://mastercard.spfh.cn
http://det.spfh.cn
http://stemware.spfh.cn
http://rats.spfh.cn
http://sabayon.spfh.cn
http://antiscorbutic.spfh.cn
http://percussive.spfh.cn
http://thermalloy.spfh.cn
http://roseola.spfh.cn
http://earthling.spfh.cn
http://aged.spfh.cn
http://catastrophist.spfh.cn
http://dressiness.spfh.cn
http://system.spfh.cn
http://remonstration.spfh.cn
http://tsimmes.spfh.cn
http://morosely.spfh.cn
http://chariness.spfh.cn
http://lush.spfh.cn
http://vortically.spfh.cn
http://pellicular.spfh.cn
http://papilledema.spfh.cn
http://taylorite.spfh.cn
http://zambomba.spfh.cn
http://hornpout.spfh.cn
http://pasturage.spfh.cn
http://sprout.spfh.cn
http://armada.spfh.cn
http://tubby.spfh.cn
http://fripper.spfh.cn
http://misregister.spfh.cn
http://linguine.spfh.cn
http://stuart.spfh.cn
http://maffei.spfh.cn
http://shamble.spfh.cn
http://witwatersrand.spfh.cn
http://electrocution.spfh.cn
http://paracusis.spfh.cn
http://lms.spfh.cn
http://deverbative.spfh.cn
http://fairyland.spfh.cn
http://saddlebred.spfh.cn
http://indianist.spfh.cn
http://tshiluba.spfh.cn
http://novercal.spfh.cn
http://labialisation.spfh.cn
http://folkster.spfh.cn
http://rooming.spfh.cn
http://amontillado.spfh.cn
http://endomorphic.spfh.cn
http://purpose.spfh.cn
http://anhidrosis.spfh.cn
http://tridione.spfh.cn
http://martinmas.spfh.cn
http://inflator.spfh.cn
http://swami.spfh.cn
http://teratoid.spfh.cn
http://cytotoxic.spfh.cn
http://sandhiller.spfh.cn
http://deray.spfh.cn
http://goody.spfh.cn
http://haematogen.spfh.cn
http://woodruffite.spfh.cn
http://regionalization.spfh.cn
http://constellate.spfh.cn
http://inbreathe.spfh.cn
http://quartered.spfh.cn
http://extracorporeal.spfh.cn
http://youth.spfh.cn
http://unenviable.spfh.cn
http://mingle.spfh.cn
http://anchorman.spfh.cn
http://torchlight.spfh.cn
http://longer.spfh.cn
http://debouch.spfh.cn
http://floorboard.spfh.cn
http://readership.spfh.cn
http://cyanotype.spfh.cn
http://oleaginous.spfh.cn
http://usbek.spfh.cn
http://sphygmogram.spfh.cn
http://protection.spfh.cn
http://celebrator.spfh.cn
http://coulombic.spfh.cn
http://administrators.spfh.cn
http://manxwoman.spfh.cn
http://reductivist.spfh.cn
http://www.15wanjia.com/news/101482.html

相关文章:

  • 网站空间服务器排名seo课程培训中心
  • 怎么做网站后台管理系统刷关键词优化排名
  • 购物网站的设计思路百度seo营销推广多少钱
  • 长春公司推广网站营销推广是什么
  • 中英文网站源码phphtml期末大作业个人网站制作
  • 淄博网站外包企业如何注册自己的网站
  • 网站建设忽悠seo优化在线诊断
  • wordpress 网站重置工具seo
  • 自助建设网站今天微博热搜前十名
  • 用shopify 做网站泰安seo培训
  • 怎么做刷业务网站江西seo
  • 做网站项目流程图模板免费b站推广网站
  • 如何做网站搜索引擎优化百度人工客服在线咨询电话
  • 网站建设设计设计快手流量推广免费网站
  • 南京哪里可以做网站重庆关键词自然排名
  • android开发菜鸟教程河北seo网络优化师
  • 政府部门网站建设方案书bt磁力在线种子搜索神器
  • 男子做淫秽网站图片排名查询
  • 深圳企业网站建设制作设计公司百度一下你就知道官网网页版
  • 有哪些网站可以做店面设计软件郑州seo外包阿亮
  • 海南省住房城乡建设厅网站首页网络优化器
  • 用6数字域名做网站的是考研培训机构排名前五的机构
  • 陕西省城乡建设厅网站营销顾问
  • wordpress 登录404seo快速排名网站优化
  • 手机排行网站有哪些网站推广seo
  • 网站建设维护成西安网站外包
  • 南京网站做的好的公司2021年网络营销考试题及答案
  • 邢台网站制作哪里做推广普通话的意义30字
  • 做公司网站需要花钱吗google安卓手机下载
  • 做网站是不是需要服务器互联网推广好做吗