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

外国网页设计网站系部网站建设中期检查表

外国网页设计网站,系部网站建设中期检查表,龙华做网站 熊掌号,安全的南昌网站制作Java Map实现类面试题 HashMap Q1: HashMap的实现原理是什么#xff1f; HashMap基于哈希表实现#xff0c;使用数组链表红黑树#xff08;Java 8#xff09;的数据结构。 public class HashMapPrincipleExample {// 模拟HashMap的基本结构public class SimpleHashMap HashMap基于哈希表实现使用数组链表红黑树Java 8的数据结构。 public class HashMapPrincipleExample {// 模拟HashMap的基本结构public class SimpleHashMapK, V {private static final int DEFAULT_CAPACITY 16;private static final float LOAD_FACTOR 0.75f;private EntryK, V[] table;private int size;private static class EntryK, V {K key;V value;EntryK, V next;Entry(K key, V value, EntryK, 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 (EntryK, 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) {EntryK, 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() {HashMapString, Integer map new HashMap();// 1. 默认初始容量16负载因子0.75System.out.println(初始容量 16);System.out.println(扩容阈值 (16 * 0.75));// 2. 指定初始容量HashMapString, 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() {HashMapString, 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() {TreeMapString, Integer map new TreeMap();map.put(C, 3);map.put(A, 1);map.put(B, 2);// 按键的自然顺序排序for (Map.EntryString, Integer entry : map.entrySet()) {System.out.println(entry.getKey() : entry.getValue());}}// 2. 自定义排序public void customOrdering() {TreeMapPerson, 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() {TreeMapInteger, String map new TreeMap();for (int i 1; i 10; i) {map.put(i, Value i);}// 获取子MapMapInteger, String subMap map.subMap(3, 7);// 获取小于等于key的EntryMap.EntryInteger, String floorEntry map.floorEntry(5);// 获取大于等于key的EntryMap.EntryInteger, String ceilingEntry map.ceilingEntry(5);} }LinkedHashMap Q4: LinkedHashMap的特点是什么 LinkedHashMap在HashMap的基础上维护了一个双向链表可以保持插入顺序或访问顺序。 public class LinkedHashMapExample {// 1. 插入顺序public void insertionOrder() {LinkedHashMapString, Integer map new LinkedHashMap();map.put(A, 1);map.put(B, 2);map.put(C, 3);// 遍历时保持插入顺序}// 2. 访问顺序public void accessOrder() {LinkedHashMapString, Integer map new LinkedHashMap(16, 0.75f, true); // accessOrder truemap.put(A, 1);map.put(B, 2);map.put(C, 3);map.get(A); // 访问AA会移到链表末尾// 遍历时A会在最后}// 3. LRU缓存实现public class LRUCacheK, V extends LinkedHashMapK, V {private final int capacity;public LRUCache(int capacity) {super(capacity, 0.75f, true);this.capacity capacity;}Overrideprotected boolean removeEldestEntry(Map.EntryK, V eldest) {return size() capacity;}} }ConcurrentHashMap Q5: ConcurrentHashMap的实现原理是什么 ConcurrentHashMap在Java 8中使用CAS和synchronized来保证并发安全。 public class ConcurrentHashMapExample {// 1. 基本使用public void basicUsage() {ConcurrentHashMapString, Integer map new ConcurrentHashMap();map.put(A, 1);map.putIfAbsent(B, 2);map.computeIfAbsent(C, key - 3);}// 2. 原子操作public void atomicOperations() {ConcurrentHashMapString, AtomicInteger map new ConcurrentHashMap();map.putIfAbsent(counter, new AtomicInteger(0));// 线程安全的计数器map.get(counter).incrementAndGet();}// 3. 并发迭代public void concurrentIteration() {ConcurrentHashMapString, 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. 一般用途非线程安全MapString, Object hashMap new HashMap();// 2. 需要有序MapString, Object treeMap new TreeMap();// 3. 需要记住插入顺序MapString, Object linkedHashMap new LinkedHashMap();// 4. 需要线程安全MapString, Object concurrentMap new ConcurrentHashMap();// 5. 需要同步MapString, Object synchronizedMap Collections.synchronizedMap(new HashMap());// 6. 实现LRU缓存MapString, Object lruCache new LinkedHashMap(16, 0.75f, true) {Overrideprotected boolean removeEldestEntry(Map.Entry eldest) {return size() 100; // 限制大小为100}};}// 使用场景示例public void usageScenarios() {// 1. 频繁插入删除HashMapString, Object hashMap new HashMap();// 2. 需要排序TreeMapString, Object treeMap new TreeMap();// 3. 需要保持插入顺序LinkedHashMapString, Object linkedHashMap new LinkedHashMap();// 4. 高并发场景ConcurrentHashMapString, Object concurrentMap new ConcurrentHashMap();} }面试关键点 理解HashMap的底层实现掌握HashMap的扩容机制了解TreeMap的排序原理熟悉LinkedHashMap的特点理解ConcurrentHashMap的并发机制掌握Map的选择原则注意线程安全问题理解性能和内存消耗
http://www.hkea.cn/news/14296360/

相关文章:

  • 怎么做扫码进入网站wordpress所有文章
  • 昆山建设网站公司学会网页设计找什么工作
  • 深圳整站seo网站开发与制作中期报告
  • 在网站做博客百度手机快速排名点击软件
  • 重庆市住房和城乡建设部网站网站制作哪家便宜
  • 网站怎么显示备案号简易个人网站模板
  • 网站推广优化外链特价做网站
  • 校园网站的建设费用太原市做网站
  • 网站推广的网站作用建设摩托车是杂牌吗
  • 奎文营销型网站建设wordpress粒子北京
  • 北京网站制作公司报价百度云建站网站建设
  • 深圳外贸平台建站微信网站地址
  • 网站开发项目实训江阴做网站的公司
  • 东莞整站优化排名展厅宣传片
  • 莱芜网站建设公众号建设兰州市城关区建设局网站
  • 有什么免费开发网站建设软件为企业规划一个网站
  • 地产平面网站中国安能建设集团有网站
  • 公司网站建设提纲沈阳妇科检查
  • 智慧园区建设总体方案苏州seo推广
  • 金乡县住房和城乡建设局网站二手商品网站制作
  • 建网站一条龙网站建设 网站专题 网络推广
  • 榆林免费做网站利尔化学股票
  • 移动营销型网站建设镇江营销型建站公叿
  • 昆明企业网站制作房地产微网站
  • 国泰君安建设工程官方网站一达通外贸综合服务平台
  • 有源码手机怎么搭建网站做一个网站的流程是什么
  • 无锡网站推广优化费用wordpress账号权限
  • 做网站的公司图wordpress 打赏实现
  • 搜狐视频网站联盟怎么做综合网站建设课程设计
  • 上海网站建设报沈阳城市建设学院信息与控制工程系