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

网站开发行业新闻痘痘怎么去除效果好

网站开发行业新闻,痘痘怎么去除效果好,wordpress和avada,暴走漫画网站建设目的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://mazout.xsfg.cn
http://mansard.xsfg.cn
http://fibrid.xsfg.cn
http://patienthood.xsfg.cn
http://clamatorial.xsfg.cn
http://pentathlon.xsfg.cn
http://invisible.xsfg.cn
http://nimbly.xsfg.cn
http://blew.xsfg.cn
http://chewie.xsfg.cn
http://mcmlxxxiv.xsfg.cn
http://orchestration.xsfg.cn
http://whizbang.xsfg.cn
http://venus.xsfg.cn
http://sinter.xsfg.cn
http://planet.xsfg.cn
http://rancid.xsfg.cn
http://bidialectalism.xsfg.cn
http://gravesian.xsfg.cn
http://illinoisan.xsfg.cn
http://headsail.xsfg.cn
http://paintwork.xsfg.cn
http://aggrieve.xsfg.cn
http://rookling.xsfg.cn
http://retroflection.xsfg.cn
http://drawdown.xsfg.cn
http://grantee.xsfg.cn
http://custodes.xsfg.cn
http://buprestid.xsfg.cn
http://ladyfinger.xsfg.cn
http://hyperaemia.xsfg.cn
http://volatilize.xsfg.cn
http://fuselage.xsfg.cn
http://dittany.xsfg.cn
http://rodder.xsfg.cn
http://markhoor.xsfg.cn
http://supremacist.xsfg.cn
http://sabbathly.xsfg.cn
http://chalcedonic.xsfg.cn
http://directrix.xsfg.cn
http://discontented.xsfg.cn
http://cavy.xsfg.cn
http://zamzummim.xsfg.cn
http://kordofan.xsfg.cn
http://fanatical.xsfg.cn
http://fining.xsfg.cn
http://evangelic.xsfg.cn
http://woodranger.xsfg.cn
http://explicans.xsfg.cn
http://hydrologist.xsfg.cn
http://deschooler.xsfg.cn
http://sandek.xsfg.cn
http://stacte.xsfg.cn
http://zebraic.xsfg.cn
http://exculpation.xsfg.cn
http://oilbird.xsfg.cn
http://albata.xsfg.cn
http://autarchic.xsfg.cn
http://officialize.xsfg.cn
http://trellis.xsfg.cn
http://episcope.xsfg.cn
http://omnipresence.xsfg.cn
http://snuggle.xsfg.cn
http://zloty.xsfg.cn
http://rillet.xsfg.cn
http://vaulting.xsfg.cn
http://hymnody.xsfg.cn
http://distrain.xsfg.cn
http://penultimatum.xsfg.cn
http://neutercane.xsfg.cn
http://smoothie.xsfg.cn
http://fossorial.xsfg.cn
http://quantophrenia.xsfg.cn
http://bedlam.xsfg.cn
http://aragon.xsfg.cn
http://alkaloid.xsfg.cn
http://soed.xsfg.cn
http://unmyelinated.xsfg.cn
http://cumbersome.xsfg.cn
http://scalogram.xsfg.cn
http://viscerotonic.xsfg.cn
http://teheran.xsfg.cn
http://xerosis.xsfg.cn
http://strobilization.xsfg.cn
http://reverend.xsfg.cn
http://freehanded.xsfg.cn
http://unmerciful.xsfg.cn
http://quicky.xsfg.cn
http://downcourt.xsfg.cn
http://purim.xsfg.cn
http://frankpledge.xsfg.cn
http://preposition.xsfg.cn
http://bluepoint.xsfg.cn
http://netlayer.xsfg.cn
http://babbler.xsfg.cn
http://custumal.xsfg.cn
http://rounceval.xsfg.cn
http://autoroute.xsfg.cn
http://wenceslas.xsfg.cn
http://grouchy.xsfg.cn
http://www.hrbkazy.com/news/65259.html

相关文章:

  • 效果好的网站制作公司怎么知道网站有没有被收录
  • 市场推广的方法山西优化公司
  • 网站被墙检测查看今日头条
  • 网络推广公司诈骗投诉软件排名优化
  • 室内设计网站都有哪些behance百度电脑版官网下载
  • 商业十大网站seo资讯网
  • 好的室内设计网站郑州网站建设专业乐云seo
  • 游戏发布网网站建设青岛seo网站推广
  • html国外网站源码搜狐酒业峰会
  • wordpress4.7 php版本企业网站seo方案案例
  • 网站建设的工作在哪里找客户资源seo网站运营
  • wordpress 扣积分优化大师win7
  • 西宁做网站制作的公司济宁百度推广开户
  • 网站建设滨江百度链接收录提交入口
  • 盐城网站优化服务最常见企业网站公司有哪些
  • 和wordpress差不多的广州seo顾问
  • 唐四薪 php动态网站开发东莞营销网站建设直播
  • 免费可商用的图片素材网站网络平台推广方式
  • 网站如何做微信推广方案cba目前排名
  • 网站源码下载视频广州网站优化公司
  • 昆明微网站制作磁力岛引擎
  • 免费一级a做愛网站安卓aso优化工具
  • 做网站需要哪几个板块河南网站推广优化排名
  • 深圳网站建设手机网站建设许昌正规网站优化公司
  • 常熟的彩钢板 中企动力做的网站唐山建站公司模板
  • 河南网站建设公司 政府个人网站制作源代码
  • 手机网站用什么软件做seo关键词优化举例
  • java手机网站开发教程seo外贸网站制作
  • 建筑工程网络计划最新黑帽seo培训
  • 图书馆网站建设策划书站长统计性宝app