hashmap

Android - passing hashmap through intent

谁说胖子不能爱 提交于 2020-01-25 11:43:06
问题 I'm trying to pass a Hashmap of custom objects through an intent. I am trying to use parcelable as that's what i read is the correct way to do things in android (i.e. parcelable over serializable). However, when i try and get the hashmap using getParcelable, it can't find my hashmap in the intent, but getSerializable can. Is it OK to use serializable for hashmaps? Is this my only option? 回答1: You can of course serialize your map, as long as your Objects are serializable. What you also could

ConcurrentHashMap(Java 8)

老子叫甜甜 提交于 2020-01-25 11:40:14
HashMap 是我们日常最常见的一种容器,它以键值对的形式完成对数据的存储,但众所周知,它在高并发的情境下是不安全的。尤其是在 jdk 1.8 之前,rehash 的过程中采用头插法转移结点,高并发下,多个线程同时操作一条链表将直接导致闭链,死循环并占满 CPU。 当然,jdk 1.8 以来,对 HashMap 的内部进行了很大的改进,采用数组+链表+红黑树来进行数据的存储。rehash 的过程也进行了改动,基于复制的算法思想,不直接操作原链,而是定义了两条链表分别完成对原链的结点分离操作,即使是多线程的情况下也是安全的。当然,它毕竟不是并发容器,除非大改,否则依然是不能应对高并发场景的,或者说即使没有因多线程访问而造成什么问题,但是效率必然是受到影响的。例如在多线程同时添加元素的时候可能会丢失数据,迭代 map 的时候发生 fail-fast 等。 本篇文章将要介绍的 ConcurrentHashMap 是 HashMap 的并发版本,它是线程安全的,并且在高并发的情境下,性能优于 HashMap 很多。我们主要从以下几个方面对其进行学习: 历史版本的实现演变 重要成员属性的介绍 put 方法实现并发添加 remove 方法实现并发删除 其他的一些方法的简单介绍 一、历史版本的实现演变 jdk 1.7 采用分段锁技术,整个 Hash 表被分成多个段,每个段中会对应一个

Java append `HashMap` values to existing HashMap if key matches

别说谁变了你拦得住时间么 提交于 2020-01-25 08:11:06
问题 I have below HashMap(to.String()) printed below. HashMap<String, HashMap<String, HashMap<String, Integer>>> abc = new HashMap<>(); HashMap abc = {disabled={account={testConfiguration=1, iterate=1}}} I want to append {group={iterate=1}} to existing map if key disabled matches. Finally my map should look like below, how can I achieve it? HashMap abc = {disabled={account={testConfiguration=1, iterate=1}, {group={iterate=1}}} 回答1: I think you want this: abc.computeIfPresent("disabled", (k,v) -> {

Java append `HashMap` values to existing HashMap if key matches

ぐ巨炮叔叔 提交于 2020-01-25 08:10:05
问题 I have below HashMap(to.String()) printed below. HashMap<String, HashMap<String, HashMap<String, Integer>>> abc = new HashMap<>(); HashMap abc = {disabled={account={testConfiguration=1, iterate=1}}} I want to append {group={iterate=1}} to existing map if key disabled matches. Finally my map should look like below, how can I achieve it? HashMap abc = {disabled={account={testConfiguration=1, iterate=1}, {group={iterate=1}}} 回答1: I think you want this: abc.computeIfPresent("disabled", (k,v) -> {

HashMap get/put complexity

爷,独闯天下 提交于 2020-01-25 07:33:27
问题 We are used to saying that HashMap get/put operations are O(1). However it depends on the hash implementation. The default object hash is actually the internal address in the JVM heap. Are we sure it is good enough to claim that the get/put are O(1) ? Available memory is another issue. As I understand from the javadocs, the HashMap load factor should be 0.75. What if we do not have enough memory in JVM and the load factor exceeds the limit ? So, it looks like O(1) is not guaranteed. Does it

HashMap和Hashtable区别

泄露秘密 提交于 2020-01-25 06:30:25
区别 1、线程区别 首先看HashMap的源码在添加时的处理 public V put(K key, V value) { return putVal(hash(key), key, value, false, true); } 其次看Hashtable的源码在添加时的处理 public synchronized V put(K key, V value) { // Make sure the value is not null if (value == null) { throw new NullPointerException(); } 后者在操作时添加了synchronized保证了线程安全,Hashtable数据操作的时候都会上锁,所以效率比较低下。 2、为什么HashMap可以添加空key,而Hashtable不行呢? HashMap在添加空key的时候做了特殊处理,如果key的null的话就等于0 static final int hash(Object key) { int h; return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16); } Hashtable在添加时做了判断如果key为空时会抛出空异常 if (value == null) { throw new

HashMap与HashTable

吃可爱长大的小学妹 提交于 2020-01-24 23:56:47
HashMap HashMap底层是一个Entry[]数组;当发生hash冲突的时候,hashMap是采用链表的方式来解决;在对应的数组位置存放链表的头节点;新加入的节点会从头节点加入;超过阀值(默认8),单链表就会转为红黑树,提高检索速度; HashMap扩容的条件是:当size大于threshold时,对HashMap进行扩容(threshold = 容量*加载因子)默认加载因子为0.75 扩容是是新建了一个HashMap的底层数组,而后调用transfer方法,将旧HashMap的全部元素添加到新的HashMap中(要重新计算元素在新的数组中的索引位置)。 扩容是一个相当耗时的操作,因为它需要重新计算这些元素在新的数组中的位置并进行复制处理。因此,我们在用HashMap的时,最好能提前预估下HashMap中元素的个数,这样有助于提高HashMap的性能。 HashTable和HashMap的区别 继承的父类不同 HashTable继承自Dictionary类,而HashMap继承自AbstractMap类;但两者都实现了Map接口; 线程安全不同 HashTable中的方法是Synchronized的,而HashMap是非Synchronized的;在多线程并发环境下,可以考虑使用HashTable,不需要为自己的方法实现同步;而HashMap就需要自己增加同步;

五,图形界面编程

时光毁灭记忆、已成空白 提交于 2020-01-24 21:25:09
五,图形界面编程 一,图形界面编程 swt; 1,awt 1,图形类 Button 2,辅助类 1,工具类 2,字体类 3,颜色类 2,Swing 1,图形类 jButton 二,容器与组件 1,首层容器 Contoner 1 ,所有的中间容器和组件都放在首层容器里 2 ,常用的首层容器 JApplet-动画效果-过时 Jdiologo-弹出框 Jframe-常用的首层容器 Jwindow 3 ,先 new jframe对象,在设置宽高 ,ste size(宽,高); 内宽高,先水平在垂直先宽后高,单位像素 4 ,设置窗体大小为可不可变 setResizable(false); 5 ,设置外边框 setLocation(x,y); 内宽高,先水平在垂直先宽后高,单位像素 6 ,工具类的调用; Toolkit tk = Toolkit.getDefaultToolkit(); tk.getScreenSize().getWidth();获取当前系统的像素宽度, getScreenSize().getHeight();获取当前系统的像素高度 以上两个返回类型是 double类型的 7 ,设置标题; this.setTitle("我的第一个窗体"); 8 ,设置图标; this.setIconImage(tk.createImage("img/hp.JPG")); 9 ,设置窗体关闭

sending HashMap by angularjs $http.get in spring mvc

≯℡__Kan透↙ 提交于 2020-01-24 19:19:01
问题 I want to send and retrieve HashMap through angularjs and receive it in springmvc controller. I have successfully send and received List but unable to send HashMap. My code is. $scope.addskill = function(skills){ // $scope.list = []; // $scope.list.push(skills.skillName, skills.expMonth, skills.expYear, skills.experties); var map = {}; map['name'] = skills.skillName; map['month'] = skills.expMonth; map['year'] = skills.expYear; map['experties'] = skills.experties; alert(map['name']); var

sending HashMap by angularjs $http.get in spring mvc

拜拜、爱过 提交于 2020-01-24 19:18:29
问题 I want to send and retrieve HashMap through angularjs and receive it in springmvc controller. I have successfully send and received List but unable to send HashMap. My code is. $scope.addskill = function(skills){ // $scope.list = []; // $scope.list.push(skills.skillName, skills.expMonth, skills.expYear, skills.experties); var map = {}; map['name'] = skills.skillName; map['month'] = skills.expMonth; map['year'] = skills.expYear; map['experties'] = skills.experties; alert(map['name']); var