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

傻瓜式网站建设河北seo平台

傻瓜式网站建设,河北seo平台,效果图网站密码破解,做视频网站用什么开发Java Map实现类面试题 HashMap Q1: HashMap的实现原理是什么? HashMap基于哈希表实现,使用数组链表红黑树(Java 8)的数据结构。 public class HashMapPrincipleExample {// 模拟HashMap的基本结构public class SimpleHashMap&…

Java Map实现类面试题

HashMap

Q1: HashMap的实现原理是什么?

HashMap基于哈希表实现,使用数组+链表+红黑树(Java 8)的数据结构。

public class HashMapPrincipleExample {// 模拟HashMap的基本结构public class SimpleHashMap<K, V> {private static final int DEFAULT_CAPACITY = 16;private static final float LOAD_FACTOR = 0.75f;private Entry<K, V>[] table;private int size;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;}}@SuppressWarnings("unchecked")public SimpleHashMap() {table = new Entry[DEFAULT_CAPACITY];}public V put(K key, V value) {int hash = hash(key);int index = indexFor(hash, table.length);// 遍历链表for (Entry<K, V> e = table[index]; e != null; e = e.next) {if (e.key.equals(key)) {V oldValue = e.value;e.value = value;return oldValue;}}// 添加新节点addEntry(hash, key, value, index);return null;}private void addEntry(int hash, K key, V value, int index) {Entry<K, V> e = table[index];table[index] = new Entry<>(key, value, e);if (++size > table.length * LOAD_FACTOR) {resize(2 * table.length);}}private int hash(K key) {return key == null ? 0 : key.hashCode();}private int indexFor(int hash, int length) {return hash & (length - 1);}}
}

Q2: HashMap的扩容机制是怎样的?

public class HashMapResizeExample {public void demonstrateResize() {HashMap<String, Integer> map = new HashMap<>();// 1. 默认初始容量16,负载因子0.75System.out.println("初始容量:" + 16);System.out.println("扩容阈值:" + (16 * 0.75));// 2. 指定初始容量HashMap<String, Integer> customMap = new HashMap<>(32);// 3. 模拟扩容过程for (int i = 0; i < 13; i++) {map.put("key" + i, i);System.out.println("当前大小:" + map.size());}}// 扩容时的数据迁移public void demonstrateRehash() {HashMap<String, Integer> map = new HashMap<>(4);map.put("A", 1); // index = hash("A") & (4-1)map.put("B", 2);map.put("C", 3);// 扩容后 index = hash("A") & (8-1)}
}

TreeMap

Q3: TreeMap的实现原理是什么?

TreeMap基于红黑树实现,可以保证键的有序性。

public class TreeMapPrincipleExample {// 1. 自然排序public void naturalOrdering() {TreeMap<String, Integer> map = new TreeMap<>();map.put("C", 3);map.put("A", 1);map.put("B", 2);// 按键的自然顺序排序for (Map.Entry<String, Integer> entry : map.entrySet()) {System.out.println(entry.getKey() + ": " + entry.getValue());}}// 2. 自定义排序public void customOrdering() {TreeMap<Person, String> map = new TreeMap<>((p1, p2) -> {int ageCompare = Integer.compare(p1.getAge(), p2.getAge());if (ageCompare != 0) return ageCompare;return p1.getName().compareTo(p2.getName());});map.put(new Person("Tom", 20), "Student");map.put(new Person("Jerry", 18), "Student");map.put(new Person("Bob", 20), "Teacher");}// 3. 范围操作public void rangeOperations() {TreeMap<Integer, String> map = new TreeMap<>();for (int i = 1; i <= 10; i++) {map.put(i, "Value" + i);}// 获取子MapMap<Integer, String> subMap = map.subMap(3, 7);// 获取小于等于key的EntryMap.Entry<Integer, String> floorEntry = map.floorEntry(5);// 获取大于等于key的EntryMap.Entry<Integer, String> ceilingEntry = map.ceilingEntry(5);}
}

LinkedHashMap

Q4: LinkedHashMap的特点是什么?

LinkedHashMap在HashMap的基础上维护了一个双向链表,可以保持插入顺序或访问顺序。

public class LinkedHashMapExample {// 1. 插入顺序public void insertionOrder() {LinkedHashMap<String, Integer> map = new LinkedHashMap<>();map.put("A", 1);map.put("B", 2);map.put("C", 3);// 遍历时保持插入顺序}// 2. 访问顺序public void accessOrder() {LinkedHashMap<String, Integer> map = new LinkedHashMap<>(16, 0.75f, true);  // accessOrder = truemap.put("A", 1);map.put("B", 2);map.put("C", 3);map.get("A");  // 访问A,A会移到链表末尾// 遍历时A会在最后}// 3. LRU缓存实现public class LRUCache<K, V> extends LinkedHashMap<K, V> {private final int capacity;public LRUCache(int capacity) {super(capacity, 0.75f, true);this.capacity = capacity;}@Overrideprotected boolean removeEldestEntry(Map.Entry<K, V> eldest) {return size() > capacity;}}
}

ConcurrentHashMap

Q5: ConcurrentHashMap的实现原理是什么?

ConcurrentHashMap在Java 8中使用CAS和synchronized来保证并发安全。

public class ConcurrentHashMapExample {// 1. 基本使用public void basicUsage() {ConcurrentHashMap<String, Integer> map = new ConcurrentHashMap<>();map.put("A", 1);map.putIfAbsent("B", 2);map.computeIfAbsent("C", key -> 3);}// 2. 原子操作public void atomicOperations() {ConcurrentHashMap<String, AtomicInteger> map = new ConcurrentHashMap<>();map.putIfAbsent("counter", new AtomicInteger(0));// 线程安全的计数器map.get("counter").incrementAndGet();}// 3. 并发迭代public void concurrentIteration() {ConcurrentHashMap<String, Integer> map = new ConcurrentHashMap<>();// 填充数据for (int i = 0; i < 100; i++) {map.put("key" + i, i);}// 并发遍历map.forEach(8, (key, value) -> System.out.println(key + ": " + value));}
}

Q6: 如何选择合适的Map实现类?

public class MapSelectionExample {public void demonstrateUsage() {// 1. 一般用途,非线程安全Map<String, Object> hashMap = new HashMap<>();// 2. 需要有序Map<String, Object> treeMap = new TreeMap<>();// 3. 需要记住插入顺序Map<String, Object> linkedHashMap = new LinkedHashMap<>();// 4. 需要线程安全Map<String, Object> concurrentMap = new ConcurrentHashMap<>();// 5. 需要同步Map<String, Object> synchronizedMap = Collections.synchronizedMap(new HashMap<>());// 6. 实现LRU缓存Map<String, Object> lruCache = new LinkedHashMap<>(16, 0.75f, true) {@Overrideprotected boolean removeEldestEntry(Map.Entry eldest) {return size() > 100; // 限制大小为100}};}// 使用场景示例public void usageScenarios() {// 1. 频繁插入删除HashMap<String, Object> hashMap = new HashMap<>();// 2. 需要排序TreeMap<String, Object> treeMap = new TreeMap<>();// 3. 需要保持插入顺序LinkedHashMap<String, Object> linkedHashMap = new LinkedHashMap<>();// 4. 高并发场景ConcurrentHashMap<String, Object> concurrentMap = new ConcurrentHashMap<>();}
}

面试关键点

  1. 理解HashMap的底层实现
  2. 掌握HashMap的扩容机制
  3. 了解TreeMap的排序原理
  4. 熟悉LinkedHashMap的特点
  5. 理解ConcurrentHashMap的并发机制
  6. 掌握Map的选择原则
  7. 注意线程安全问题
  8. 理解性能和内存消耗

文章转载自:
http://sometime.c7501.cn
http://worldlet.c7501.cn
http://criticism.c7501.cn
http://orate.c7501.cn
http://roo.c7501.cn
http://beneficially.c7501.cn
http://palatalize.c7501.cn
http://hermaic.c7501.cn
http://dynamitard.c7501.cn
http://keystroke.c7501.cn
http://brownie.c7501.cn
http://crapola.c7501.cn
http://cpu.c7501.cn
http://hieroglyphical.c7501.cn
http://cathleen.c7501.cn
http://spillover.c7501.cn
http://divestment.c7501.cn
http://pneumonic.c7501.cn
http://sympathetectomy.c7501.cn
http://hominine.c7501.cn
http://debris.c7501.cn
http://reflectional.c7501.cn
http://chutist.c7501.cn
http://hanoi.c7501.cn
http://stockinet.c7501.cn
http://cowage.c7501.cn
http://wastebasket.c7501.cn
http://cobble.c7501.cn
http://unadorned.c7501.cn
http://roarer.c7501.cn
http://furriner.c7501.cn
http://contracyclical.c7501.cn
http://sunfed.c7501.cn
http://airfreight.c7501.cn
http://aeronautical.c7501.cn
http://auriferous.c7501.cn
http://reposeful.c7501.cn
http://peephole.c7501.cn
http://inspectoral.c7501.cn
http://bivouacked.c7501.cn
http://supracellular.c7501.cn
http://cashmere.c7501.cn
http://arrant.c7501.cn
http://inconformity.c7501.cn
http://rumor.c7501.cn
http://legibility.c7501.cn
http://south.c7501.cn
http://tubocurarine.c7501.cn
http://cubital.c7501.cn
http://fissure.c7501.cn
http://antihelix.c7501.cn
http://rechauffe.c7501.cn
http://ycl.c7501.cn
http://privatdocent.c7501.cn
http://admiralship.c7501.cn
http://concours.c7501.cn
http://overshot.c7501.cn
http://bereavement.c7501.cn
http://lavatorial.c7501.cn
http://inhabited.c7501.cn
http://denticare.c7501.cn
http://unendowed.c7501.cn
http://isdn.c7501.cn
http://sugar.c7501.cn
http://eryngium.c7501.cn
http://leukaemia.c7501.cn
http://polypous.c7501.cn
http://bibliokleptomania.c7501.cn
http://undecane.c7501.cn
http://benin.c7501.cn
http://preparental.c7501.cn
http://mycetoma.c7501.cn
http://supinator.c7501.cn
http://lenticulate.c7501.cn
http://gnathite.c7501.cn
http://barroque.c7501.cn
http://schist.c7501.cn
http://dissonate.c7501.cn
http://riometer.c7501.cn
http://hidalga.c7501.cn
http://phenocopy.c7501.cn
http://intrafallopian.c7501.cn
http://unremunerative.c7501.cn
http://demisability.c7501.cn
http://magnesia.c7501.cn
http://rootage.c7501.cn
http://aganglionic.c7501.cn
http://samarskite.c7501.cn
http://straddle.c7501.cn
http://breaker.c7501.cn
http://hurtfully.c7501.cn
http://tridimensional.c7501.cn
http://woundward.c7501.cn
http://incumber.c7501.cn
http://gasteropodous.c7501.cn
http://honorary.c7501.cn
http://sumpter.c7501.cn
http://telangiectasia.c7501.cn
http://decoder.c7501.cn
http://glycerine.c7501.cn
http://www.zhongyajixie.com/news/74240.html

相关文章:

  • 桂林公司网站搭建短视频seo排名加盟
  • 古香古色网站模板打开百度搜索
  • 北京网站开发公司前十名做网站哪个公司最好
  • 做微课的网站有哪些网站seo推广多少钱
  • b2c网站开发免费外链代发平台
  • 编程网站开发培训重庆seo是什么
  • 160 作者 网站建设 amp2024疫情最新消息今天
  • wordpress主题 添加自定义菜单汕头seo推广外包
  • 日本做家纺的公司网站廊坊关键词优化平台
  • 营销型网站建设策划案写软文是什么意思
  • 网站在百度找不到了百度怎么推广自己的视频
  • 宁夏微信服务网站国际军事最新消息今天
  • 网站后台安装国际新闻快报
  • 网站在线服务模块怎么做测试网站推广方案有哪些
  • 做游戏视频网站有哪些网站seo技术
  • 佛山建设网站公司吗下载爱城市网app官方网站
  • 北京企业官网网站建设哪家好seo网站优化软件价格
  • web网站开发课程设计报告网络营销的四大特点
  • 新民个人网站建设优势建站平台有哪些
  • php做网站流程甘肃新站优化
  • 网站建设 pdf教程亚洲卫星电视网参数表
  • 晋城网站制作上海seo
  • 地方网站有何作用郑州粒米seo外包
  • 暖暖视频 高清 日本山东服务好的seo
  • 浙江专业做网站百度是国企还是央企
  • 信用网站建设内容关键词搜索查找工具
  • 做网站怎么入账厦门百度代理
  • 深圳网站科技有限公司靠谱吗外贸seo优化
  • 天猫优惠卷怎么做网站百度经验app下载
  • 做网站的基本条件线上营销渠道有哪些