hashmap

Java集合类--HashMap

烂漫一生 提交于 2019-12-25 14:07:36
一、HashMap基本源码实现 1、HashMap基本结构 HashMap继承AbstractMap抽象类,AbstractMap实现Map接口。 public class HashMap<K,V> extends AbstractMap<K,V> implements Map<K,V>, Cloneable, Serializable { private static final long serialVersionUID = 362498820763181265L; .... } public abstract class AbstractMap<K,V> implements Map<K,V> { protected AbstractMap() { } ... } 二、HashMap的基本数据结构 jdk1.8之前,HashMap是由“数组+链表”的结构组成,jdk1.8之后,HashMap得到很大的改善;数据结构也发生了改变:“数组+链表+红黑树”;当链表节点大于8时,会转换为红黑树;否则仍然以链表结构。 头节点指的是table表上索引位置的节点,也就是链表的头节点 根节点(root节点)指的是红黑树最上面的那个节点,也就是没有父节点的节点 红黑树的根节点不一定是索引位置的头节点 转为红黑树节点后,链表的结构还存在,通过next属性维持

各大公司Java后端开发面试题

吃可爱长大的小学妹 提交于 2019-12-25 13:14:40
各大公司Java后端开发面试题 原创 2017年03月01日 11:31:11 标签: java / 面试题 / Spring / 春招 / BAT 40184 ThreadLocal(线程变量副本) Synchronized实现内存共享,ThreadLocal为每个线程维护一个本地变量。 采用空间换时间,它用于线程间的数据隔离,为每一个使用该变量的线程提供一个副本,每个线程都可以独立地改变自己的副本,而不会和其他线程的副本冲突。 ThreadLocal类中维护一个Map,用于存储每一个线程的变量副本,Map中元素的键为线程对象,而值为对应线程的变量副本。 ThreadLocal在Spring中发挥着巨大的作用,在管理Request作用域中的Bean、事务管理、任务调度、AOP等模块都出现了它的身影。 Spring中绝大部分Bean都可以声明成Singleton作用域,采用ThreadLocal进行封装,因此有状态的Bean就能够以singleton的方式在多线程中正常工作了。 友情链接: 深入研究java.lang.ThreadLocal类 Java内存模型: Java虚拟机规范中将Java运行时数据分为六种。 1.程序计数器:是一个数据结构,用于保存当前正常执行的程序的内存地址。Java虚拟机的多线程就是通过线程轮流切换并分配处理器时间来实现的,为了线程切换后能恢复到正确的位置

Convert a list of objects to a hashmap in Java

橙三吉。 提交于 2019-12-25 12:06:50
问题 I have a java List<Object> and each object is a String containing a String and an Integer. I have no control over the List since it is returned by an API method. I'd like to cast the List to a Map. What is the best way to do this? 回答1: Create a Map of the type you like (HashMap, TreeMap, HashTable,...) and then iterate through your list and add all elements one by one. There is no better way (except if you are willing to code your own map that is backed by this list which - in some cases -

How to obfuscate an integer?

不问归期 提交于 2019-12-25 12:03:26
问题 From a list of integers in C#, I need to generate a list of unique values. I thought in MD5 or similar but they generates too many bytes. Integer size is 2 bytes. I want to get a one way correspondence, for example 0 -> ARY812Q3 1 -> S6321Q66 2 -> 13TZ79K2 So, proving the hash, the user cannot know the integer or to interfere a sequence behind a list of hashes. For now, I tried to use MD5(my number) and then I used the first 8 characters. However I found the first collision at 51389. Which

How to obfuscate an integer?

喜夏-厌秋 提交于 2019-12-25 12:00:03
问题 From a list of integers in C#, I need to generate a list of unique values. I thought in MD5 or similar but they generates too many bytes. Integer size is 2 bytes. I want to get a one way correspondence, for example 0 -> ARY812Q3 1 -> S6321Q66 2 -> 13TZ79K2 So, proving the hash, the user cannot know the integer or to interfere a sequence behind a list of hashes. For now, I tried to use MD5(my number) and then I used the first 8 characters. However I found the first collision at 51389. Which

JSONObject from HashMap - Cast to String failed

依然范特西╮ 提交于 2019-12-25 09:48:48
问题 I need to create a JSONObject from a HashMap of a custom class's toString and a float value. I tried the code below hoping that it would just work: public class MyClass { ... public String toString() { return "a nice string" } } HashMap<MyClass,Float> map = new HashMap<MyClass,Float>(); map.put(...); JSONObject json = new JSONObject(map); But I get: java.lang.ClassCastException: MyClass cannot be cast to java.lang.String What's the best way to create this JSONObject ? Thanks. 回答1: You need to

Connect TableView columns to the HashMap values

只谈情不闲聊 提交于 2019-12-25 08:52:27
问题 I'm writing a simple crud-redactor, and there is a problem I stuck on: I have a class: public class Note { List<String> columnNames; HashMap<String, SimpleStringProperty> items; } which I need to somehow connect with a TableView via setCellValueFactory . For example I have 3 values in HashMap with keys "id" , "name" and "age" . So I want to have 3 columns in TableView which are connected with this HashMap and will represent its values after wrapping an array of Notes into ObservableList and

Grouping of words from a text file to Arraylist on the basis of length

大城市里の小女人 提交于 2019-12-25 07:58:35
问题 public class JavaApplication13 { /** * @param args the command line arguments */ public static void main(String[] args) { // TODO code application logic here BufferedReader br; String strLine; ArrayList<String> arr =new ArrayList<>(); HashMap<Integer,ArrayList<String>> hm = new HashMap<>(); try { br = new BufferedReader( new FileReader("words.txt")); while( (strLine = br.readLine()) != null){ arr.add(strLine); } } catch (FileNotFoundException e) { System.err.println("Unable to find the file:

potential resource leak (unassigned Closeable) with a HashMap

旧巷老猫 提交于 2019-12-25 07:38:10
问题 I have one static HashMap for my entire system which contains some object's references; let's call it myHash . The objects are only instantiated once I needed them such as private static HashMap<String, lucene.store.Directory> directories; public static Object getFoo(String key) { if (directories == null) { directories = new HashMap<String, Directory>(); } if (directories.get(key) == null) { directories.put(key, new RAMDirectory()); } return directories.get(key); // warning } Now, Eclipse is

Java: How to use a pair of keys for HashMap

元气小坏坏 提交于 2019-12-25 06:29:14
问题 From a sql database, I am reading a table with 2 fields: appName, user. So I may see: +-------------+ | appA | John | +-------------+ | appB | Mary | +-------------+ | appC | Tom | +-------------+ | appA | John | +-------------+ | appA | Mary | +-------------+ Each of these records is stored as an object of a AppClass with appName and user. Now, i want to list how many times the apps were run by different users. So : +-----------------+ | appA | John | 2 | +-----------------+ | appA | Mary |