hashmap

Removing all items of a given value from a hashmap

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-18 03:09:32
问题 So I have a java hashmap like below: hMap.put("1", "One"); hMap.put("2", "Two"); hMap.put("3", "Two"); I would like to remove ALL items where the value is "Two" If I do something like: hmap.values().remove("Two"); Only the first one is deleted, I want to remove them all, how can this be done? 回答1: hmap.values().removeAll(Collections.singleton("Two")); EDIT: the (significant) disadvantage with this concise approach is that you are basically forced to comment it, saying something like // remove

HashMap with Null Key and Null Value

有些话、适合烂在心里 提交于 2019-12-18 03:08:29
问题 Consider the following Code : import java.util.*; class Employee { String name; public Employee(String nm) { this.name=nm; } } public class HashMapKeyNullValue { Employee e1; public void display(){ Employee e2=null; Map map=new HashMap(); map.put(e2, "25"); System.out.println("Getting the Value When e2 is set as KEY"); System.out.println("e2 : "+map.get(e2)); System.out.println("e1 : "+map.get(e1)); System.out.println("null : "+map.get(null)); map.put(e1, ""); System.out.println("Getting the

How to show hashmap values in jsf?

最后都变了- 提交于 2019-12-18 02:47:48
问题 I have bean "MyBean", which has property HashMap - "map" which values type is MyClass. I want to show some properties of map in jsf using ui:repeat. But these code: <ui:repeat var="var" value="#{mybean.map}" > <tr> <td> <h:outputText value="#{var.value.property1}"></h:outputText> </td> <td><h:outputText value="#{var.value.property2}"></h:outputText></td> </tr> </ui:repeat> But this code didn't show anything. Though when I try to show hashmap values in jsp this way, it was succesfull. Where I

hashmap和hashtable

夙愿已清 提交于 2019-12-18 01:31:12
HashTable 底层数组+链表实现,无论key还是value都 不能为null ,线程 安全 ,实现线程安全的方式是在修改数据时锁住整个HashTable,效率低,ConcurrentHashMap做了相关优化 初始size为 11 ,扩容:newsize = olesize*2+1 计算index的方法:index = (hash & 0x7FFFFFFF) % tab.length HashMap 底层数组+链表实现,可 以存储null键和null值 ,线程 不安全 初始size为 16 ,扩容:newsize = oldsize*2,size一定为2的n次幂 扩容针对整个Map,每次扩容时,原来数组中的元素依次重新计算存放位置,并重新插入 插入元素后才判断该不该扩容,有可能无效扩容(插入后如果扩容,如果没有再次插入,就会产生无效扩容) 当Map中元素总数超过Entry数组的75%,触发扩容操作,为了减少链表长度,元素分配更均匀 计算index方法:index = hash & (tab.length – 1) HashMap的初始值还要考虑加载因子: 哈希冲突 :若干Key的哈希值按数组大小取模后,如果落在同一个数组下标上,将组成一条Entry链,对Key的查找需要遍历Entry链上的每个元素执行equals()比较。 加载因子 :为了降低哈希冲突的概率

Java集合类的概述

我的未来我决定 提交于 2019-12-18 01:25:38
前述   复习一下Java中的集合类,是面试笔试中常考察的一个点,特地做的整理。 什么是集合类?   集合类,也叫容器类。Java集合类可以用来存储数量庞大的对象。   我们和数组进行对比:   数组:存储基本数据类型,数据类型单一,长度固定,不能动态增大容量。   集合:存储的即可是基本类型的值,也可以是对象,可以存储多种数据类型,长度可变,可以动态增大容量。 Java集合类的体系   Java集合类主要有两个接口派生而出:Collection和Map。即集合类都是实现的这两个接口。我们在实际编程中经常使用的有 List、Set、Queue(这些是实现的 Collection 接口)HashMap、TreeMap、HashTable(这些实现的 Map 接口) Collection接口结构   Collection 接口位于 Java.util 包下,是一个父接口, List、Set、Queue 都是实现的 Collection 接口。Collection 做为父接口提供一些操作集合类的方法,因此它的子接口也有这些方法。   Collection 接口不能被实例化,并且在实际的编程过程中几乎不会使用它进行数据的存储。 Map接口结构   Map 接口实现的是键值对的存储,类似 python 中的 dict。   Map中比较常见的是 HashMap、TreeMap

Correct way to initialize HashMap and can HashMap hold different value types?

陌路散爱 提交于 2019-12-17 23:19:19
问题 So I have two questions about HashMap s in Java: What is the correct way to initialize a HashMap ? I think it might be best in my situation to use: HashMap x = new HashMap(); But Eclipse keeps suggesting that I use: HashMap<something, something> map = new HashMap(); Which is better? Can a HashMap hold different types of objects/data types as values? For example, would this work and be OK: map.put("one", 1); map.put("two", {1, 2}); map.put("three", "hello"); In the first put() , I want an int

hashmap实现原理浅析

谁说我不能喝 提交于 2019-12-17 22:55:42
看了下JAVA里面有HashMap、Hashtable、HashSet三种hash集合的实现源码,这里总结下,理解错误的地方还望指正 HashMap和Hashtable的区别 HashSet和HashMap、Hashtable的区别 HashMap和Hashtable的实现原理 HashMap的简化实现MyHashMap HashMap和Hashtable的区别 两者最主要的区别在于Hashtable是线程安全,而HashMap则非线程安全 Hashtable的实现方法里面都添加了synchronized关键字来确保线程同步,因此相对而言HashMap性能会高一些,我们平时使用时若无特殊需求建议使用HashMap,在多线程环境下若使用HashMap需要使用Collections.synchronizedMap()方法来获取一个线程安全的集合( Collections.synchronizedMap()实现原理是Collections定义了一个SynchronizedMap的内部类,这个类实现了Map接口,在调用方法时使用synchronized来保证线程同步,当然了实际上操作的还是我们传入的HashMap实例,简单的说就是Collections.synchronizedMap()方法帮我们在操作HashMap时自动添加了synchronized来实现线程同步

HashMap in Java, 100 Million entries

心不动则不痛 提交于 2019-12-17 21:53:33
问题 I want to store 100 Million terms and their frequencies (in a text database ) into a HashMap <String, Double> . It is giving me "Out of Memory" Error. I tried to increase the heap-space to -Xmx15000M . However it runs half an hour then again throw the same exception. The file size from which I'm trying to read the words and frequencies is 1.7GB. Any help would be much appreciated. Thanks :-) 回答1: For word processing like that the answer is usually a tree rather than hashmap, if you can live

Create a map from a list of maps

霸气de小男生 提交于 2019-12-17 19:49:18
问题 I have a list of maps. List<Map<Integer, String>> The values in the list are, for example <1, String1> <2, String2> <1, String3> <2, String4> As an end result, I want a Map>, like <1, <String1, String3>> <2, <String2, String4>> How can I achieve this in Java. CODE : List<Map<Integer, String>> genericList = new ArrayList<Map<Integer,String>>(); for(TrackActivity activity : activityMajor){ Map<Integer, String> mapIdResponse = activity.getMapIdResponse(); genericList.add(mapIdResponse); } Now

Hashmap stuck on get

懵懂的女人 提交于 2019-12-17 19:47:23
问题 I have a strange issue with HashMap. There are multiple threads that accessing same hashmap (not threadsafe). Sometime, the process gets stuck. when I inspect the thread stack, i see many threads in state: java.lang.Thread.State: RUNNABLE at java.util.HashMap.get(HashMap.java:303) Note this happens very rare. And can't be reproduced on demand. Why it gets stuck? There is no synchronization on hashmap. keys are strings 回答1: There are multiple threads that accessing same hashmap (not threadsafe