hashtable

Cast a hashtable.Keys into List<int> or other IEnumerable<int>

狂风中的少年 提交于 2019-11-29 18:25:36
问题 I know, I have other options, e.g. I could maintain a separate list of keys. Please don't suggest other options. I simply want to know if I can pull this off. Please don't ask me what problem I'm trying to solve, or anything like that. This is a pure and simple CS question. I want to know if anyone knows of a way to take the keys from a Hashtable and cast them into a List<int> or some other type of IEnumerable<int> (given of course that my keys are in fact integers). Given that I can do this

How can I convert List<object> to Hashtable in C#?

谁都会走 提交于 2019-11-29 16:46:13
问题 I have a list of objects, each containing an Id, Code and Description. I need to convert this list into a Hashtable, using Description as the key and Id as the value. This is so the Hashtable can then be serialised to JSON. Is there a way to convert from List<Object> to Hashtable without writing a loop to go through each item in the list? 回答1: Let's assume that your List contains objects of type Foo (with an int Id and a string Description). You can use Linq to turn that list into a

Unexpected floating-point representations in Python

与世无争的帅哥 提交于 2019-11-29 15:14:10
Hello I am using a dictionary in Python storing some cities and their population like that: population = { 'Shanghai' : 17.8, 'Istanbul' : 13.3, 'Karachi' : 13.0, 'mumbai' : 12.5 } Now if I use the command print population , I get the result: {'Karachi': 13.0, 'Shanghai': 17.800000000000001, 'Istanbul': 13.300000000000001, 'mumbai': 12.5} whereas if I use the command print population['Shanghai'] I get the initial input of 17.8 . My question to you is how does the 17.8 and the 13.3 turned into 17.800000000000001 and 13.300000000000001 respectively? How was all that information produced? And why

How to serialize HashTable<String, String> to XML using JAXB?

时光毁灭记忆、已成空白 提交于 2019-11-29 14:53:40
问题 I am trying to use JAXB to serialize a HashTable<String, String> to XML. I am very new to Java (came from C#), so I am kinda perplexed by this task. I have seen the following code: public static <T> String ObjectToXml(T object, Class<T> classType) throws JAXBException { JAXBContext jaxbContext = JAXBContext.newInstance(classType); StringWriter writerTo = new StringWriter(); Marshaller marshaller = jaxbContext.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

Hashmap and hashtable in multithreaded environment

て烟熏妆下的殇ゞ 提交于 2019-11-29 13:56:20
I am really confused on how these 2 collections behave in multithreaded environment. Hash table is synchronized that means no 2 threads will be updating its value simultaneously right? Look at ConcurrentHashMap s for Thread safe Maps. They offer all the features of HashTable with a performance very close to a HashMap. Performance is gained by instead of using a map wide lock, the collection maintains a list of 16 locks by default, each of which is used to lock a single bucket of the map. You can even configure the number of buckets :) Tweaking this can help performance depending on your data.

What are the differences between Hashmap vs Hashtable in theory?

半腔热情 提交于 2019-11-29 12:07:30
Are there are differences between hashmap and hashtable in theory? I don't mean in the concrete definitions given in Java (or the implementation), but in theory. Isn't a hashtable a map that uses hashing ... hence a hashmap? woliveirajr According to Wikipedia , they are the same: In computing, a hash table (hash map) is a data structure used to implement an associative array (...) According to Wikibooks , it's the same: A hash table, or a hash map, is a data structure that associates keys with values. Some answer on StackOverflow also states: Hashtable is often useful (they are also called

If key in hashtable is a class object, how does containsKey work?

谁说我不能喝 提交于 2019-11-29 11:40:19
When we put a class Object (that has say three data members) in a hashtable, how can I prevent putting another entry into the hash table whose key has the same three data members ? Cos I am guessing this will be a new object. So hashtable.containsKey() will return false even when there is a key (this class object) that has the very same data members as the one that is waiting to be inserted. More clearly: I have a class like class Triplet { private Curr curr; private Prev prev; private Next next; } I have a hashtable structure like: Hashtable<Triplet, Integer> table = new Hashtable<Triplet,

Why are hash maps better than trie maps?

巧了我就是萌 提交于 2019-11-29 10:50:07
By trie map I mean an associative array, where the payloads are stored in a trie instead of a hash table. When I'm using a hash map/table, the keys I use are typically strings. What are the advantages of a hash map over some trie based map? I have read that a hash map is faster - but it seems to me that a consistent hash functions would have to check each element of the (char) array for the final hash - iterating over the array once. In a trie you would similarly have to iterate over the array just once. It does seem to me that this would use a lot more memory when encoding small objects (even

Is the hashCode function generated by Eclipse any good?

家住魔仙堡 提交于 2019-11-29 10:44:33
问题 Eclipse source menu has a "generate hashCode / equals method" which generates functions like the one below. String name; @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((name == null) ? 0 : name.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; CompanyRole other = (CompanyRole) obj; if (name == null) { if

Java中HashMap和HashTable区别

徘徊边缘 提交于 2019-11-29 09:41:08
前几天被一家公司电面的时候被问到HashMap和HashTable的区别,当时就懵逼了,hashTable是个啥?从来没用过啊,于是电面完之后马上google了一把,这回涨姿势了; HashMap和HashTable同属于Java.util包下的集合类,Hashtable是个过时的集合类,存在于Java API中很久了。在Java 4中被重写了,实现了Map接口,所以自此以后也成了Java集合框架中的一部分。 HashMap和Hashtable都实现了Map接口,但决定用哪一个之前先要弄清楚它们之间的分别。主要的区别有:线程安全性,同步(synchronization),以及速度。 对比项 HashMap HashTable 是否线程安全 否 是 是否synchronized 否 是 是否支持多线程共享 在有正确的同步机制的前提下可以支持多线程共享 支持 单线程效率 高 底 只随时间推移元素次序是否不变 不一定 是 是否支持null的key和value 支持 不支持 另一个区别是HashMap的迭代器(Iterator)是fail-fast迭代器,而Hashtable的enumerator迭代器不是fail-fast的。所以当有其它线程改变了HashMap的结构(增加或者移除元素),将会抛出ConcurrentModificationException,但迭代器本身的remove(