hashmap

Java 8: HashMap initialization with lambda expressions

自作多情 提交于 2019-12-21 03:35:09
问题 I'm trying to declare and define larger hash map at once. This is how I do it: public HashMap<Integer, Callable<String>> opcode_only = new HashMap<Integer, Callable<String>>() {{ put(x, y); put(x, y); }}; But, when I try to use lambda expressions in body of put , I'm hitting on eclipse warrning/error. This is how I use lambda in HashMap: public HashMap<Integer, Callable<String>> opcode_only = new HashMap<Integer, Callable<String>>() {{ put(0, () -> { return "nop"; }); put(1, () -> { return

How to convert String into Hashmap in java

a 夏天 提交于 2019-12-21 03:32:42
问题 How can I convert a String into a HashMap ? String value = "{first_name = naresh, last_name = kumar, gender = male}" into Map<Object, Object> = { first_name = naresh, last_name = kumar, gender = male } Where the keys are first_name , last_name and gender and the values are naresh , kumar , male . Note: Keys can be any thing like city = hyderabad . I am looking for a generic approach. 回答1: This is one solution. If you want to make it more generic, you can us StringUtils library. String value =

Reverse a hash in Ruby

房东的猫 提交于 2019-12-21 03:24:28
问题 How would I reverse the elements in the hash, keeping the same values and keys, but reversing their order in the hash. Like so: { "4" => "happiness", "10" => "cool", "lala" => "54", "1" => "spider" } And convert that to: { "1" => "spider", "lala" => "54", "10" => "cool", "4" => "happiness" } Or, perhaps I could run a each loop backwards, starting from the last element in the hash, rather than the first? 回答1: You could convert the Hash to an Array, reverse that, and then convert it back to a

How to detect duplicates in a vector of unordered_map?

淺唱寂寞╮ 提交于 2019-12-21 03:18:30
问题 Given a vector of unordered_map<u_int,int> , I would like to check if the vector contains any duplicated values. Two unordered_maps are considered duplicated if all of their keys and their corresponding values are equal. I know the comparison operator exists for unordered_maps , but I would like to avoid the pairwise comparison of each element with each other. One classical solution is to insert the values of the vector into a set , then to compare the number of elements in the set and the

Objective-C HashMap equivalent

百般思念 提交于 2019-12-21 03:15:29
问题 I am trying to convert a piece of Java code which uses a HashMap that contains as a key an object and a value as an object. private static HashMap<Class<? extends Component>, ComponentType> componentTypes = new HashMap<Class<? extends Component>, ComponentType>(); I've been reading on how to do this with Obj-C but I have not been successful, most people suggest using a NSDictionary, the problem is that they keys need to be strings and I need them as objects. The other option was NSMapTable,

Choose Trie or HashMap for storing a word frequency list?

£可爱£侵袭症+ 提交于 2019-12-21 02:57:04
问题 I have a txt file containing 1 million English word with their frequencies in this format: good 345667 bad 456777 ... I need to store it using either a HashMap or a Trie data structure in Java. Later on I need to look up words from the list without other operations. My understanding is that, the look up is slower for HashMap than Trie, but Trie will take up more memory usage, and the implementation of a Trie also takes effort, while HashMap already is ready for use. For production code, do

java中hashcode和equals的区别和联系

旧巷老猫 提交于 2019-12-21 02:54:53
HashSet和HashMap一直都是JDK中最常用的两个类,HashSet要求不能存储相同的对象,HashMap要求不能存储相同的键。 那么Java运行时环境是如何判断HashSet中相同对象、HashMap中相同键的呢?当存储了“相同的东西”之后Java运行时环境又将如何来维护呢? 在研究这个问题之前,首先说明一下JDK对equals(Object obj)和hashcode()这两个方法的定义和规范: 在Java中任何一个对象都具备equals(Object obj)和hashcode()这两个方法,因为他们是在Object类中定义的。 equals(Object obj)方法用来判断两个对象是否“相同”,如果“相同”则返回true,否则返回false。 hashcode()方法返回一个int数,在Object类中的默认实现是“将该对象的内部地址转换成一个整数返回”。 接下来有两个个关于这两个方法的重要规范(我只是抽取了最重要的两个,其实不止两个): 规范1:若重写equals(Object obj)方法,有必要重写hashcode()方法,确保通过equals(Object obj)方法判断结果为true的两个对象具备相等的hashcode()返回值。说得简单点就是:“如果两个对象相同,那么他们的hashcode应该 相等”。不过请注意:这个只是规范

Flink - state

三世轮回 提交于 2019-12-21 01:47:25
public class StreamTaskState implements Serializable, Closeable { private static final long serialVersionUID = 1L; private StateHandle<?> operatorState; private StateHandle<Serializable> functionState; private HashMap<String, KvStateSnapshot<?, ?, ?, ?, ?>> kvStates; Flink中state分为三种, 可以看到,StreamTaskState是对三种state的封装, 1. KVState 是最基本的state, 抽象是一对,KvState和KvStateSnapshot 通过两个接口,互相转化 /** * Key/Value state implementation for user-defined state. The state is backed by a state * backend, which typically follows one of the following patterns: Either the state is stored * in the key/value state object

Java面试题库及答案解析

核能气质少年 提交于 2019-12-20 23:48:06
1、面向对象编程(OOP)有哪些优点? 代码开发模块化,更易维护和修改。 代码复用。 增强代码的可靠性和灵活性。 增加代码的可理解性。 2、面向对象编程有哪些特性? 封装、继承、多态、抽象 封装 封装给对象提供了隐藏内部特性和行为的能力。对象提供一些能被其他对象访问的方法来改变它内部的数据。在Java当中,有3种修饰符:public,private和protected。每一种修饰符给其他的位于同一个包或者不同包下的对象赋予了不同的访问权限。 下面列出了使用封装的好处: 通过隐藏对象的属性来保护对象内部的状态。 提高了代码的可用性和可维护性,因为对象的行为可以被单独的改变或者是扩展。 禁止对象之间的不良交互提高模块化。 继承 继承给对象提供了从基类获取字段和方法的能力。继承提供了代码的重用,也可以在不修改类的情况下给现存的类添加新特性。 多态 多态是编程语言给不同的底层数据类型做相同的接口展示的一种能力。一个多态类型上的操作可以应用到其他类型的值上面。 抽象 抽象是把想法从具体的实例中分离出来的步骤,因此,要根据他们的功能而不是实现细节来创建类。Java支持创建只暴露接口而不包含方法实现的抽象的类。这种抽象技术的主要目的是把类的行为和实现细节分离开。 3、什么是Java虚拟机?为什么Java被称作是“平台无关的编程语言”? Java虚拟机是一个可以执行Java字节码的虚拟机进程

How to choose size of hash table?

时间秒杀一切 提交于 2019-12-20 23:29:13
问题 Suppose I have 200.000 of words, and I am going to use hash*33 + word[i] as a hash function, what should be the size of table for optimization, for minimum memory/paging issue? Platform used - C (c99 version), words are English char words, ASCII values One time initialization of hash table (buckets of link list style), used for searching next, like dictionary search. After collision , that word will be added as new node into bucket. 回答1: A good rule of thumb is to keep the load factor at 75%