hashtable

HashTables in Cocoa

不羁岁月 提交于 2019-11-30 11:21:39
问题 HashTables/HashMaps are one of the most (if not the most) useful of data-structures in existence. As such, one of the first things I investigated when starting to learn programming in Cocoa was how to create, populate, and read data from a hashtable. To my surprise: all the documentation I've been reading on Cocoa/Objective-C programming doesn't seem to explain this much at all. As a Java developer that uses "java.util" as if it were a bodily function: I am utterly baffled by this. So, if

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

假如想象 提交于 2019-11-30 11:09:10
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? 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 Dictionary like this: var dict = myList.Cast<Foo>().ToDictionary(o => o.Description, o => o.Id); If you have access

Why can't you use null as a key for a Dictionary<bool?, string>?

人盡茶涼 提交于 2019-11-30 10:52:26
问题 Apparently, you cannot use a null for a key, even if your key is a nullable type. This code: var nullableBoolLabels = new System.Collections.Generic.Dictionary<bool?, string> { { true, "Yes" }, { false, "No" }, { null, "(n/a)" } }; ...results in this exception: Value cannot be null. Parameter name: key Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the

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

≡放荡痞女 提交于 2019-11-30 09:40:53
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); marshaller.marshal(object, writerTo); //create xml string from the input object return writerTo

System.Collections.Generic.Dictionary = Ultimate performance?

隐身守侯 提交于 2019-11-30 09:09:26
I'm writing a Haxe C# target, and I've been studying performance differences for Haxe's std library so we can provide the best performance possible through its cross platform code. One very good example is for the hash table code. I was a little reluctant about using .NET's Dictionary, as it seems bulky (structs for key/value pairs can take up a huge amount of memory because of memory alignment issues, besides from unnecessary information held by it), and since on the std library there is no such thing as an object hash, I really thought I could squeeze a little performance by not having to

Get size of ActionScript 3 Dictionary

眉间皱痕 提交于 2019-11-30 08:53:19
问题 var d:Dictionary = new Dictionary(); d["a"] = "b"; d["b"] = "z"; How to get the length/size of the dictionary (which is 2) ? 回答1: There is no built-in method to get the size/lenght/count of an AS3 dictionary. There are workarounds: for example, you can create a custom dictionary class which extends or wraps the flash.utils.Dictionary class, adding the counter functionality. You can manage the count as entries are added/removed, or count on-demand using a simple For loop iteration: public

How does one implement hash tables in a functional language?

心已入冬 提交于 2019-11-30 08:16:42
Is there any way to implement hash tables efficiently in a purely functional language? It seems like any change to the hash table would require creating a copy of the original hash table. I must be missing something. Hash tables are pretty darn important data structures, and a programming language would be limited without them. Is there any way to implement hash tables efficiently in a purely functional language? Hash tables are a concrete implementation of the abstract "dictionary" or "associative array" data structure. So I think you really want to ask about the efficiency of purely

Is the hashCode function generated by Eclipse any good?

一笑奈何 提交于 2019-11-30 08:07:56
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 (other.name != null) return false; } else if (!name.equals(other.name)) return false; return true; } If I

How can I enumerate a hashtable as key-value pairs / filter a hashtable by a collection of key values

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-30 08:00:35
问题 Editor's note: This question has a complicated history, but boils down to this: * To learn how to enumerate the entries of a hashtable by its key-value pairs , see the accepted answer. * To learn how to filter a hashtable by a collection of key values , see the other answer. I think I fell into the X Y problem again, my initial question was about Filtering a hash table. I discovered it's easier to filter before creating the hash table. Question answered, right? Nope, the Y problem was looping

Can we write a Hashtable to a file?

久未见 提交于 2019-11-30 06:57:42
I have a Hashtable<string,string> , in my program I want to record the values of the Hashtable to process later. My question is: can we write object Hastable to a file? If so, how can we later load that file? Yes, using binary serialization ( ObjectOutputStream ): FileOutputStream fos = new FileOutputStream("t.tmp"); ObjectOutputStream oos = new ObjectOutputStream(fos); oos.writeObject(yourHashTable); oos.close(); Then you can read it using ObjectInputStream The objects that you put inside the Hashtable (or better - HashMap ) have to implement Serializable If you want to store the Hashtable in